Sie sind auf Seite 1von 16

5/19/17 Types of Recursion in C.

Tail Linear Binary Multiple
Programming Tutorials and Interview Questions

Custom Search

C Programming
Java Programming
Data Structures
Web Development
Tech Interview

What is Recursion? Types of Recursion

Recursion in C Language
Recursion in C - A Working
How does Recursion Work
in C Program?
Types of Recursion
Linear Recursion
Tail Recursion
Binary Recursion
Multiple Recursion
How to Trace Recursive
Pros and Cons of Recursion
Conclusion - Recursion
Recursion in C Language
Recursion in C or in any other programming language is a programming technique
where a function calls itself certain number of times. A function which calls itself is
called a recursive function, the call is recursive call and the process of function
implementation is recursion.

Formally, Recursion is a programming technique that comes from recurrence

relation, where the problem is divided further in sub problems smaller in size but
same in nature. This division stops when the problem cannot be divided further. This 1/16
5/19/17 Types of Recursion in C. Tail Linear Binary Multiple

point is called base case from where control moves back towards the original problem
by solving sub problems and assembling results in order to get the final result.

Recursion in C - A Working Example

While studying recursion in C, if you are asked to write a function void printBin(int
n), which takes a decimal integer as an argument, and prints it's binary form on
screen. You can think of two implementations of this function. First version is an
iterative one that stores binary digits in an array as they are produced then prints
the array content in reverse order. The second one is a recursive version void
recPrintBin(int n). Recursive version recPrintBin calls itself by passing n/2 to
the subsequent calls until n is either 1 or zero, then start printing n%2 from the stack
of calls, and solves the problem recursively.

Below piece of code is the iterative version ( void Ads by Google

printBin(int n)) of decimal to binary conversion. In Number Trace

this implementation we first get the number converted
in 1's and 0's but not in right order, so to print the Call Trace
string of binary digits in correct order string binS first
needs to be reversed, and then printed.

File: dec_to_bin.c
Program: decimal to binary conversion
of positive integer (iterative version)
#include <stdio.h>
#include <string.h>

void printBin (int);

void reverse(char *);

int main ()
printf("Binary of 19 is: ");
void printBin (int n)
char binS[256]; //contains binary digits.
int i = 0;
binS[i++] = n % 2 + '0';
} while (n/=2);

binS[i] = '\0'; //null at the end of string

reverse (binS); //reverse the order of 1's and 0's 2/16
5/19/17 Types of Recursion in C. Tail Linear Binary Multiple

printf ("%s\n", binS);


/* helper function to reverse a string */

void reverse (char *s)
int lastIndex = strlen(s) - 1;
int firstIndex = 0;
char ch;
while (firstIndex < lastIndex)
ch = s[firstIndex];
s[firstIndex] = s[lastIndex];
s[lastIndex] = ch;

Binary of 19 is: 10011

Now, see the recursive version of the same problem.

Demonstration of Recursion in C
File: rec_dec_to_bin.c
Program: decimal to binary conversion
of positive integer (recursive version)
#include <stdio.h>
#include <string.h>

void recPrintBin (int);

int main ()
int n;
printf("Enter an integer: ");
scanf("%d", &n);
printf("Recursively computed binary of %d is: ", n);

void recPrintBin (int n)

if (n == 1 || n == 0) //base case
printf ("%d", n);
recPrintBin (n/2); //recursive call
printf("%d", n%2);
5/19/17 Types of Recursion in C. Tail Linear Binary Multiple
Enter an integer: 19
Recursively computed binary of 19 is: 10011

From above pieces of codes, it can be clearly seen that the second piece of code is
more concise and cleaner. However, application of recursion is completely problem
dependent and it may not be suitable for all problem types.

If you analyze decimal to binary conversion problem, Ads by Google

you will find that in the first solution (iterative one),
Linear Programming
the binary-digit generated in the last iteration should
be printed first. That's the reason character array Programming Code
binS needed to be reversed before printing on console.

