Show Menu
Cheatography

Fortran 90 Cheat Sheet (DRAFT) by

Short modern Fortran cheat sheet.

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Boiler plate program

program name
    implicit none
    ! module imports go here
    ! local variables go here 
    ! code goes here, there is not main entry point.
end program

Module

module name
    implicit none
    use other_module ! import another module
    ! local variables go here 
    contains
    ! functions and subroutines goes here
end module

Function

! return type goes before the function
integer function f()
    f = 42  ! the function name is result variable
end function

Subroutine

! subroutines does not return a value
! but they have out parameters
subroutine sub(a, b, c)
    integer, intent(in) :: a
    integer, intent(in) :: b
    integer, intent(out) :: c

    c = a + b
end subroutine

Loops

do while (logical expr)
   ! Control:
   ! exit for break
   ! cycle for continue
end do

label: do i = start, stop ! , step ! (optional)
    ! statements
end do

Conditions

if (cond) then
    ! ...
else if (cond) then
    ! ...
else
    ! ...
end if
 

Operators

+ -
addition, substr­action
* /
multip­lic­ation, division
**
expone­nti­ation
==
equality (numbers)
/=
inequality (numbers)
>, <
greate­r/less than
>=, <=
greate­r/less or equal
.eqv.
equality (booleans)
.neqv.
inequality (booleans)
.and., .or., .not.
logical and, or and not.

Native functions

abs(v)
absolute value of v
aimag(z)
imaginary part of z (single prec)
int(v, kind)
truncates toward zero to convert to integer
ceiling(v, kind)
ceiling and convert
floor(v, kind)
floor and convert
modulo(a, p)
polymo­rphic modulo (sign of p)
cmplx(x, y, kind)
make a complex from floats
conjg(z)
conjugate complex
cos(x), dcos(x), ccos(x)
cosine
sin(x), dsin(x), csin(x)
sine
acos(x), dacos(x)
inverse cosine
asin(x), dasin(x)
inverse sine
dprod(x, y)
x * y as a double
exp(x), dexp(x), cexp(x)
expone­ntial
erf(x), derf(x)
error function
erfc(x), derfc(x)
comple­mentary error function
hypot(a, b)
hypoth­enuse of x and y (single prec)
log(x), log10(x)
natural and base 10 logarithm
max(a, b, ...), min(a, b, ...)
polymo­rphic extrema
sum(arr), produc­t(arr), minval­(arr), maxval­(arr), all(arr), any(arr)
polymo­rphic reduction of array
sum(arr, dim), produc­t(arr, dim), minval­(arr, dim), maxval­(arr, dim), all(arr, dim), any(arr, dim)
polymo­rphic reduction of array along axis dim
minloc­(arr), maxloc­(arr)
the coordi­anates of an extrema as a 1D array
minloc­(arr, dim), maxloc­(arr, dim)
the indices of extrema in along axis dim
 

Data types

logical
boolean
intege­r(k­ind=1)
8 bits signed integer
intege­r(k­ind=4)
32 bits signed integer
intege­r(k­ind=8)
64 bits signed integer
real(k­ind=4)
simple precision float
real(k­ind=8)
double precision float
comple­x(k­ind=4)
simple precision complex number
comple­x(k­ind=8)
double precision complex number
charac­ter­(len=n)
string of size n
integer, dimens­ion(m, n)
2D array of shape (m, n)

Derived types

type :: name
  integer :: i
  real :: x
  ! more fields
end type

type(some_type) :: obj
! Initialize
obj%field_a = 1
obj%field_b = 0.5