function
Try it yourself →Subroutine that returns a value.
Syntax
function name(x) name = ... end function
Description
The function name carries the return value (name = result). Defined after the contains block within the program.
Example
program h
print *, square(5)
contains
integer function square(x)
integer :: x
square = x * x
end function square
end program hExpected output
25
When to use?
Preventing code duplication, calculation units.
Common mistakes
- ⚠️ The return value is assigned to the function's NAME: square = x*x.