Implementing Recursive Function in C : A Step-by-Step Guide for Factorial Calculation


In C programming, a recursion function is a function that calls itself. A function that calls itself is known as a recursive function. Recursion is a technique in which a function calls itself, either directly or indirectly, in order to solve a problem. Recursive functions are used to solve problems that can be broken down into smaller, identical sub-problems

The basic syntax of a recursive function in C is as follows:

    Syntax :
           return_type function_name ( parameters )
           {
                // Base case if (base_case_condition)
                {
                      return base_case_result ;
                }
                // Recursive case
                else
                {
                      return function_name ( modified_parameters ) ;
                }
           }

Here, the return_type specifies the data type of the value that the function will return, the function_name is the name of the function, and the parameters are the input values that the function takes. The base_case_condition is a test that is used to check if the problem has been solved and the base_case_result is the value that is returned when the problem has been solved. In the recursive case, the function calls itself with modified_parameters in order to solve a smaller sub-problem. Here is an example of a recursive function that calculates the factorial of a given number:

  • This program is an example of a recursive function in C that calculates the factorial of a given number. The function, called factorial, takes an integer argument i and calculates the factorial of that number using recursion.
  • The function first checks if the value of i is less than or equal to 1, if it is the function returns 1. This is the base case of the recursion. If i is greater than 1, the function returns the value of i multiplied by the factorial of i-1.
  • This is the recursive case, the function calls itself with an argument of i-1 and the result is used to calculate the factorial of the original argument i. This recursive call of the function continues until the base case is met, which will be when i becomes less than or equal to 1.
  • The main function calls the factorial function with an argument of 5 and the result is printed to the console.
  • This program demonstrates how a recursive function can be used to solve a problem, in this case, how to calculate the factorial of a number. It also shows how the base case and the recursive case work together to solve the problem.

Source Code

//Recursion Function in C Programming
#include<stdio.h>
/*
    5!
    1*2*3*4*5
*/
 
int factorial(int i) //5
{
    if(i<=1){
        return 1;
    }
    return i*factorial(i-1);  //5*4*3*2*1
}
 
int main()
{
    printf("Factorial : %d",factorial(5));
    return 0;
}
 
To download raw file Click Here

Output

Factorial : 120

List of Programs


Sample Programs


Switch Case in C


Conditional Operators in C


Goto Statement in C


While Loop Example Programs


Looping Statements in C

For Loop Example Programs


Array Examples in C

One Dimensional Array


Two Dimensional Array in C


String Example Programs in C


Functions Example Programs in C