Regular versus recursive functions in Fortran

This is a short post that contrasts recursive versus regular Fortran functions and gives an example factorial $n!$ recursive function.  

Basics of a Fortran function

In Fortran, unlike many other languages, a user-defined function can only calculate a single value such as an integer, an array, or a character string. Also, the name of the function should also be the name of the value that it calculates by assigning it somewhere inside the function (see example below). Lastly, a return statement is not needed in a Fortran function unless the end of the function is always reached every time it is invoked. 

function add(num1, num2)
  ! a regular function that adds two numbers
  implicit none
  intent(in) :: num1, num2
  real :: num1, num2, add
  !the function name "add" must be assigned to the return value
  add = num1 + num2
end function add

What makes a recursive Fortran function different?

Recursive functions were added to Fortran 90 and later versions. The main syntactical difference with normal Fortran functions is the intrinsic "recursive" declaration and "result()" assignment that both go in the function declaration line. For example, using the factorial as an example:

recursive function factorial(n) result(fact)
  implicit none
  intent(in) :: n
  integer (kind=8)::  fact, n 
  if (n == 0) then
    fact = 1
    fact = n * factorial(n-1)
end function factorial

Notice the function declaration must begin with the intrinsic word "recursive".  Also, the returned value unlike in regular/non-recursive Fortran functions, is declared on the first line as well using the intrinsic "result()".  In this case the actual value that is returned is "fact" when the function "factorial" is called. Whereas in non-recursive Fortran functions the name of the function is also the name of the returned value. To use a recursive function in a program however one must still call on the function by its name but in this case the return value is distinguished from the function value. Here is a a program using the above function:

program fact
  implicit none
  integer :: n, factorial 
  print *, "Enter a positive integer: "
  read (*,*) n
  print *, 'factorial = ', factorial(n) 
end program fact

As you can see, in the main program the recursive function is called by its name just like a regular Fortran function, nothing mysterious here. There you have it, writing a recursive functions in Fortran is just about as simple as writing a regular Fortran function. 

Check out this post for basics on compiling and running a Fortran program.

Here are a couple recommended books on modern Fortran programming, the first for those interested in general knowledge of the language and the second is intended for numerical methods used in scientific computing and other fields:
Affiliate disclaimer: the above links are part of the Amazon affiliate program meaning that a small portion comes back to this site at no cost to you and helps support this content.


Popular posts from this blog