You are on page 1of 17

RECURSION

Introduction
Recursion Basics. Some computer programming languages allow a module
or function to call itself. This technique is known as recursion. In recursion,
a function either calls itself directly or calls a function that in turn calls
the original function is called recursive function.
Example a function calling itself.
int function(int value)
{
if(value < 1)
return;
function(value - 1);
cout<<value;
}
Example a function that calls another function
which in turn calls it again.
int function(int value)
{
if(value < 1)
return;
function(value - 1);
cout<<value;
}
Implementation:
Function Call
A Recursive function is a function that makes a call to itself. To
prevent infinite recursion, you need an if-else statement (of
some sort) where one branch makes a recursive call, and the
other branch does not. The branch without a recursive call is
usually the base case (base cases do not make recursive calls
to the function).Functions can also be mutually recursive. For
example, function f() can call function g() and
function g() can call function f(). This is still considered
recursion because a function can eventually call itself. In this
case, f() indirectly calls itself.
Recursive methods are either Tail
recursive or Non-tail recursive.
Definition: Tail recursive method has the recursive
call as the last statement in the method. Recursive
methods that are not tail recursive are called non-
tail recursive.
// An example of tail recursive function
void cout<<int n
{
if (n < 0)
return;
cout << " " << n;

// The last executed statement is recursive


call
cout<<n-1;
}
Direct &Indirect Recursion
A method is recursive if it can call itself, either
Directly:
void f()
{ ... f() ...
}
Or
Indirectly:
void f()
{ ... g()...
}
void g()
{ ... f() ...
}
Direct and Indirect Recursion
When in the body of a method there is a call to the same method, we
say that the method is directly recursive.
If method A calls method B, method B calls method C, and method C
calls method A we call the methods A, B and C indirectly
recursive or mutually recursive.
Nested recursion
When a recursive function call has itself as the parameter, then it is
called nested recursion. One of the common examples of nested
recursion is the Ackermann function. Look at the following equation:
Backtracking
Backtracking is a technique for returning to a given position
(e.g., entry point) after trying other avenues that are unsuccessful in
solving a particular problem
Time Complexity
In case of iterations, we take number of iterations to
count the time complexity. Likewise, in case of
recursion, assuming everything is constant, we try to
figure out the number of times a recursive call is being
made. A call made to a function is (1), hence the (n)
number of times a recursive call is made makes the
recursive function (n).
Summary
Recursive definitions are programming concepts that
define themselves
Recursive definitions serve two purposes: Generating
new elements Testing whether an element belongs to a set
Recursive definitions are frequently used to define
functions and sequences of numbers
Summary (continued)
Tail recursion is characterized by the use of only one
recursive call at the very end of a method implementation.
Backtracking is a technique for returning to a given position
(e.g., entry point) after trying other avenues that are
unsuccessful in solving a particular problem.
The process of translating one executable statement at a
time and immediately executing it is called interpretation.

You might also like