Recursion in C programming landguage

Recursion in C language

The term recursion refers to a situation in which a function calls itself either directly or indirectly. Indirect recursion occurs when one function calls another function that then calls the first function. C allows recursive functions, and they can be useful in some situations.

For example, recursion can be used to calculate the factorial of a number. The factorial of a number x is written x! and is calculated as follows:C version history

At Version 4 Unix released at Nov. 1973, the Unix kernel was extensively re-implemented by C.[10] By this time, the C language had acquired some powerful features such as struct types. Unix was one of the first operating system kernels implemented in a language other than assembly. Earlier instances include the Multics system (which was written in PL/I) and Master Control Program (MCP) for the Burroughs B5000 (which was written in ALGOL) in 1961. In around 1977, Ritchie and Stephen C. Johnson made further changes to the language to facilitate portability of the Unix operating system. Johnson’s Portable C Compiler served as the basis for several implementations of C on new platforms.

recursion fuction in c language with example Androwep-Tutorials
recursion fuction in c language with example Androwep-Tutorials

Code Example


#include<stdio.h>
int sum(int n);
int main()
{
    int number, result;
    printf("Enter a positive integer: ");
    scanf("%d", &number);
    result = sum(number);
    printf("sum = %d", result);
    return 0;
}
int sum(int num)
{
    if (num!=0)
        return num + sum(num-1); // sum() function calls itself
    else
        return num;
}

Example recursion

int factorial(int n){

if(n==0) return 1;

else return (n* factorial(n-1));

}

What is recursion in C language?

Recursion is the process of repeating items in a self-similar way. … 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.

What is the difference between function and recursion?

Recursion is when a statement in a function calls itself repeatedly. The iteration is when a loop repeatedly executes until the controlling condition becomes false. The primary difference between recursion and iteration is that is a recursion is a process, always applied to a function.

What are the advantages of recursion in C?

Reduce unnecessary calling of function. 2. Through Recursion one can Solve problems in easy way while its iterative solution is very big and complex.For example to reduce the code size for Tower of Honai application, a recursive function is bet suited.

What is recursion in C with example?

Recursion is the process which comes into existence when a function calls a copy of itself to work on a smaller problem. Any function which calls itself is called recursive function, and such function calls are called recursive calls. … For Examplerecursion may be applied to sorting, searching, and traversal problems.