022. Functions should be pure and have intent(in) arguments#

topic: Procedures

intent(in) arguments cannot be changed. pure = no side effects. Use subroutine instead for a procedure with side effects.

func.f90 | | Godbolt Compiler Explorer logo | Fortran logo#
print *, area(3., 4.)  ! 12

contains

real pure function area(length, width)
   real, intent(in) :: length, width
   area = length*width
end function area

end
Output1#
   12.0000000    

real in the function definition line indicates the type of the return value. By default, the value corresponding to a variable with the same name as the function is returned (here area).



1

Compiled using GNU Fortran (Ubuntu 11.3.0-1ubuntu1~22.04) 11.3.0 with no flags