C Programming

How to Use Recursive Function in C

When it comes to solving problems in computer programming, there are many techniques available. One of these is recursion, which is a process that involves calling a function within itself.

This article will explore how to implement recursive functions in the C programming language. We will discuss the basic syntax and structure of recursive functions, as well as provide an example of how they can be used to solve common programming problems.

What is the Recursive Function

In C programming, the recursive function is a function that calls itself during its execution. It is beneficial for solving complex problems that require repetitive computations or branching logic. By breaking a problem down into smaller sub-problems that can be solved recursively, the program can arrive at a solution efficiently and elegantly.

The following are two prerequisites for creating recursion in C Programming:

  1. An exit condition: This condition helps the function determine when to exit. Without an exit condition, the code may enter an infinite loop.
  2. Changing the counter: The counter should be changed with every call towards the function.

Syntax for Recursive Function in C

The syntax of C recursive function is given as:

return_type function_name(parameters) {
    // base case
    if (condition) {
        return some_value;
    }
    // recursive case
    return function_name(modified_parameters);
}

 
Here, return_type is the data type of the value returned by the function, function_name is the name of the function, and parameters are the input parameters passed to the function.

The function is first defined with a base case that provides a termination condition, and then a recursive case that calls the function itself with modified input parameters.

How to Use Recursive Function in C

When a recursive function is called, it sets aside some memory to run its operations. If the condition is met, it passes the result back to the previous function, which also frees up the memory it set aside. This process keeps repeating until the function that started it all returns its final output. However, when the criteria aren’t met, the function will continue to make recursive calls until it eventually crashes the program.

The following is a simple code to use the recursive function in C programming:

#include <stdio.h>

int factorial(int n) {
    // Base case
    if (n == 0) {
        return 1;
    }
    // Recursive case
    else {
        return n * factorial(n-1);
    }
}

int main() {
    int num;
    printf("Enter a non-negative number: ");
    scanf("%d", &num);

    printf("Factorial of %d is %d", num, factorial(num));
    return 0;
}

 
The above code prompts the user to enter a non-negative integer and calculates its factorial using a recursive function called factorial(). The function first checks if the base case is met (i.e., if the input is 0), and returns 1 if so. Otherwise, it calls itself with the argument (n-1) until the base case is met. A final result has been then returned to the main() function, which prints it to the console.

Conclusion

Recursive functions are a powerful programming technique for solving problems that require repeated execution of similar logic. However, they must be used carefully, as they require more memory and time than incremental programs. It is important to define a base condition for the recursive function and ensure that the exit condition is met to avoid an infinite loop. With the help of this tutorial, you now have a good understanding of how to create and use recursive functions in C programming.

About the author

Komal Batool Batool

I am passionate to research technologies and new ideas and that has brought me here to write for the LinuxHint. My major focus is to write on programming languages and computer science related topics.