0% found this document useful (0 votes)
7 views2 pages

Week 5 (C) Recursion

Recursion is a programming technique where a function calls itself directly or indirectly, allowing operations to be expressed in terms of themselves. Recursive functions must have a base criteria to stop the recursion and a progressive approach to reach that base. While recursion can lead to cleaner code and is useful in complex data structures, it also consumes more memory and processing time compared to iteration.

Uploaded by

zarnabm476
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views2 pages

Week 5 (C) Recursion

Recursion is a programming technique where a function calls itself directly or indirectly, allowing operations to be expressed in terms of themselves. Recursive functions must have a base criteria to stop the recursion and a progressive approach to reach that base. While recursion can lead to cleaner code and is useful in complex data structures, it also consumes more memory and processing time compared to iteration.

Uploaded by

zarnabm476
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Recursion

Recursion is a programming technique that allows the programmer to express operations in terms of themselves. Simply
we can say that when a function calls itself.

In recursion, a function α either calls itself directly or calls a function β that in turn calls the original function α. The
function α is called recursive function.
In other words recursion is thus the process of defining something in terms of itself.

Recursive functions
Example − a function calling itself. Known as Direct Recusrrsion
int function(int value) {
if(value < 1)
return;
function(value - 1);

printf("%d ",value);
}
Example − a function that calls another function which in turn calls it again. Known as Indirect Recusrrsion
int function1(int value1) {
if(value1 < 1)
return;
function2(value1 - 1);
printf("%d ",value1);
}
int function2(int value2) {
function1(value2);
}

Properties
A recursive function can go infinite like a loop. To avoid infinite running of recursive function, there are two
properties that a recursive function must have −
• Base criteria − There must be at least one base criteria or condition, such that, when this condition is
met the function stops calling itself recursively.
• Progressive approach − The recursive calls should progress in such a way that each time a recursive
call is made it comes closer to the base criteria.

Difference between Recursion and Iteration


Criteria Recursion Iteration

When a function calls itself directly or When there are some set of instructions that are
Definition indirectly carried out repeatedly

Recursion is implemented with the use


Implementation of function calls Iteration is implemented by the use of loops
Stack memory is used to store local No memory is used except for the initialization
Memory Usage variables & parameters of control variables

Speed Slow speed of execution Fast speed of execution

We want to calculate the factorial value using recursive function.


Example of recursion.

How are recursive functions stored in memory or Implementation of Recurrsion?


Recursion uses more memory, because the recursive function adds to the stack with each recursive call, and keeps
the values there until the call is finished. The recursive function uses LIFO (LAST IN FIRST OUT) Structure just like the
stack data structure.

Advantages of Recursion
1. It makes our code shorter and cleaner.
2. Recursion is required in problems concerning data structures and advanced algorithms, such as Graph and
Tree Traversal.

Disadvantages of Recursion
1. It takes a lot of stack space compared to an iterative program.
2. It uses more processor time.
3. It can be more difficult to debug compared to an equivalent iterative program.

You might also like