Subroutines and functions
A procedure is a piece of a program with a name. Fortran has two kinds: a function gives back a value and sits inside an expression, a subroutine does its work when it is named with call and gives back nothing but the arguments it changed. This lesson writes one of each, calls them from a program, and looks at the two statements that shape a procedure body: the result clause and an early return.
A function
function square(x) result(y) declares a function called square with one dummy argument and a result called y. The body assigns y and ends; the value of y is what the function returns, and the name of the function is used in the expression where it is called.
The file funcs.f90 holds a module with the two procedures and a program that uses them:
module maths
implicit none
contains
function square(x) result(y)
real, intent(in) :: x
real :: y
y = x * x
end function square
subroutine scale(a, factor)
real, intent(inout) :: a(:)
real, intent(in) :: factor
a = a * factor
end subroutine scale
end module maths
program funcs_demo
use maths, only: square, scale
implicit none
real :: v(4)
print '(a,f6.1)', "square(3.0) = ", square(3.0)
v = [1.0, 2.0, 3.0, 4.0]
call scale(v, 2.5)
print '(a,4f7.1)', "scaled : ", v
end program funcs_demo
gfortran -Wall -Wextra -std=f2018 funcs.f90 -o funcs
./funcs
square(3.0) = 9.0
scaled : 2.5 5.0 7.5 10.0
The module is the container of the two procedures and the reason the program can call them with their interfaces known; the program units lesson has the four units and their order. scale changed the array in place: an array argument is a view of the caller data, and intent(inout) is what allows the write.
A subroutine
A subroutine has no result variable and no type. It is called as a statement, and everything it produces comes back through its arguments: an array it fills, a scalar it sets, a unit it writes to. The absence of a value is the whole difference, and it is a useful one: a procedure that changes three arrays has no single result, and forcing it into a function would mean returning one of them and pretending the others came along.
The dummy argument of scale is a(:), the assumed shape of the array lesson, so the subroutine works for an array of any length. factor and x in the function are intent(in), which promises the compiler they are not written; the next lesson is about what that promise buys.
CALL and the value
The syntax is the rule: a function name with parentheses is a value and may appear wherever a value may, including inside an arithmetic expression; a subroutine name is only ever used after call. The program below uses the same function twice inside expressions, which a subroutine cannot do.
program expr_demo
implicit none
real :: x
x = 2.0
print '(a,f8.2)', "x squared plus one : ", square(x) + 1.0
print '(a,f8.2)', "twice the square : ", 2.0 * square(x)
contains
function square(v) result(r)
real, intent(in) :: v
real :: r
r = v * v
end function square
end program expr_demo
gfortran -Wall -Wextra -std=f2018 expr.f90 -o expr
./expr
x squared plus one : 5.00
twice the square : 8.00
A procedure placed after contains inside the program is a contained procedure: it sees every variable of the program and it has an explicit interface by construction. That is the shortest way to split a small program, and the module is the way to share a procedure between programs.
The RESULT clause
result(y) names the value the function returns. Without it the function name itself is the result variable, and an assignment to the name inside the body is what returns the value; with it the name stays free to be a local variable of the same name as something else. The clause also lets a function be recursive without the name being both the result and the call, which the recursion lesson needs.
The result variable must be declared like any other, and it may be an array or a derived type. A function whose result is an array must have its shape declared in the body, and every caller then gets a value with that shape.
RETURN in the middle
return leaves the procedure at once, which is how a subroutine skips the rest of its body for an input it does not handle. In a function the same statement returns the value the result variable holds at that moment.
program early_demo
implicit none
call check(5)
call check(-5)
contains
subroutine check(n)
integer, intent(in) :: n
if (n < 0) then
print *, "negative, skipped"
return
end if
print *, "positive :", n
end subroutine check
end program early_demo
gfortran -Wall -Wextra -std=f2018 early.f90 -o early
./early
positive : 5
negative, skipped
A procedure returns at its end anyway, so the explicit statement earns its place only when it guards the rest of the body. The same job is done by an if block around the remaining statements, and a reader usually prefers the block; the early return is the shorter form when the rest of the body is long.
Which one to write
Write a function when the procedure computes one value from its arguments and changes nothing, because that is the shape an expression needs and the shape a test can check without looking at the arguments afterwards. Write a subroutine when the procedure does something: fills several arrays, writes a report, reads a record.
The distinction is not enforced by the language, and a function that writes to its arguments compiles. It is the reason the intent(in) on the arguments of a function is worth writing every time: a function with an intent(out) argument is a subroutine with an awkward call.
Exercises
- Add a function cube(x) beside square and print cube(2.0). Which name holds the result if you leave out the result clause?
- Call scale on a three-element array and on a section. Which lengths does it see?
- Change intent(inout) of scale to intent(in). What does the compiler say?
- Write the early return of check as an if block around the last print. Is the output the same?
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What is the difference between a function and a subroutine?
Answer
A function returns a value and can sit in an expression; a subroutine is called as a statement
The result value is the whole difference.
-
What does the RESULT clause of a function name?
Answer
The variable that holds the value the function returns
It names the result variable.
-
What does this program print?
program p implicit none print *, twice(3) contains function twice(n) result(r) integer, intent(in) :: n integer :: r r = 2 * n end function twice end program pAnswer
6
The function returns twice its argument.
-
How is a subroutine called?
Answer
CALL name(arguments)
A subroutine is a statement, so it needs CALL.
-
What does
containsintroduce?Answer
The procedures of the program unit that follows it
Contained procedures follow CONTAINS in a program or module.
-
What does
returndo in a subroutine?Answer
Leaves the subroutine at that point
RETURN ends the procedure execution.
-
How does a subroutine hand a result back?
Answer
Through its arguments, which it may write
A subroutine has no result variable.
-
What does
call scale(v, 2.5)do to v?Answer
Multiplies every element by 2.5 through the dummy argument
The array is passed by reference and INTENT(INOUT) allows the write.
-
Why is
intent(in)worth writing on the arguments of a function?Answer
It promises the arguments are not written, so the function is a value and nothing else
The promise is what makes a function safe to use inside an expression.
-
What does a contained procedure see?
Answer
Every variable of the program unit that contains it
A contained procedure is inside the host scope.
The programs on this page were built with GNU Fortran (Alpine 15.2.0) 15.2.0 and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.
Article author: Arthur Isaev