Basics
We will learn by doing. Let us start by writing a simple program that asks for some user input, performs some arithmetic operations, and returns a result.
! Program : Add two integer numbers
PROGRAM add
IMPLICIT NONE
INTEGER :: a, b, output
PRINT *, "Input a ="
READ *, a
PRINT *, "Input b ="
READ *, b
output = a + b
PRINT*, "Output: ", a, "+", b, "=", output
END PROGRAM add
-
Variable names and keywords in FORTRAN are not case sensitive. You can write keywords in both lowercase or uppercase. For example,
implicit
andIMPLICIT
are the same. Older, f77 specification only allowed uppercase letters. -
PROGRAM name and file name are not related.
We store the above code in a file named: 01_add.f90
. We compile and execute
the code as follows:
gfortran 01_add.f90
./a.out
Now let's write another simple program that converts centimeter to inches:
! Program : Converting centimeter to inches
PROGRAM cm2inch
IMPLICIT NONE
REAL :: cm, inch
PRINT "(a,$)", "Enter length in centimeter = "
READ *, cm
inch = cm / 2.54
PRINT *, "Length in inches = ", inch
END PROGRAM cm2inch
Calculate the circumference of a circle given the diameter:
! Program : Calculate the circumference of a circle given the radius.
PROGRAM circ
IMPLICIT NONE
REAL :: r, circum
REAL, PARAMETER :: pi = 3.14159
WRITE(*, "(a)", advance='NO') "Enter radius of the circle: "
READ *, r
circum = 2 * pi * r
PRINT *, "Circumference of the circle = ", circum
END PROGRAM circ
Formatting output:
! Program : Formatting output in Fortran
PROGRAM format
IMPLICIT NONE
INTEGER :: no_of_students = 25
REAL :: result1, result2
result1 = SIN(1.23)
result2 = 1.234728 * EXP(12.0)
PRINT '(i4)', no_of_students ! Interger with 4 spaces for it
PRINT '(f6.4)', result1 ! Real number with 6 spaces of
! which 4 for the decimal part
PRINT '(e8.2)', result2 ! exponential with 2 spaces for
! decimal part
END PROGRAM format
Example outputs:
WRITE(*, "(I4)") 1234
1234
WRITE(*, "(I8)") 1234
1234
WRITE(*, "(I8.6)") 1234
001234
WRITE(*, "(F8.4)") pi
3.1416
WRITE(*, "(E12.4)") pi
0.3142E+01
A comment starts with !
. The continuation of a single statement to the next
line is done by &
at the end of previous line. If you need to break a very
long character string, use the concatenation operator //
.
PRINT *, "My name is " // &
"Pranab."
A single statement can be at max 256 lines long. Also, you can separate multiple
statements in a line with semicolon (;
).
Names
The names must not be longer than 63 characters, must be composed of alphanumeric characters and underscores, first character must be a letter. Names are case insensitive.
Types
- Integer (example:
-2
,0
,10
) - Real (example:
3.14
,1.2E2
,2.3D-2
.D
indicates double precision.) - Complex (example:
2.0 - 1.5i
) - Logical (
.true.
,.false.
) - Character: The length of a string can be specified by
len
.
Variables
integer :: no_of_students
real :: radius, circumference
complex :: cx
logical :: done
character(len=20) :: name
Constants
real, parameter :: pi = 3.14159
KIND type parameter
The KIND type parameters supported by GNU Fortran for the primitive data types are:
INTEGER: 1, 2, 4, 8†, 16† (default 4‡)
LOGICAL: 1, 2, 4, 8†, 16† (default 4‡)
REAL: 4, 8, 10†, 16† (default 4§)
COMPLEX: 4, 8, 10†, 16† (default 4§)
DOUBLE PRECISION: 4, 8, 10†, 16† (default 8§)
CHARACTER: 1, 4 (default 1)
† not available on all systems
‡ unless -fdefault-integer-8
is used
§ unless -fdefault-real-8
is used
The KIND
value matches the storage size in bytes, except for COMPLEX
where
the storage size is twice as much (or both real and imaginary part are a real
value of the given size). It is recommended to use the SELECTED_CHAR_KIND
,
SELECTED_INT_KIND
and SELECTED_REAL_KIND
intrinsics or the INT8
, INT16
,
INT32
, INT64
, REAL32
, REAL64
, and REAL128
parameters.
Examples of setting double precision:
REAL(KIND=KIND(0.0D0)), PARAMETER :: pi = 4 * ATAN(1.0D0)
! or specify decimal precision and exponent range
REAL(KIND=SELECTED_REAL_KIND(12, 200)), PARAMETER :: pi = 4 * ATAN(1.0D0)
PRINT *, PRECISION(pi), RANGE(pi)
! type conversion
REAL(KIND=KIND(0.0D0)) :: x
INTEGER :: y
y = 1
x = REAL(y, KIND=KIND(0.0D0))
! integer with range -10^(12) to 10^(12)
INTEGER(KIND=SELECTED_INT_KIND(12)) :: large_int
Implicit typing
In Fortran variables need not to be declared beforehand, but this practice
should be avoided by IMPLICIT NONE
at the top of program. If a variable is not
declared, the first letter of its name determines its type. If the name starts
with i, j, k, l, m, n, it is considered integer, otherwise real.
Intrinsic functions
Functions | Description |
---|---|
abs(x) | absolute value of a numerical argument |
complx(x, y) | converts to complex |
floor(x) | greatest integer ≤ x floor(3.2)=3 , floor(-4.9)=-5 |
int(x) | converts to integer. int(-4.9)=-4 , int(0.2)=0 , int(3.3)=3 |
nint(x [, ikind]) | rounds to nearest integer. Optional ikind for precision |
real(x [, ikind]) | converts to real |
mod(a, p) | a - int(a/p)*p |
modulo(a,p) | a - floor(a/p)*p |
Mathematical functions
Functions | Description |
---|---|
sin | sine function |
asin | inverse sine |
sinh | sine hyperbolic |
cos | cosine function |
acos | inverse cosine |
cosh | cos hyperbolic |
tan | tangent function |
atan | inverse tan |
atan2 | inverse tangent for complex numbers |
tanh | tan hyperbolic |
sqrt | square root |
exp | exponential |
log | natural logarithm |
log10 | common logarithm |
Reading multiple inputs
INTEGER :: M, N
PRINT '(A,$)', "Enter matrix dimensions: "
! inputs must be separated by at least one blank character or comma
READ *, M, N
Formatting output
PRINT "(f5.1, es9.1, a, i4)", 3.5, 1.4e4, " and ", 12
PRINT "(2(i4))", 12, 8 ! same as "(i4, i4)" or "(2i4)"
! line breaks
PRINT "(a, /, a)", "first line", "second line" ! two line breaks: 2/
! tab editor, second number will be printed in the position 10-13
PRINT "(i4, t10, i4)", 3, 5