0% found this document useful (0 votes)
11 views

Trade-off between recursion and iteration

Dsa

Uploaded by

Tanisha jain
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)
11 views

Trade-off between recursion and iteration

Dsa

Uploaded by

Tanisha jain
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
You are on page 1/ 2

Trade-off between recursion and iteration:

• Recursion and iteration both repeatedly execute the set of instructions.


• 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 recursion is a process,
always applied to a function while the iteration is applied to the set of instructions
which we want to get repeatedly executed.

BASIS FOR RECURSION ITERATION


COMPARISON
Basic The statement in a body of Allows the set of instructions
function calls the function to be repeatedly executed.
itself.
Format In recursive function, only Iteration includes
termination condition (base initialization, condition,
case) is specified. execution of statement within
loop and update (increments
and decrements) the control
variable.
Stack The stack is used to store the Does not uses stack.
set of new local variables and
parameters each time the
function is called.
Condition If the function does not If the control condition in the
converge to some condition iteration statement never
called (base case), it leads to become false, it leads to
infinite recursion. infinite iteration.
Infinite Repetition Infinite recursion can crash Infinite loop uses CPU cycles
the system. repeatedly.
Overhead Recursion possesses the No overhead of repeated
overhead of repeated function function call.
calls.
Speed Slow in execution. Fast in execution.
Size of Code Recursion reduces the size of Iteration makes the code
the code. longer.
Applied Recursion is always applied to Iteration is applied to iteration
functions. statements or "loops".

Conclusion:

The recursive function is easy to write, but they do not perform well as compared to
iteration whereas, the iteration is hard to write but their performance is good as
compared to recursion.
Example:

Let us understand recursion with a function which will return the factorial of the
number.

int factorial(int num)


{
int answer;
if (num==1)
{
return 1;
}
else
{
answer = factorial(num-1) * num; //recursive calling
}
return (answer);
}

Let’s understand iteration regarding above example.

int factorial(int num)


{
int answer=1; //needs initialization because it may contain a garbage value before its initialization

for(int t =1; t>num; t++) //iteration


{
answer=answer * (t);
return (answer);
}

You might also like