Download the BYJU'S Exam Prep App for free GATE/ESE preparation videos & tests - Download the BYJU'S Exam Prep App for free GATE/ESE preparation videos & tests -

Recursive Function in C

The C programming language allows any of its functions to call itself multiple times in a program. Here, any function that happens to call itself again and again (directly or indirectly), unless the program satisfies some specific condition/subtask is called a recursive function.

In this article, we will take a look at the recursive function in C and its uses according to the GATE Syllabus for CSE (Computer Science Engineering). Read ahead to learn more.

Table of Contents

Recursion and Recursive Function in C

In C language, recursion refers to the process in which a function repeatedly calls its multiple copies when working on some very small problem. Here, the functions that keep calling themselves repeatedly in a program are known as the recursive functions, and such types of functions in which the recursive functions call themselves are known as recursive calls.

The process of recursion in the C language consists of multiple recursive calls. And, it is also a prerequisite that you choose a termination condition for the recursive function- or else, the program will be stuck in a loop.

Pseudocode for Recursive Functions in C

Let us check the pseudocode used for writing the recursive function in any code:

if (base_test)

{

return given_value;

}

else if (another_base_test)

{

return other_given_value;

}

else

{

// Giving a Statement here;

recursive call;

}

Working of a Recursive Function in C

It is simple to understand how a recursive function works in the C language. It involves certain tasks and divides them into various subtasks. Some of the subtasks consist of termination conditions/conditions. These subtasks need to satisfy these conditions to terminate the program. Else, as discussed above, a never-ending loop will be created.

Next, the process of recursion finally stops, and we then get to derive the final result from the recursive function. Here, we also have the concept of the base case. A base case refers to the case where the function doesn’t recur, and thus, we have the base case. There are various instances when the recursive function tries to perform a subtask by repeatedly calling itself. It is known as the recursive case.

Let us take a look at the format used for writing the recursive function in C.

Examples

Here, we will write a C program that prints the 10th values of the Fibonacci series in the form of output.

#include<stdio.h>

int numberfibonacci(int);

void main ()

{

int a,b;

printf(“Please enter the value of the n number here : “);

scanf(“%d”,&a);

b= numberfibonacci(a);

printf(“%d”,b);

}

int numberfibonacci(int a)

{

if (a==0)

{

return 0;

}

else if (a == 1)

{

return 1;

}

else

{

return numberfibonacci(a-1) + numberfibonacci(a-2);

}

}

The output generated here from the code mentioned above would be:

Please enter the value of the n number here : 10

55

Let us take a look at another example. We will write a program in C that finds the factorial of an available number using the recursive function and not loops.

#include<stdio.h>

#include<conio.h>

int fact(int a); /* Definition of Function */

void main()

{

int number, result;

clrscr();

printf(“Please enter a non-negative number here: “);

scanf(“%d”,&number);

result = factorial(number); /* Function Calling in a Normal way */

printf(“%d! = %d” ,number ,result);

getch();

}

int factorial(int a) /* Definition of Function */

{

int x=1;

if(a <= 0)

{

return(1);

}

else

{

x = a * factorial(a-1); /* Function Call Recursively as the fact() calls itself in the program */

return(x);

}

}

The output generated here from the code mentioned above would be: Please enter a non-negative number here: 5

5! = 120

Image 1

Pros and Cons of Using Recursive Functions in C

The recursive functions lead to a very short recursion code in the C language. These are much shorter as compared to the iterative codes- and thus, pretty confusing and tricky to understand. Therefore, we generally avoid using the recursive functions unless in need.

Every problem that occurs in C can’t be solved by recursion. This process only comes in handy when we perform certain tasks that need to be defined with similar types of subtasks. Not every problem needs to do that. For example, searching, sorting, and traversal problems become easy to solve with recursion in the C language.

The iterative solutions are much easier to understand and use, as well as efficient when compared to the process of recursion. On top of that, any function that we generally solve recursively also has the scope to be solved iteratively. Thus, iteration is more preferable. Yet, some programs are better solved by recursive functions in C, such as the Fibonacci series, Factorial finding, Tower of Hanoi, etc.

Practice Problems on Recursive Function in C

1. Take a look at the following program, and find the output for different sets of inputs:

#include<stdio.h>

int numberfibonacci(int);

void main ()

{

int a,b;

printf(“Please enter the value of the n number here : “);

scanf(“%d”,&a);

b= numberfibonacci(a);

printf(“%d”,b);

}

int numberfibonacci(int a)

{

if (a==0)

{

return 0;

}

else if (a == 1)

{

return 1;

}

else

{

return numberfibonacci(a-1) + numberfibonacci(a-2);

}

}

The output of the following inputs would be:

A. Input: 12

Answer: Please enter the value of the n number here : 12

144

B. Input: 7

Answer: Please enter the value of the n number here : 10

13

C. Input: 15

Answer: Please enter the value of the n number here : 10

610

D. Input: 20

Answer: Please enter the value of the n number here : 10

6765

2. Take a look at the following program, and find the output for different sets of inputs:

#include<stdio.h>

#include<conio.h>

int fact(int a); /* Definition of Function */

void main()

{

int number, result;

clrscr();

printf(“Please enter a non-negative number here: “);

scanf(“%d”,&number);

result = factorial(number); /* Function Calling in a Normal way */

printf(“%d! = %d” ,number ,result);

getch();

}

int factorial(int a) /* Definition of Function */

{

int x=1;

if(a <= 0)

{

return(1);

}

else

{

x = a * factorial(a-1); /* Function Call Recursively as the fact() calls itself in the program */

return(x);

}

}

The output of the following inputs would be:

A. Please enter a non-negative number here: 11

11! = 39916800

B. Please enter a non-negative number here: 6

6! = 720

C. Please enter a non-negative number here: 9

11! = 362880

D. Please enter a non-negative number here: 7

7! = 5040

FAQs

Frequently Asked Questions

Q1

How do we use recursion in C? How does it work?

Recursion divides the tasks in a program into various subtasks. Some of the subtasks consist of termination conditions. These subtasks need to satisfy these conditions to terminate the program. Else, a never-ending loop will be created.
Next, the recursion finally stops, and we get the result from the recursive function.

Q2

What is the difference between a base case and a recursive case?

If the function doesn’t recur, it is known as the base case. There are various instances when the recursive function tries to perform a subtask by repeatedly calling itself. It is known as the recursive case.

Q3

Why do we prefer iteration over recursion in any C program?

The iterative solutions are much easier to understand, and any function that we generally solve recursively has the scope to be solved iteratively.
Also, the recursive functions lead to a very short code that is pretty confusing and tricky to understand. Recursion can’t solve every problem that occurs in C. This process only comes in handy when we perform certain tasks that need to be defined with similar types of subtasks. Not every problem needs to do that.

Keep learning and stay tuned to get the latest updates on GATE Exam along with GATE Eligibility Criteria, GATE Admit Card, GATE Syllabus for CSE (Computer Science Engineering), GATE CSE Notes, GATE CSE Question Paper, and more.

Also Explore,

Comments

Leave a Comment

Your Mobile number and Email id will not be published.

*

*