On the contrary, the recursive version does not need any reverse of binary digits
because in recursion, the very last call to function is processed very first, then second
last and so on. This is done with help of a stack of calls, where the last call to function
is placed on top of the stack, therefore processed first. That's how recursion in C is
tackled and processed.

There are some classical examples which can be better implemented using recursion
rather than their iterative counterparts. Towers of Hanoi, recursive listing of
directories or folders and Russian Matryoshka dolls are some of them.

How does Recursion Work in C Program?

A recursive function has following two parts:
1. The base case
2. Recursive call
Base case is the case for which the function is evaluated without recursion. In above
example the base case is if(n == 1 || n == 0), which prints value of n without
making any subsequent recursive call. Recursive call is the central part of a recursive
function and during each call parameter values come closer to the base case. And,
when the base case is reached it is evaluated without recursion and results are
returned to the previous call, then from previous to previous and so on, until the
function reaches to its caller.

Types of Recursion
A function is recursive if it makes a call to itself directly or indirectly. If a function
f() calls itself within from its own body, it is called recursive. Secondly, if a function

f() calls another function g() that ultimately calls back to f(), then it can also be 4/16
5/19/17 Types of Recursion in C. Tail Linear Binary Multiple

considered a recursive function. Following variants of recursion tell us making

recursive calls in different ways depending upon the problem.

Linear Recursion
In linear recursion a function calls exactly once to itself each time the function is
invoked, and grows linearly in proportion to the size of the problem. Finding
maximum among an array of integers could be a good example of linear recursion.
The same is demonstrated below:

Let's assume that we are given an array arr of n decimal integers and we have to
find the maximum element from the given array. We can solve this problem using
linear recursion by observing that the maximum among all integers in arr is arr[0],
if n==1, or the maximum of first n-1 in arr and the last element in arr.

File: rec_max_array.c
Program: Find maximum among array elements

#include <stdio.h>
#define SIZE 10

int recursiveMax (int *, int );

int max (int, int);

int main ()
int arr[SIZE] = {1, 3, 5, 4, 7, 19, 6, 11, 10, 2};
int max = recursiveMax(arr, SIZE);
printf("Maximum element among array items is: %d\n", max);

int recursiveMax (int *arr, int n)

if (n == 1)
return arr[0];

return max (recursiveMax (arr, n-1), arr[n-1]);


/* helper function to compute max of two decimal integers */

int max (int n, int m)
if (n > m) return n;
return m;

Maximum element among array items is: 19 5/16
5/19/17 Types of Recursion in C. Tail Linear Binary Multiple

Tail Recursion
Tail Recursion is another form of linear recursion, where the function makes a
recursive call as its very last operation. Note that, there is a difference between last
operation and last statement. If you see at recursiveMax (defined above), then you
will find that recursive call to function recursiveMax is last statement but not last
operation, code arr[n-1] yet to be processed to complete the execution of last

Therefore, a function will be said tail recursive if the Ads by Google

recursive call is the last thing the function performs.
Software Type
When a function uses tail recursion, it can be converted
to the non-recursive one, by iterating through the Coding Java
recursive calls rather than calling them explicitly.
Reversing array elements could be a good example of tail recursion. The recursive
solution of reversing array elements problem is illustrated as below, followed by it's
iterative version.

Suppose, we are given an array of decimal integers, and we have to reverse the order
of elements in which they are stored. We can solve this problem by using linear
recursion. Reversal of an array can be achieved by swapping the first and last
elements and then recursively reversing the remaining elements in the array.

File: rec_array_reverse.c
Program: Reverse array elements recursively.

#include <stdio.h>
#define SIZE 10

void recursiveRev (int *, int, int);

int main ()
int arr[SIZE] = {1, 2, 3, 4, 5, 6, 7, 8, 9 , 10};
int i;
recursiveRev (arr, 0, SIZE-1);

printf ("Printing array elements after reversing them...\n");

for (i = 0; i < SIZE; i++)
printf ("%d\n", arr[i]);

void recursiveRev (int * arr, int i, int j)

