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

Recursion

Recursion uses function calls where a function calls itself directly or indirectly. It requires a base case for termination. Stack memory is used to store states. Recursion is slower than iteration. Iteration uses loops to repetitively execute statements. It uses a control variable initialized and updated in each loop to check against a condition for termination. A loop continues updating the control variable until the condition is met. Iteration is faster than recursion.

Uploaded by

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

Recursion

Recursion uses function calls where a function calls itself directly or indirectly. It requires a base case for termination. Stack memory is used to store states. Recursion is slower than iteration. Iteration uses loops to repetitively execute statements. It uses a control variable initialized and updated in each loop to check against a condition for termination. A loop continues updating the control variable until the condition is met. Iteration is faster than recursion.

Uploaded by

deleonjherry07
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Recursion Iteration

 A statement in the function’s body  Allows the execution of a sequential


calls the function itself. set of statements repetitively using
 A recursive function must comprise of conditional loops.
at least one base case i.e. a condition  There are loops with a control
for termination of execution. variable that need to be initialized,
 The function keeps on converging to incremented or decremented and a
the defined base case as it conditional control statement that
continuously calls itself. continuously gets checked for the
 Stack memory is used to store the termination of execution.
current state of the function.  The value of the control variable
 If there is no base case defined, continuously approaches the value in
recursion causes a stack overflow the conditional statement.
error.  A control variable stores the value,
 The execution of recursion is which is then updated, monitored, and
comparatively slower. compared with the conditional
statement.
 Infinite loops keep utilizing CPU
cycles until we stop their execution
manually.
 The execution of iteration is
comparatively faster.

Create a program that will differentiate recursion over iteration (Lastname, FirstName)
#include <iostream> #include <iostream>
using namespace std; using namespace std;

void recursiveDisplay(int n) { void iterativeDisplay(int n) {


if (n > 0) { for (int i = 0; i < n; ++i) {
cout << "DE LEON, JHERRY\n"; cout << "DE LEON, JHERRY\n";
recursiveDisplay(n - 1); }
} }
}
int main() {
int main() { int repetitions;
int repetitions;
cout << "ITERATION\n ";
cout << "RECURSION\n "; cout << "Enter the number of repetitions: ";
cout << "Enter the number of repetitions: "; cin >> repetitions;
cin >> repetitions;
iterativeDisplay(repetitions);
recursiveDisplay(repetitions);
return 0;
return 0; }
}

Sourcelink:

What's the difference between recursion and iteration? (educative.io)

You might also like