A function may be called from another function or it may call itself. Recursion implies afunction calling itself. If a function call occurs inside the function itself, it is called

direct recursion

. If a function calls another function, which in turn makes a call to the first one,it is called

indirect recursion

.Each invocation of a function causes a new allocation of the variables declared inside it.The storage for auto variables is automatically allocated and freed on function entry andreturn.

Advantages of recursive functions:

•

Recursive functions are more concise and understandable.

Disadvantages of recursive functions:

•

Recursive functions are slower than the corresponding iterative functions.

•

Recursive functions are slow because of the overhead of passing arguments andreturning values.

•

Recursive functions take up more memory. Each time the function calls itself, thecurrent status of the function must be preserved (the line being executed, thevalues so far). This information is saved onto a data structure called a “stack”. If recursion goes on for a long time, we get the “Stack Overflow” error. So it isimportant to give an “end – condition” so that recursion eventually terminates.

Ackermann function

It takes two natural numbers as arguments and yields another natural number. Its valuegrows extremely quickly; even for small inputs. For example for (4,3), the values of theAckermann function are so large that they cannot be feasibly computed, and in fact their decimal expansions require more digits than there are particles in the entire visibleuniverse.

Page 2 of 3The evaluation of these functions always terminates. The recursion is bounded because ineach recursion either m decreases, or m remains the same and n decreases. Each time thatn reaches zero, m decreases, so m eventually reaches zero as well. However, when mdecreases there is no upper bound on how much n can increase.For small values of m like 1, 2, or 3, the Ackermann function grows relatively slowlywith respect to n. For m

≥

4, however, it grows much more quickly. The expansion of A(4, 3) cannot be recorded in the known physical universe.Computing A(1, n) takes linear time in n. Computing A(2, n) takes quadratic time in n.The time required for higher values of m, is much more.

Explanation

We now illustrate the expansion of the function for m = 1, and n = 2. Theevaluation of

The Ackermann function, due to its definition in terms of extremely deep recursion, can be used as a benchmark of a compiler's ability to optimize recursion. This function can beused to compare the amount of time required to evaluate this function for fixed argumentsin many different programming language implementations.