Self study guide: Fortran 95 - University of Cambridge
Self study guide: Fortran 95 - University of Cambridge
Self study guide: Fortran 95 - University of Cambridge
You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
• We want to avoid using a variable which is not given a value; this could happen if<br />
we mistyped the name <strong>of</strong> a variable in one <strong>of</strong> the expressions.<br />
Consider the following modified form <strong>of</strong> our program:<br />
program vertical<br />
!<br />
! Vertical motion under gravity<br />
!<br />
implicit none<br />
! acceleration due to gravity<br />
real, parameter :: g = 9.8<br />
! variables<br />
real :: s ! displacement<br />
real :: t ! time<br />
real :: u ! initial speed ( m / s)<br />
! set values <strong>of</strong> variables<br />
t = 6.0<br />
u = 60<br />
! calculate displacement<br />
s = u * t - g * (t**2) / 2<br />
! output results<br />
write(*,*) ’Time = ’,t,’ Displacement = ’,s<br />
end program vertical<br />
We have changed three lines and some <strong>of</strong> the comments. The line:<br />
implicit none<br />
is an important statement which says that all variables must be defined before use.<br />
You should always include this line in all programs. 1<br />
The second change is to the line:<br />
real, parameter :: g = 9.8<br />
This in fact defines g to be a constant equal to the value 9.8; an attempt to reassign g<br />
via a statement like the one in the original version (g = 9.8 on a line by itself) will<br />
now lead to an error. The syntax <strong>of</strong> this statement is as follows:<br />
After the definition <strong>of</strong> the variable type real we give a series <strong>of</strong> options<br />
separated by commas up until the ‘::’ after which we give the variable name with<br />
an optional assignment.<br />
1 It is an unfortunate legacy <strong>of</strong> older versions <strong>of</strong> <strong>Fortran</strong> that you could use variables without defining<br />
them, and in that case <strong>Fortran</strong> supplied rules to determine what the variable type was.<br />
7