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, substraction |
* / |
multiplication, division |
** |
exponentiation |
== |
equality (numbers) |
/= |
inequality (numbers) |
>, < |
greater/less than |
>=, <= |
greater/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) |
polymorphic 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) |
exponential |
erf(x), derf(x) |
error function |
erfc(x), derfc(x) |
complementary error function |
hypot(a, b) |
hypothenuse of x and y (single prec) |
log(x), log10(x) |
natural and base 10 logarithm |
max(a, b, ...), min(a, b, ...) |
polymorphic extrema |
sum(arr), product(arr), minval(arr), maxval(arr), all(arr), any(arr) |
polymorphic reduction of array |
sum(arr, dim), product(arr, dim), minval(arr, dim), maxval(arr, dim), all(arr, dim), any(arr, dim) |
polymorphic reduction of array along axis dim |
minloc(arr), maxloc(arr) |
the coordianates of an extrema as a 1D array |
minloc(arr, dim), maxloc(arr, dim) |
the indices of extrema in along axis dim |
|
|
Data types
|
boolean |
|
8 bits signed integer |
|
32 bits signed integer |
|
64 bits signed integer |
|
simple precision float |
|
double precision float |
|
simple precision complex number |
|
double precision complex number |
|
string of size n |
integer, dimension(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
|
|