RECURSIVE FUNCTIONS

  • Simple solutions and clear.
  • We can convert complex algoritms to simple.
  • Very powerfull.

MEMORY RAM:

  • Direct Recursive:
CODE#include<stdio.h>
long int multiplyNumbers(int n);
int main() {
int n;
printf("Enter a positive integer: ");
scanf("%d",&n);
printf("Factorial of %d = %ld", n, multiplyNumbers(n));
return 0;
}

long int multiplyNumbers(int n) {
if (n>=1)
return n*multiplyNumbers(n-1);
else
return 1;
}
  • Indirect Recursive:
CODEint testfunc1(int num) {
if (num == 0)
return 0;
else
return (testfunc2(num — 1));
}
int testfunc2(int num2) {
return testfunc1(num2–1);
}
  • Bottom Recursion:
CODEint fact(int n){
if (n == 1)
return 1;
else
return (n * fact(n - 1));
}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store