Fortran Procedures - Functions and Subroutines Return statement


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 = -1000.
    end if

  end do
end function

This function performs an iterative computation. If the value of f becomes negative the function returns value -1000.