Recursion in C

Recursion in C

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

The origin of C is closely tied to the development of the Unix operating system, originally implemented in assembly language on a PDP-7 by Dennis Ritchie and Ken Thompson, incorporating several ideas from colleagues. Eventually, they decided to port the operating system to a PDP-11. The original PDP-11 version of Unix was developed in assembly language. Thompson needed a programming language to make utilities. At first, he tried to make a Fortran compiler, but soon gave up the idea and made a new language, B, Thompson’s simplified version of BCPL.[10] However, few utilities were written in B because B was too slow, and B could not take advantage of PDP-11 features such as byte addressability. In 1972, Ritchie started to improve B, which resulted in creating a new language C. C compiler and some utilities made by C were included in Version 2 Unix.

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.

Code Example

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
        return num;

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.