Presentation is loading. Please wait.

Presentation is loading. Please wait.

Ms N Nashandi Dr SH Nggada Week 08 – Recursion. Outline We shall be covering the following What is a recursion? Iteration versus Recursion. Recursion.

Similar presentations


Presentation on theme: "Ms N Nashandi Dr SH Nggada Week 08 – Recursion. Outline We shall be covering the following What is a recursion? Iteration versus Recursion. Recursion."— Presentation transcript:

1 Ms N Nashandi Dr SH Nggada Week 08 – Recursion

2 Outline We shall be covering the following What is a recursion? Iteration versus Recursion. Recursion 22016/01/03Ms N Nashandi and Dr SH Nggada

3 What is Recursion? Recursion refers to a function (or method) calling itself This could be a direct or indirect call Direct call Assuming you have a function named fun1, if fun1 calls itself within the code of fun1 then this is a direct call Indirect call Assuming fun1 calls another function named fun2 and fun2 in turn calls fun1 then this is an indirect call This could get complicated, as fun2 itself will experience an indirect call of itself, however, the process would have been initiated by fun1 2016/01/04NUST Advanced Programming3

4 Iteration versus Recursion Recursion just like Iteration, may lead to indefinite execution of code Therefore, as it is the case with iteration, a means to ending the recursion should exist in the function 2016/01/04NUST Advanced Programming4

5 Iteration versus Recursion An iteration has no means of calling itself but contains statements that refer back to the start of the loop. On the other hand, a recursion has a means to calling itself Recursion is usually used to simplify computation than it could be done with a iteration A more sophisticated recursion (as used in Quick sort) could divide problems into simpler parts (divide and conquer) Iteration and recursion both use repetition Iteration explicitly uses repetition structure while recursion achieves repetition via repeated method calls 2016/01/04NUST Advanced Programming5

6 Iteration versus Recursion Which is better? There is no clear answer Mathematicians prefer recursive approach Solutions are often shorter Note, good recursive solutions may be more difficult to design and test. Programmers seem to often prefer iterative solutions It seems more appealing to many Control stays within or local to loop and also appears less magical 2016/01/04NUST Advanced Programming6

7 Iteration versus Recursion Which Approach to Use It largely depends on the problem For instance, factorial problem which is often used to demonstrate the use of recursion is pretty artificial It doe not matter what approach you use because it is so simple The use of factorial though to demonstrate recursion helps in understanding the concept of recursion For many advanced data structures (e.g. trees), are more efficient if implemented using recursive approach If you are not sure which one is better for a given problem, you could use code analysis techniques to figure out which one to use 2016/01/04NUST Advanced Programming7

8 Recursion To understand the implementation of recursion, we shall use examples Example 1 The evaluation of a formula such as X n where X is a double (thus, could take an integer equivalent value) and n is an integer This simply refers to multiplying X by itself n times Example 2 The evaluation of the factorial of a given integer, i.e. X! This simply refers to the multiple of X and its preceding integers and stopping at 0. Note, 0! = 1 2016/01/04NUST Advanced Programming8

9 Recursion To understand the implementation of recursion, we shall use examples We shall use 2 implementations for each example Iteration Recursion 2016/01/04NUST Advanced Programming9

10 Recursion Example 1 - X n Iteration 2016/01/04NUST Advanced Programming10 int main() { double X; // base number int n; // power number cout << "Enter Base Number: "; // prompts the user to enter base cin >> X; // reads base number cout << "Enter Power: "; // prompts the user to enter power cin >> n; // reads power double result = 1; // holds the result of the evaluation // Calculates X raised to power of n for (int i = 1; i <= n; i++) { result = result * X; } cout << X << " to the power of " << n << " is " << result << endl; system("pause"); // pauses execution and waits for key stroke return 0; // returns to the operating system }

11 Recursion Example 1 - X n Iteration 2016/01/04NUST Advanced Programming11 Example execution

12 Recursion Example 1 - X n Recursion 2016/01/04NUST Advanced Programming12 int main() { double X; // base number int n; // power number cout << "Enter Base Number: "; // prompts the user to enter base cin >> X; // reads base number cout << "Enter Power: "; // prompts the user to enter power cin >> n; // reads power double result; // holds the result of the evaluation result = Power(X, n); // calls the recursive function cout << X << " to the power of " << n << " is " << result << endl; system("pause"); // pauses execution and waits for key stroke return 0; // returns to the operating system }

13 Recursion Example 1 - X n Recursion 2016/01/04NUST Advanced Programming13 double Power(double X, int n) { if (n == 0) return 1; // returns 1 if the power is 0 else return X * Power(X, n - 1); // performs recursive evaluation } Recursive function – Power(X, n)

14 Recursion Example 1 - X n Recursion 2016/01/04NUST Advanced Programming14 Example execution

15 Recursion Example 1 - X n The if statement provides for the value 1 to be returned if n is zero, and in all other cases, it returns the result of the expression, X*Power(X, n-1) The expression causes a further call to the function Power() with current value of n reduced by 1 Thus, the else clause in the if statement provides the essential mechanism necessary to avoid an indefinite sequence of recursive function calls Clearly, if the value of n is other than zero within the function Power(), a further call to the function Power() occurs For any given value of n other than 0, the function calls itself n times 2016/01/04NUST Advanced Programming15

16 Recursion Example 2 – X! Iteration 2016/01/04NUST Advanced Programming16 int main() { int X; // base number cout << "Enter Number: "; // prompts the user to enter base cin >> X; // reads base number int result = 1; // holds the result of the evaluation and intialises it to 1 for (int i = X; i >= 1; i--) // from X down to 1; could be the other way round { result = result * i; // performs the evaluations } cout << X << " factorial is " << result << endl; // prints out the result system("pause"); // pauses execution and waits for key stroke return 0; // returns to the operating system }

17 Recursion Example 2 – X! Iteration 2016/01/04NUST Advanced Programming17 Example execution

18 Recursion Example 2 – X! Recursion 2016/01/04NUST Advanced Programming18 int main() { int X; // base number cout << "Enter Number: "; // prompts the user to enter base cin >> X; // reads base number int result = Factorial(X); // calls the recursive function for factorial cout << X << " factorial is " << result << endl; // prints out the result system("pause"); // pauses execution and waits for key stroke return 0; // returns to the operating system }

19 Recursion Example 2 – X! Recursion 2016/01/04NUST Advanced Programming19 int Factorial(int X) { if (X == 0) return 1; else return X * Factorial(X - 1); }

20 Recursion Example 2 – X! Recursion 2016/01/04NUST Advanced Programming20 Example execution

21 Recursion From the examples, it could be deduced that iteration exclusively uses repetition while Recursion uses selection to achieve repetition 2016/01/04NUST Advanced Programming21

22 Further Reading Find out to use recursion for more complex problems or data structures 222016/01/03Ms N Nashandi and Dr SH Nggada

23 End of Lecture 23 ? 2016/01/03Ms N Nashandi and Dr SH Nggada


Download ppt "Ms N Nashandi Dr SH Nggada Week 08 – Recursion. Outline We shall be covering the following What is a recursion? Iteration versus Recursion. Recursion."

Similar presentations


Ads by Google