22.03.2013 Views

Self study guide: Fortran 95 - University of Cambridge

Self study guide: Fortran 95 - University of Cambridge

Self study guide: Fortran 95 - University of Cambridge

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

• Finally the main program testset.f90<br />

program testset<br />

! include global data<br />

use gd<br />

! make available the setval routine<br />

use setval<br />

real :: x, y<br />

! read values from terminal<br />

read(*,*) a_g, b_g<br />

! set x and y and check output<br />

call setv(x, y)<br />

write(*,*) x, y<br />

end program testset<br />

• Compile the two modules:<br />

f<strong>95</strong> -c gd.f90<br />

f<strong>95</strong> -c setval.f90<br />

• Now the main program, creating an executable program:<br />

f<strong>95</strong> -o testset testset.f90 gd.o setval.o<br />

• Run the program and check the output.<br />

These two examples, although simple, illustrate the usefulness <strong>of</strong> modules.<br />

6.2 public and private attributes<br />

The examples <strong>of</strong> modules in the previous section were quite simple. In general you<br />

are likely to want a module which not only declares some global data, but also defines<br />

routines in the same module. However we may want to differentiate variables which<br />

are global and available in all routines in which the module is used, as we saw in the<br />

last section, and variables which are to be global only within the module (i.e. used by<br />

all the routines in the module, but not available in the routines or program segments<br />

which include the module via the use statement). We can in fact achieve this using<br />

the private and public attributes. For example the variable a_g in the<br />

following will be available in all program segments including the module in which<br />

this definition occurs, whereas the variable b is a global variable only within the<br />

current module<br />

real, public :: a_g<br />

real, private :: b<br />

The default is for variables is to be public.<br />

41

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!