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.

We should now have a look at some examples. Let’s return to the swapmain<br />

program from section 5.3 and implement this in two files using modules. First edit<br />

the file swapmain.f90 and to contain:<br />

program swapmain<br />

use swapmod ! use statements must come first<br />

implicit none<br />

real :: a, b<br />

! Read in two values<br />

read(*,*) a, b<br />

call swap(a,b)<br />

write(*,*) a, b<br />

end program swapmain<br />

Now in the second file, called swapmod.f90 the module is defined:<br />

module swapmod<br />

implicit none ! you need this in every module<br />

!<br />

! Global declarations would appear here if any<br />

!<br />

contains<br />

! routines provided by this module<br />

subroutine swap(x, y)<br />

real :: x, y, temp<br />

temp = x<br />

x = y<br />

y = temp<br />

end subroutine swap<br />

end module swapmod<br />

• Now compile the swapmod.f90 file:<br />

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

The –c option is a request to compile only and not try to produce an<br />

executable program.<br />

Use ls to see what files you have. You should have swapmod.mod and<br />

swapmod.o files.<br />

39

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

Saved successfully!

Ooh no, something went wrong!