Recursion
Recursion is a programming technique where a function calls itself again and again.Base case: Every recursive function uses a condition to terminate. It is known as base case. As soon as the condition for base case is true the control terminates the function otherwise, it keeps calling the function
Recursion is a programming technique where a function calls itself again and again.
Base case: Every recursive function uses a condition to terminate. It is known as base case. As soon as the condition for base case is true the control terminates the function otherwise, it keeps calling the function again and again(infinitely).
The statement that calls the function repeatedly from its block is said to be 'Recursive case'.
Advantages of recursion
* It makes the code small and clear so the program code becomes clear and easy to understand.
* Minimum number of variables are used
* The algorithm is precise, readable and efficient
*Easier to debug and obtain error free result.
Limitations of recursion
* More execution time
* It can result in stack overflow
* Occupies more space
Example code of a recursive function:
Types of recursion
1. Direct recursion (linear recursion)
In this type of recursion the function is called from within the same block.
Two categories of direct recursion are
* Tail recursion: In this type of recursion recursive call is the last statement of the function. For example factorial of a number code.
//Recursive factorial function
int fact(int n)
{
if(n==0) return 1;
else
return (n* fact(n-1));
}* Binary recursion:In this type of recursion function calls itself from two distinct points. For example GCD program code.
//Recursive GCD function
int gcd(int n1,int n2)
{
if(n1==n2)
return n1;
else
if(n1>n2)
return(gcd(n1-n2,n2));
else
return(gcd(n1,n2-n1));
}2. Indirect Recursion (Mutual Recursion):
In this type of recursion the function is called from another function block mutually. Let two functions A and B such that function B is called from function A and vice versa then this technique is called mutual recursion.
For programs related to recursion click the given link
Armstrong Number with Recursion
Binary Search
Related Posts
Continue learning with these related articles

ISC Practical 2027: College Enrolment Code Validator and Stream Report – Algorithm and Java Program
Fully solved ISC Computer Science Practical 2027 question: validate college enrolment codes made of a stream, year and serial number, give the exact reason for each rejection, auto-correct lowercase streams and prepare a stream-wise report.

ISC Practical 2027: Digital Lock Security Code Analyzer – Algorithm and Java Program
Fully solved ISC Computer Science Practical 2027 number-logic question: for a 5 to 10 digit security code, find the first and last digits, odd and even position sums, product of non-zero digits, digit frequency and classify the code as STRONG, MODERATE or WEAK.

Boolean Algebra for ISC Class 12: Laws, Canonical SOP and POS, and Simplification
ISC Class 12 Computer Science notes on Boolean algebra: all the laws and theorems, duality, De Morgan's laws, minterms and maxterms, converting to canonical SOP and POS, and step-by-step algebraic simplification with solved examples.