C Recursion

Published:2020-03-17
Share:

Recursion in programming is the process of calling a procedure repeatedly in a self-similar way, a recursive function or routine is a function that call itself repeatedly.

void recursion() {
   recursion(); /* function calls itself */
}

int main() {
   recursion();
}

The C programming language supports recursion, i.e., a function to call itself. But while using recursion, programmers need to be careful to define an exit condition from the function, otherwise it will go into an infinite loop. Recursive functions are very useful to solve many mathematical problems, such as calculating the factorial of a number, generating Fibonacci series, etc. Number Factorial The following example calculates the factorial of a given number using a recursive function :

#include <stdio.h>

unsigned long long int factorial(unsigned int i) {

   if(i <= 1) {
      return 1;
   }
   
   return i * factorial(i - 1);
}

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

When the above code is compiled and executed, it produces the following result:

Factorial of 12 is 479001600

 

Generating Fibonacci series is another example of using recursive function:

#include <stdio.h>

int fibonacci(int i) {

   if(i == 0 || i == 1) {
      return i;
   }
	
   return fibonacci(i-1) + fibonacci(i-2);
}

int main() {

   int i;
	
   for (i = 0; i < 10; i++) {
      printf("%d ", fibonacci(i));
   }
	
   return 0;
}

When the above code is compiled and executed, it produces the following result :

0 1 1 2 3 5 8 13 21 34

 

Read also

C Functions

All C programs has at least one function, which is main() ,  and all the most simple programs define more functions. A function is a group of statements that together perform a task. You can divide up your code into separate functions. How you divide up your code among different functions is up to you, but logically the division is such that each function performs a specific task. A function declaration tells the compiler about a function's name, return type, and parameters. A functi...

Go Range

The range keyword is used to iterate over elements of an slice, array, map or channel. range over Arrays and Slices Use one variable to get the index of an element. Use two varaibles to get both index and the value of an element. range over Maps Use one variable to get the key of an element. Use two varaibles to get both key and the value of an element. range over Channels Use one variable to get the value of an element. How to use range ? ...

Using Linux Debian As Router Setup

In this articles series you will learn how to setup Linux (Debian) as a Router for home/business local networks, to spice things I''ll show how to add dns server, proxy/web caching server, and basic web traffic filter to our cute debian box.   In this series I''ll go over: Introduction . Hardware Requirements . Software installation . Basic Setup . /etc/network/Interfaces Configuration . Unbound dns server setup . DHCP server setup . Squid setup. ...

Php Predefined Variables

PHP  language has relatively  large number of predefined variables. Moreover an additional set of predefined arrays containing variables from the web server the environment, and user input. These new arrays are called SUPERGLOBALS Although many PHP frameworks provide abstractions over these superglobals, you still can access these variables as they are always  automatically available in all scopes, PHP SUPERGLOBALS Variable & Description $GLOBALS ...