Named constants in Fortran
parameter makes a constant the compiler will defend, from pi to the crew count.
program parameters_constants
implicit none
! parameter makes a named constant; the compiler holds you to it.
real, parameter :: pi = 3.141593
real, parameter :: earth_radius_km = 6371.0
integer, parameter :: crew = 12
print *, "Equator, roughly: ", 2.0 * pi * earth_radius_km, " km"
print *, "Walking shifts of ", crew, " keepers"
end program parameters_constants
How it works
real, parameter :: pi = 3.141593fixes the value at compile time.- Assigning to a
parameterlater is a compile error, not a surprise. - The circumference expression uses the constants like any other value.
Keywords and builtins used here
endimplicitintegernoneparameterprintprogramreal
The run, in numbers
- Lines
- 10
- Characters to type
- 364
- Tokens
- 49
- Three-star pace
- 70 tpm
At the three-star pace of 70 tokens a minute, this run takes about 42 seconds.
Step 3 of 3 in Programs & variables, step 3 of 27 in Language basics.