In C programming, a recursion function is a function that calls itself. A function that calls itself is known as a recursive function. Recursion is a technique in which a function calls itself, either directly or indirectly, in order to solve a problem. Recursive functions are used to solve problems that can be broken down into smaller, identical sub-problems
The basic syntax of a recursive function in C is as follows:
Syntax :
return_type function_name ( parameters )
{
// Base case if (base_case_condition)
{
return base_case_result ;
}
// Recursive case
else
{
return function_name ( modified_parameters ) ;
}
}
Here, the return_type specifies the data type of the value that the function will return, the function_name is the name of the function, and the parameters are the input values that the function takes. The base_case_condition is a test that is used to check if the problem has been solved and the base_case_result is the value that is returned when the problem has been solved. In the recursive case, the function calls itself with modified_parameters in order to solve a smaller sub-problem. Here is an example of a recursive function that calculates the factorial of a given number:
//Recursion Function in C Programming #include<stdio.h> /* 5! 1*2*3*4*5 */ int factorial(int i) //5 { if(i<=1){ return 1; } return i*factorial(i-1); //5*4*3*2*1 } int main() { printf("Factorial : %d",factorial(5)); return 0; }To download raw file Click Here
Factorial : 120
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions