#
Fortran Wiki

recursion

## Recursion

Fortran 77 did not explicitly allow recursive functions and subroutines, although there were several ways around this eg Fortran Examples. From Fortran 90 onwards, recursive functions and subroutines could be declared and used freely.

A well known recursion benchmark:

```
program ackermann
integer :: ack
write(*,*) ack(3, 12)
end program ackermann
recursive function ack(m, n) result(a)
integer, intent(in) :: m,n
integer :: a
if (m == 0) then
a=n+1
else if (n == 0) then
a=ack(m-1,1)
else
a=ack(m-1, ack(m, n-1))
end if
end function ack
```

One more working example

Compiler | Language | Total time |

gcc | C | 0.80 s |

gfortran 4.4.3 | Fortran | 2.97 s |

gfortran 4.6.0 | Fortran | 2.91 s |

g95 0.93 | Fortran | 6.19 s |

Stalin | scheme | 7.35 s |

(Note that the Fortran version can perform at the same speed as the C implementation if F2003 pass-by-value is used.)

Revised on January 28, 2014 17:57:58
by

gry?
(129.94.239.191)
(1360 characters / 0.0 pages)