43 - Data Structure - Recursion Basics
43 - Data Structure - Recursion Basics
43 - Data Structure - Recursion Basics
if(value < 1)
return;
function(value - 1);
printf("%d ",value);
Example − a function that calls another function which in turn calls it again.
if(value1 < 1)
return;
function2(value1 - 1);
printf("%d ",value1);
function1(value2);
}
Properties
Implementation
This implies, the caller function has to suspend its execution temporarily and
resume later when the execution control returns from the callee function. Here,
the caller function needs to start exactly from the point of execution where it
puts itself on hold. It also needs the exact same data values it was working on.
For this purpose, an activation record (or stack frame) is created for the caller
function.
This activation record keeps the information about local variables, formal
parameters, return address and all information passed to the caller function.
Analysis of Recursion
One may argue why to use recursion, as the same task can be done with
iteration. The first reason is, recursion makes a program more readable and
because of latest enhanced CPU systems, recursion is more efficient than
iterations.
Time Complexity
Space Complexity