C Recursive Functions

Recursive functions in C are functions that call themselves to solve problems by breaking them down into smaller, similar subproblems. They simplify complex problems such as calculating factorials, Fibonacci numbers, or computing powers by reusing the same logic repeatedly until a base condition is met.


Example 1: Factorial Using Recursion

In this example, we calculate the factorial of a number using a recursive function. The function calls itself with a decremented value until it reaches the base condition.

main.c

</>
Copy
#include <stdio.h>

int factorial(int n) {
    if(n <= 1)
        return 1;  // Base condition: factorial of 0 or 1 is 1
    else
        return n * factorial(n - 1);  // Recursive call
}

int main() {
    int number = 5;
    int result = factorial(number);
    printf("Factorial of %d is %d\n", number, result);
    return 0;
}

Explanation:

  1. factorial(int n): This recursive function computes the factorial of n.
  2. if(n <= 1): Checks the base condition; if n is 1 or less, it returns 1.
  3. return n * factorial(n - 1);: If n is greater than 1, the function calls itself with n - 1 and multiplies the result by n.
  4. In main(), we set number to 5, call factorial(number), and print the result.

Output:

Factorial of 5 is 120

Example 2: Fibonacci Sequence Using Recursion

In this example, we calculate the nth Fibonacci number using a recursive function. The Fibonacci sequence is defined such that each number is the sum of the two preceding ones.

main.c

</>
Copy
#include <stdio.h>

int fibonacci(int n) {
    if(n <= 1)
        return n;  // Base condition: return n if n is 0 or 1
    else
        return fibonacci(n - 1) + fibonacci(n - 2);  // Recursive call: sum of two preceding numbers
}

int main() {
    int n = 7;
    int result = fibonacci(n);
    printf("Fibonacci number at position %d is %d\n", n, result);
    return 0;
}

Explanation:

  1. fibonacci(int n): Defines a recursive function to compute the nth Fibonacci number.
  2. if(n <= 1): Base condition that returns n when n is 0 or 1.
  3. return fibonacci(n - 1) + fibonacci(n - 2);: Recursively calculates the sum of the two previous Fibonacci numbers.
  4. In main(), we call fibonacci(7) and display the result.

Output:

Fibonacci number at position 7 is 13

Example 3: Recursive Power Function

In this example, we compute the power of a number using recursion. The function calculates base raised to the power exponent by multiplying the base repeatedly.

main.c

</>
Copy
#include <stdio.h>

int power(int base, int exponent) {
    if(exponent == 0)
        return 1;  // Base condition: any number raised to the power 0 is 1
    else
        return base * power(base, exponent - 1);  // Recursive call: multiply base with power(base, exponent-1)
}

int main() {
    int base = 2;
    int exponent = 5;
    int result = power(base, exponent);
    printf("%d raised to the power %d is %d\n", base, exponent, result);
    return 0;
}

Explanation:

  1. power(int base, int exponent): A recursive function that computes the power of a number.
  2. if(exponent == 0): The base condition returns 1 when the exponent is 0.
  3. return base * power(base, exponent - 1);: For exponent greater than 0, the function calls itself with exponent - 1 and multiplies the result by base.
  4. In main(), we calculate 2^5 and print the result.

Output:

2 raised to the power 5 is 32

Conclusion

In this tutorial, we learned about recursive functions in C and explored three practical examples:

  1. Factorial Calculation: Demonstrates how recursion can simplify the computation of factorial values.
  2. Fibonacci Sequence: Uses recursion to compute Fibonacci numbers by summing previous terms.
  3. Power Function: Shows how to compute the power of a number recursively.

Recursive functions are a powerful tool in C, enabling you to solve problems that have a natural recursive structure.