Basic Flow of FORTRAN (77, 90, and 95) body code and subroutines:

  1. Name your body code and subroutine;
  2. Declare variables;
  3. Write the actual code;
  4. Signal an end of your body code and subroutine.

Walkthrough of sample code : learn_f.f

Body code - myfirstprogram
Subroutine - writefile
Function - circlearea

Declaration of variables:
Format: Variable Type Var Name 1, Var Name 2 ......
Basic variable types: Character (string), real, integer, logical, complex
There are also "long format" for real and complex numbers
All string declaration: character*n myword (n = length of string)
Parameter = fixed value, cannot be changed
Declaration of arrays: Variable Type (n0:nf, m0:mf, ......)
Initialize your variables! (Otherwise variables contain garbled information)

Basic I/O to/from screen and keyboard:
Write (*,*) -> write to screen without formatting
Read (*,*) -> read keyboard input without formatting

do [label, i.e. 50] n=1,5
50 continue
Label: Used in loops and formatting.

Format statement:
Used to format text, numbers
Usage:[Label] format (format statements)
Common format statements:
fnn.m = a real number with a total of nn digits (including decimal points) with m-1 digits after decimal points inn = integer with a total nn digits
ann = string with a total nn characters
Complex numbers are "formed" by 2 real numbers (a+bi); to format them, you need a compound format statement like 2(f8.2)

Basic math operations and intrinsic (build-in) functions:
* = multiply, / = division, + = addition, - = subtraction, ** = power
The order of calculations follows basic math rules: ** > * & / < +/- unless specified by brackets "( .... )" In sample code, I used the intrinsic function sqrt() (square root) and complex () (creates a complex number). Other common functions:
sin (), cos (), tan (), exp(), real (), int(), ...
Fortran uses radians.

[var type] function functionname (variable names passed...)
Calling a function: Variable = functionname (variables passed)
The name of variables passed does not have to same as the ones specified in the functions, but the order must agree!

Subroutine subroutinename (variables passed)
Calling a subroutine: call subroutinename (variables passed)

Basic file I/O
Opening a ascii file:
open (unit=n,file=filenumber)
Closing a ascii file:
close (unit=n)
Writing to a file:
write (unit number, format statement/reference) variable names
Reading from a file (not shown in sample code):
read (unit number, format statement/reference) variable names
You may write binary files too = > Grads, Matlab, IDL...
Danger Warning:
Binary files created by PCs (Windows/Linux) do not have the same format as the ones make by UNIX machines.