typestar

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

  1. real, parameter :: pi = 3.141593 fixes the value at compile time.
  2. Assigning to a parameter later is a compile error, not a surprise.
  3. The circumference expression uses the constants like any other value.

Keywords and builtins used here

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.

Type this snippet

Step 3 of 3 in Programs & variables, step 3 of 27 in Language basics.

← Previous Next →