Recursive Function in C: Pseudocode, Working, Pros and Cons

Jasmine Grover logo

Jasmine Grover Content Strategy Manager

Content Strategy Manager

Recursive functions in the C programming language allow functions to call themselves multiple times within a program. These functions are termed recursive due to their capability to call themselves (directly or indirectly) over and over until a specific condition or subtask is satisfied. The recursive function in C and its uses is important for the GATE Syllabus for CSE (Computer Science Engineering).

  • For tasks like calculating factorials, generating Fibonacci sequences, and solving the Tower of Hanoi problem, recursive functions are essential tools.
  • Recursive functions, when applied correctly, become a valuable asset for solving diverse programming challenges.

Key Terms: Recursion, Recursion Function, Recursive Function, Pseudocode, Working, Fibonacci Sequence, Tower of Hanoi, Factorial Finding


Recursion and Recursive Function in C 

[Click Here for Sample Questions]

Recursion is the process in which a function repeatedly calls its many copies when tackling a relatively tiny problem in the C programming language. Here, recursive functions are those that continually invoke themselves within a program, and recursive calls are those kinds of functions where recursive functions invoke themselves.

  • In the C programming language, recursion is accomplished through several recursive calls.
  • Furthermore, the recursive function must have a termination condition; otherwise, the program will get trapped in a loop.

Pseudocode for Recursion Function in C

[Click Here for Sample Questions]

Check the pseudocode below that is used to write recursive functions in any code:

if (base_test)

{

return given_value;

}

else if (another_base_test)

{

return other_given_value;

}

// Giving a Statement here;

}


Working of a Recursive Function in C 

[Click Here for Sample Questions]

Recursive functions in the C programming language are simple to understand. It comprises particular responsibilities that are divided into numerous smaller jobs.

Termination conditions exist in some of the subtasks. These subtasks have to fulfill these conditions for the program to be finished. Otherwise, a never-ending cycle will result, as was already indicated.

  • Here, the basic case concept is also present.
  • A base case is a scenario in which the function doesn't recur, in which case the base case exists.
  • The recursive function may repeatedly invoke itself to try to finish a subtask. It is referred to as the recursive case. 

Examples

Here is a C program that generates output using the Fibonacci sequence' tenth values:

#include<stdio.h>

int numberfibonacci(int); 

void main ()

