70 likes | 83 Views
Learn about recursive functions and their activation records, call stacks, expressiveness compared to iteration, and efficiency concerns.
E N D
ICOM 4015 Advanced Programming Lecture 6 Procedural Abstraction IV Recursive Functions Reading: Chapter 3 Prof. Bienvenido Velez ICOM 4015
Adminsitrivia • Email lists: • icom4015-forum@ece.uprm.edu • icom4015-students@ece.uprm.edu • icom4015-staff@ece.uprm.edu • Examen I • Martes 6 de marzo – • 5-7 PM • S-113 ICOM 4015
Procedural Abstraction IV Outline • Recursive Functions • Activation records, call stacks • Expressiveness of recursion vs. iteration • Efficiency concerns • function call overhead • duplication of work • process complexity ICOM 4015
Example 0Factorials // factorials.cc // Implements recursive and interative versions of algorithms for // computing the factorial (N!) of a number. // Standard C++ header files #include <iostream> // Forward definitions of auxiliary functions long recFactorial(long n); long iterFactorial(long n); int main() { long number; while(true) { cout << "Please enter a positive number (or negative to end): "; cin >> number; if (number < 0) return 0; cout << "Recursive: " << number << "! = " << recFactorial(number) << endl; cout << "Iterative: " << number << "! = " << iterFactorial(number) << endl; } } long recFactorial(long n) { if (n==0) { return 1; } else { return (n * recFactorial(n - 1)); } } long iterFactorial(long n) { long product = 1; for (long i=1; i<=n; i++) { product *= i; } return product; } [bvelez@amadeus] ~/icom4015/lec07 >>factorials Please enter a positive number (or negative to end): 3 Recursive: 3! = 6 Iterative: 3! = 6 Please enter a positive number (or negative to end): 4 Recursive: 4! = 24 Iterative: 4! = 24 Please enter a positive number (or negative to end): 5 Recursive: 5! = 120 Iterative: 5! = 120 Please enter a positive number (or negative to end): 6 Recursive: 6! = 720 Iterative: 6! = 720 Please enter a positive number (or negative to end): -1 [bvelez@amadeus] ~/icom4015/lec07 >>fibonacci ICOM 4015
Example 1Fibonacci Numbers // fibonacci.cc // Iterative and recursive algorithms for computing Fibonacci numbers ... // Auxiliary Functions long recFibonacci(long n) { if (n==0) { return 0; } else if (n==1) { return 1; } else { return (recFibonacci(n-1) + recFibonacci(n-2)); } } long iterFibonacci(long n) { if (n==0) { return 0; } else if (n==1) { return 1; } long F0 = 0; long F1 = 1; long FN; for (long i=1; i<n; i++) { FN = F0 + F1; F0 = F1; F1 = FN; } return FN; } [bvelez@amadeus] ~/icom4015/lec07 >>fibonacci Please enter a positive number (or negative to end): 3 Recursive: F(3) = 2 Iterative: F(3) = 2 Please enter a positive number (or negative to end): 4 Recursive: F(4) = 3 Iterative: F(4) = 3 Please enter a positive number (or negative to end): 8 Recursive: F(8) = 21 Iterative: F(8) = 21 Please enter a positive number (or negative to end): ICOM 4015
Example 1Fibonacci Numbers // fibonacci.cc // Iterative and recursive algorithms for computing Fibonacci numbers // Standard C++ header files #include <iostream> // Forward definitions of auxiliary functions long recFibonacci(long n); long iterFibonacci(long n); int main() { long number; while(true) { cout << "Please enter a positive number (or negative to end): "; cin >> number; if (number < 0) return 0; cout << "Recursive: F(" << number << ") = " << recFibonacci(number) << endl; cout << "Iterative: F(" << number << ") = " << iterFibonacci(number) << endl; } } … … ... ICOM 4015
Iteration vs. RecursionSummary of Concepts • Recursion is as expressive as iteration • Iteration can yield faster code • less duplication of work • less function call overhead • Recursion can yield cleaner code • may rely on a “smart” optimizing compiler to minimize call overhead ICOM 4015