if(i < j)
{ 6/16
5/19/17 Types of Recursion in C. Tail Linear Binary Multiple
int tmp;
tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;

recursiveRev (arr, i+1, j-1);


Printing array elements after reversing them...

Recursive version of reversing array elements problem can easily be converted into
iterative one as follows:

void iterativeRev (int *arr, int n)

int i = 0, j = n - 1;
int tmp;

while (i < j)
tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;


But, if you look at the signatures of recursiveRev and iterativeRev you will find a
little difference there, both functions are passed different number of arguments. It is
so, because to facilitate recursion sometimes the problem may need to be redefined.

Binary Recursion
As name suggests, in binary recursion a function makes two recursive calls to itself
when invoked, it uses binary recursion. Fibonacci series is a very nice example to
demonstrate binary recursion. See the example below: 7/16
5/19/17 Types of Recursion in C. Tail Linear Binary Multiple

fib (1) = fib (2) = 1

fib (n) = fib (n-1) + fib (n-2), if n > 2

By definition, the first two numbers in the Fibonacci sequence are 0 and 1, and each
subsequent number is the sum of the previous two [Definition Source wikipedia].

File: bin_rec_fib.c
Program: Computes n-th fibonacci number using
binary recursion.

#include <stdio.h>

int recursiveFib (int);

int main ()
int n = 6;
printf ("%dth fibonacci number is %d\n", n, recursiveFib(n));

int recursiveFib (int n)

// base case
if (n <= 1)
return n;

// binary recursive call

return recursiveFib(n-1) + recursiveFib (n - 2);

6th fibonacci number is 8

Above piece of code exercises binary recursion in order to compute 6th fibonacci
number. In this piece of code recursiveFib returns n in case n is less than or equal
to 1. This is the base condition in recursiveFib from where recursive call returns to
previous call. Following figure shows how recursiveFib is executed.

Multiple Recursion
Multiple recursion can be treated a generalized form of binary recursion. When a
function makes multiple recursive calls possibly more than two, it is called multiple

How to Trace Recursive Calls?

When a function is called recursively, each call gets a fresh copy of all local/automatic 8/16
5/19/17 Types of Recursion in C. Tail Linear Binary Multiple

variables. And every call is saved in a stack, where the last most call will be
processed first. Every program maintains a stack (a special area of memory) during
run time to remember where to go back when a function is called.
You can trace a recursive function by pushing every call it makes to a stack and then
come to the previous call when it returns. Look at the following piece of code and try
to trace it; we will do in a moment's time.

#include <stdio.h>

int mystery (int a, int b)

if (b == 0) return 0;
if (b % 2 == 0) return mystery (a+a, b/2);
return mystery (a+a, b/2) + a;

int main()
int i = 4, j = 5;
printf("mystery of %d and %d is: %d\n", i, j, mystery (4, 5));

mystery of 4 and 5 is: 20

In below figure, I represent each call with a concentric box, where inner boxes will
represent the deeper level of recursion. As you see, in the innermost box recursion
terminates and no further recursive call is made. And, it starts returning to parent
calls ladder by ladder. 9/16
5/19/17 Types of Recursion in C. Tail Linear Binary Multiple

Pros and Cons of Recursion

The main advantage recursion provides to programmers is that it takes less code to
write comparative to iterative version. The code written with help of recursion is
more concise, cleaner and easier to understand.

There are some data structures you will see which are quite easy to code with help of
recursion. Tree traversals, quick, and merge sort are a few examples.

Despite above benefits, recursion provides no storage saving, nor time. As we have
discussed that every recursive call is saved in a special memory area the program
maintains (i.e., stack). Pushing a call frame to stack and removing it back is time 10/16
5/19/17 Types of Recursion in C. Tail Linear Binary Multiple

consuming relative to the record keeping required by iterative solution. There may
be chances your recursive solution results into Stack overflow, in case the number of
recursive calls does not fit into the stack.

Conclusion - Recursion
Hope you have enjoyed reading Recursion in C. Having seen at pros and cons of
recursion, you would have realized that recursion is programmer friendly, not
system because recursive version of a function is shorter to write, cleaner to see, and
easier to maintain. And so, as it is said "anyone can write code that computers can
understand, wise programmers write code that humans can understand." So keep
coding with recursion. And yes, how long do you think the following program will take
to finish its execution?

#include <stdio.h>

long guessIt(int num)

if (num == 0) return 0;
if (num == 1) return 1;
return guessIt(num-1) + guessIt(num-2);

int main()
printf("num : guessIt(num)\n");
for (int num = 0; num < 100; num++)
printf("\n%3d: %d", num, guessIt(num));

Last Word
In this tutorial we talked of recursion in C language and how does recursion work?
Then types of recursion (linear, tail, binary, and multiple recursion), tracing
recursive calls, and pros and cons of recursion. Hope you have enjoyed reading this
tutorial. Please do write us if you have any suggestion/comment or come across any
error on this page. Thanks for reading!

1. Randal E. Bryant, David R. O'Hallaron, Computer Systems: A Programmer's Perspective
2. Introduction to programming in Java
3. Data Structures and Algorithms in Java (4th edition) 11/16
5/19/17 Types of Recursion in C. Tail Linear Binary Multiple 12/16
5/19/17 Types of Recursion in C. Tail Linear Binary Multiple

3 Comments Sort by Oldest

Add a comment...

Priya Singh G L Bajaj Institute of Technology & Management,Greater Noida.

in Binary Recursion can please explain how this block is working???
int binary Fib (int);
int main ()
{ int n = 10;
printf ("%dth fibonacci number is %d\n", n, binaryFib(n));
int binaryFib (int n)
{ // base case
if (n <= 1) return n;
// binary recursive call... See More
Like Reply 1 Aug 7, 2014 11:35am

Rajendra Chaudhary Gauhati

For a given positive number n and a fix positive value k, write a

recursive function fun(n) to print the values 2kn, ...., 22n, 21n, 20n.

After printing the values in decreasing order, it prints same

numbers again in reverse order.

Like Reply Feb 16, 2015 9:07am

Saifullah Mashal Haripur, Pakistan

Hi add me in your page
Like Reply Oct 29, 2015 6:57pm

Facebook Comments Plugin 13/16
5/19/17 Types of Recursion in C. Tail Linear Binary Multiple

1 Login

Recommend 1 Share Sort by Oldest

Start the discussion

Be the first to comment.


Why main is public static void in Java Create Custom 404 Error Page in PHP
| Will Java program compile, if main and Apache Using .htaccess
5 comments 3 years ago 2 comments 3 years ago
Has s an Very Good Explanation Manis h thanks. very clear

Java Operators: Precedence and Difference Between Malloc and Calloc

Associativity in C with Example
2 comments 2 years ago 4 comments 2 years ago
Tec hnoc rat Guy I think the A vinas hs ingh Chauhan good
associativity of Pre/Post fix operators share......informative
is from (Left to right).example:: int

Subscribe d Add Disqus to your siteAdd DisqusAdd Privacy

Get Free Tutorials by Email



73 1 14/16
5/19/17 Types of Recursion in C. Tail Linear Binary Multiple

About the Author

Krishan Kumar is the
main author for cs- He is a
software professional (post
graduated from BITS-Pilani) and loves
writing technical articles on
programming and data structures. 15/16
5/19/17 Types of Recursion in C. Tail Linear Binary Multiple

Today's Tech News

BBC fools HSBC voice recognition
security system
Posted on Thursday May 18, 2017

A reporter's twin foiled the HSBC

security measure by mimicking his
brother's voice.

General election 2017: Illegal

content sanctions threat
Posted on Friday May 19, 2017

Conservative manifesto suggests digital

providers should pay to help combat
online crime.

Text-to-switch plan for mobile

Posted on Friday May 19, 2017

Ofcom wants to make it easier for people

to switch operator - but it has ruled out
a simpler option.

Courtesy BBC News

Home Contact Us About Us Write For Us RSS Feed

copyright 2016 16/16