{

int a,b;

printf (“Kindly input the value of the variable 'n' 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 result produced in this case by the aforementioned code would be:

Kindly input the value of the variable 'n' here:

55

Have a look at another illustration. Here is a C program that uses recursive functions rather than loops to compute the factorial of a given number.

#include<stdio.h>

#include<conio.h>

int fact(int a); /* Function Prototype */

void main (){

int number, result;

cIrscr();

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

scanf(“%d”,&number);

result = factorial(number); /* Direct Function Invocation */

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

getch();

}

int factorial(int a) /* Function Prototype */

{

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 produced in this case by the aforementioned code would be: Enter a positive number only here: 5

5! = 120

Also Read:


Pros and Cons of Using Recursive Functions in C

[Click Here for Sample Questions]

The C language's recursive functions result in an extremely compact recursion code. These are substantially shorter than iterative codes, making them more difficult to grasp and making them confusing. As a result, recursive functions are rarely used unless necessary.

  • Recursion cannot be used to fix every issue in C.
  • This procedure is only useful when carrying out certain jobs that must be broken down into comparable subtasks. Not every issue has to go there.
  • Recursion in the C language, for instance, makes it simple to tackle problems involving traversal, sorting, and searching.
  • When compared to the recursive method, iterative solutions are far more effective and simpler to learn and utilize.
  • Additionally, every function that is typically solved recursively has the potential to be solved iteratively as well. Iteration is therefore preferred.
  • However, some programs, such as the Tower of Hanoi, the Fibonacci sequence, and the Factorial finding, are better solved using recursive functions in C.

Practice Problems on Recursive Function in C

[Click Here for Sample Questions]

Here are some Practice Problems on Recursive Function in C:

  1. Find the result of the following program for different inputs:

#include<stdio.h>

int numberfibonacci(int);

void main()

{

int a,b; 

printf("Kindly input the value of the variable 'n' 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: 

  1. Input 5 

Answer: Kindly input the value of the variable 'n' here: 5 

5

  1. Input: 8

Answer: Kindly input the value of the variable 'n' here: 8 

21 

  1. Input: 10

Answer: Kindly input the value of the variable 'n' here: 10

55

  1. Input: 15

Answer: Kindly input the value of the variable 'n' here: 15

610

  1. Find the output for several sets of inputs using the following program:

#include<stdio.h>

#include<conio.h>

int fact(int a); /* Function Prototype */

void main()

{

int number, result;

cIrscr();

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

scanf(“%d”,&number);

result = factorial(number); /* Direct Function Invocation */

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

getch();

}

int factorial(int a) /* Function Prototype */

{

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:

  1. Please input a non-negative number here: 11

11! = 39916800

  1. Please input a non-negative number here: 6

6! = 720

  1. Please input a non-negative number here: 9

11! - 362880

  1. Please input a non-negative number here: 7 

7! = 5040

Also Read:


Things to Remember

  • Recursive functions in C are defined as functions that call themselves repeatedly (directly or indirectly) to solve a relatively small problem.
  • The structure of a recursive function typically involves a base case, which is the scenario where the function doesn't recur, and a recursive case, where the function invokes itself to solve a subtask.
  • Recursive functions can be used to tackle problems involving traversal, sorting, and searching.
  • Iteration is generally preferred over recursion in C programs as it is more efficient and easier to comprehend for most tasks.
  • Some specific problems like the Tower of Hanoi, Fibonacci sequence, and Factorial finding are better solved using recursive functions in C.
  • Recursion is useful when problems can be broken down into comparable subtasks, and there are termination conditions for these subtasks.
  • Base cases in recursive functions serve as stopping conditions, ensuring the function eventually terminates.
  • Recursive cases involve invoking the function within itself, breaking the problem into smaller subproblems until the base case is reached.

Sample Questions

Ques. How can recursion be used in C? How does it function? (2 Marks)

Ans. The tasks of a program are broken down into different subtasks through recursion. There are termination requirements in some of the subtasks. To end the program, these subtasks must meet these requirements. Otherwise, a loop that never ends will be produced.

The recursive function's output is then obtained once the recursion finally comes to an end.

Ques. What distinguishes a base case from a recursive case? (2 Marks)

Ans. The base case of a function is when it doesn't recur. The recursive function may attempt to complete a subtask on numerous occasions by continually invoking itself. The recursive case is the name given to it.

Ques. Why is iteration preferred over recursion in C programs? (2 Marks)

Ans. The iterative solutions are significantly simpler to comprehend, and they can be used to solve any function that is typically solved recursively.

Additionally, the use of recursive functions results in a short piece of code that is difficult to read and understand. Not all C issues can be resolved through recursion. This procedure is only useful when carrying out certain jobs that must be broken down into comparable subtasks. Not every issue has to go there.

Ques. What is recursion in the context of programming, specifically in C? (1 Mark)

Ans. Recursion is the process in which a function calls itself repeatedly to solve a relatively small problem in the C programming language.

Ques. What must be included in every recursive function to ensure it terminates properly? (1 Mark)

Ans. Every recursive function must have a termination condition (base case) to prevent it from getting stuck in an infinite loop.

Ques. What are the two main components of a recursive function structure in C? (2 Marks)

Ans. The two main components of a recursive function structure are the base case, which is the scenario where the function doesn't recur, and the recursive case, where the function invokes itself to solve a subtask.

Ques. Why is iteration preferred over recursion in most C programs? (1 Mark)

Ans. Iteration is preferred over recursion because iterative solutions are generally more efficient, easier to understand, and can be used to solve any function that is typically solved recursively.

Ques. Give an example of a problem that is better solved using recursive functions in C. (1 Mark)

Ans. The Fibonacci sequence calculation is a classic example of a problem better solved using recursive functions in C.

Ques. Explain the concept of the base case and its significance in recursive functions. (4 Marks)

Ans. The base case is a crucial component of recursive functions in C programming. It is a scenario in which the function does not recur and acts as a stopping condition for the recursion process. When a recursive function encounters the base case, it stops invoking itself and starts returning results back to the previous function calls in the call stack.

The base case is significant because it ensures that the recursive function eventually terminates and prevents infinite recursion. Without a base case, the function would keep calling itself indefinitely, leading to a stack overflow and crashing the program.

In essence, the base case defines the simplest problem that the recursive function can solve directly without further recursion. By handling this base case explicitly, the function gradually breaks down the original complex problem into smaller subproblems, which are solved recursively until the base case is reached.

A well-defined base case is essential for the proper functioning of recursive functions, providing the necessary stopping condition and enabling the function to produce correct results.

For Latest Updates on Upcoming Board Exams, Click Here: https://t.me/class_10_12_board_updates


Check-Out: 

Comments


No Comments To Show