Recursion in C

In this lesson, you will learn about recursion and writing recursive functions in C, their usage, advantages, and disadvantages.

In programming, a recursion function is a function that calls itself from its body. A recursion function calls itself until a specific condition is met. It’s advisable to double test your recursion function output before you deploy it. It can cause problems if a condition is not met, which will send the function into an infinite that causes your application to crash.

Recursive is mainly used to solve mathematical equations, such as series, integrals, Fibonacci, and more.

Structure of recursive function in C

void RecursiveFunction() {
RecursiveFunction (); // calls itself
}
int main() {
   RecursiveFunction ();
}

Example of recursive function in C

Below are a few examples of recursion in C.

Factorial program using recursion in C

#include <stdio.h>

int FindFactorial(int n) {
  if (n == 1) {
    return 1;
  }
  return n * FindFactorial(n - 1); // the function calls itself here
}
int main() {
  int number;
  int factorial = 0;

  printf("Please enter a number: ");

  scanf("%d", & number);
  factorial = FindFactorial(number);
  printf("The Factorial of %d is: %ld", number, factorial);

  printf("\n");
  return 0;
}
Output
Please enter a number: 3
The Factorial of 3 is: 6

Explanation of the Factorial program above

Calculate the power of a number using recursion in C

#include <stdio.h>

int CalcualtePower(int n, int p) {
  int result = 1;
  if (p == 0) {
    return result;
  }
  result = n * (CalcualtePower(n, p - 1)); // the function calls itself here
}
int main() {
  int number, power;
  int result;

  printf("Please enter a number: ");
  scanf("%d", & number);

  printf("Please enter a power: ");
  scanf("%d", & power);

  result = CalcualtePower(number, power);

  printf("%d to the power of %d is: %ld\n", number, power, result);

  return 0;
}

Find the sum of all digits using recursion in C

#include <stdio.h>

int FindSumDigits(int number) {
  int sum = 0;
  if (number) {
    sum += (number % 10);
    FindSumDigits(number / 10); // the function calls itself here
  } else {
    return sum;
  }
}

int main() {
  int number, sum;

  printf("Please enter a number: ");
  scanf("%d", & number);

  sum = FindSumDigits(number);

  printf("The Sum of all digits for %d are: %d\n", sum);

  return 0;
}

Advantage of Recursion in C

  • Recursion can reduce time complexity.
  • Recursion produces a clearer code.
  • Recursion travels a tree faster.

Disadvantage of Recursion in C

  • Recursion uses more memory than the iterative code.
  • Recursion can be slow for some larger numbers.

Points to remember

  • A recursive function is a function that calls itself.
  • The recursion function needs to meet a condition to exist, or it will go into an infinite loop
Back to: Learn C Programming > Functions in C

Leave a Reply

Your email address will not be published. Required fields are marked *


The reCAPTCHA verification period has expired. Please reload the page.