Tutorial by Examples

Functions can be written using several types of syntax function name() integer name name = 42 end function integer function name() name = 42 end function function name() result(res) integer res res = 42 end function Functions return values through a function result....
The return statement can be used to exit function and subroutine. Unlike many other programming languages it is not used to set the return value. real function f(x) real, intent(in) :: x integer :: i f = x do i = 1, 10 f = sqrt(f) - 1.0 if (f < 0) then f = -1...
In Fortran functions and subroutines need to be explicitly declared as recursive, if they are to call themselves again, directly or indirectly. Thus, a recursive implementation of the Fibonacci series could look like this: recursive function fibonacci(term) result(fibo) integer, intent(in) :: te...
The intent attribute of a dummy argument in a subroutine or function declares its intended use. The syntax is either one of intent(IN) intent(OUT) intent(INOUT) For example, consider this function: real function f(x) real, intent(IN) :: x f = x*x end function The intent(IN) specif...
For a function or subroutine to be useful it has to be referenced. A subroutine is referenced in a call statement call sub(...) and a function within an expression. Unlike in many other languages, an expression does not form a complete statement, so a function reference is often seen in an ass...

Page 1 of 1