Presentation is loading. Please wait.

Presentation is loading. Please wait.

C++ Programming Lecture 12 Functions – Part IV

Similar presentations


Presentation on theme: "C++ Programming Lecture 12 Functions – Part IV"— Presentation transcript:

1 C++ Programming Lecture 12 Functions – Part IV
By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department

2 The Hashemite University
Outline Introduction. Recursion. Reference parameters and variables. Functions call by reference and call by value. Examples. The Hashemite University

3 The Hashemite University
Recursion I Recursive functions Are functions that calls themselves. Can only solve a base case. If not base case, the function breaks the problem into a slightly smaller, slightly simpler, problem that resembles the original problem and Launches a new copy of itself to work on the smaller problem, slowly converging towards the base case Makes a call to itself inside the return statement Eventually the base case gets solved and then that value works its way back up to solve the whole problem. The recursion step executes while the original call to the function is still open (not finished yet). The Hashemite University

4 The Hashemite University
Recursion II Example: factorial n! = n * ( n – 1 ) * ( n – 2 ) * … * 1 Can be solved either iteratively or recursively: Iteratively: int factorial = 1; for (int count = n; count >= 1; count--) factorial *= count; Recursively: Recursive relationship ( n! = n * ( n – 1 )! ) 5! = 5 * 4! 4! = 4 * 3!… Base case (1! = 0! = 1) The Hashemite University

5 Example Using Recursion: The Fibonacci Series
Each number sum of two previous ones Example of a recursive formula: fib(n) = fib(n-1) + fib(n-2) C++ code for fibonacci function long fibonacci( long n ) { if ( n == 0 || n == 1 ) // base case return n; else return fibonacci( n - 1 ) + fibonacci( n – 2 ); } The Hashemite University

6 Example Using Recursion: The Fibonacci Series
Diagram of Fibonacci function f( 3 ) f( 1 ) f( 2 ) f( 0 ) return 1 return 0 return + The Hashemite University

7 2.1 Call function fibonacci 2.2 Output results.
1 // Fig. 3.15: fig03_15.cpp 2 // Recursive fibonacci function 3 #include <iostream> 4 5 using std::cout; 6 using std::cin; 7 using std::endl; 8 9 unsigned long fibonacci( unsigned long ); 10 11 int main() 12 { unsigned long result, number; 14 cout << "Enter an integer: "; cin >> number; result = fibonacci( number ); cout << "Fibonacci(" << number << ") = " << result << endl; return 0; 20 } 21 22 // Recursive definition of function fibonacci 23 unsigned long fibonacci( unsigned long n ) 24 { 25 if ( n == 0 || n == 1 ) // base case return n; 27 else // recursive case return fibonacci( n - 1 ) + fibonacci( n - 2 ); 29 } 1. Function prototype 1.1 Initialize variables 2. Input an integer 2.1 Call function fibonacci 2.2 Output results. 3. Define fibonacci recursively Only the base cases return values. All other cases call the fibonacci function again.

8 Program Output Enter an integer: 0 Fibonacci(0) = 0
Enter an integer: 6 Fibonacci(6) = 8 Enter an integer: 20 Fibonacci(20) = 6765 Program Output Enter an integer: 30 Fibonacci(30) = Enter an integer: 35 Fibonacci(35) =

9 Recursion vs. Iteration
Repetition Iteration: explicit loop Recursion: repeated function calls Termination Iteration: loop condition fails Recursion: base case recognized Both can have infinite loops Balance between performance (iteration) and good software engineering (recursion). The Hashemite University

10 The Hashemite University
Reference Variables I Reference variable is an alias to some variable. & (ampersand) is used to signify a reference E.g: int x = 0; int &y = x; //y is a reference to an integer which is x Here y is an alias to the variable x since the address of y is equal to the address of x. So, modifying either x or y both variables will have the same modified value since both of them refer to the same memory location or address. The Hashemite University

11 Reference Variables II
Reference variables must be initialized within the same statement that defines them, if not it will be a syntax error. Reference variables must be initialized with a variable only, constants and expressions are not allowed  syntax error. You cannot reassign the reference variable to another variable since you simply copy the value of the new variable in the old one and you still working on the old one and this is considered as a logical error. The Hashemite University

12 The Hashemite University
Call By Reference I Two types of function call: Call by value Copy of data passed to function. Changes to copy do not change the original found in the caller. Used to prevent unwanted side effects. Call by reference Function can directly access data. Changes affect the original found in the caller. No copy exist (reduce overhead), however, it is dangerous since the original value is overwritten. The Hashemite University

13 The Hashemite University
Call By Reference II Function arguments can be passed by reference. In both the function header and prototype you must proceed the reference variable by &. In the function call just type the name of the variable that you want to pass. Inside the function body use the reference variable by its name without &. The Hashemite University

14 The Hashemite University
Call By Reference III Again the reference argument must be a variable (constants or expressions are not allowed  syntax error E.g.: void change( int &variable ) { variable += 3; } Adds 3 to the input variable which also affects the original variable (passed as argument to function change in the caller). The Hashemite University

15 The Hashemite University
Call By Reference IV Note that till now we have talked about the function arguments or parameters to be reference variables. But what about the return result? Can we make it a reference variable? If the function return a reference variable this means that it returns an alias to a variable inside that function. If this variable is not static (i.e. automatic) then this variable will be destroyed when the function ends. So, this function will return a reference to an undefined variable which is called dangling reference. Such thing is considered as a logical error. As possible avoid the usage of reference return results even to static variables. The Hashemite University

16 The Hashemite University
1 // Fig. 3.20: fig03_20.cpp 2 // Comparing call-by-value and call-by-reference 3 // with references. 4 #include <iostream> 5 6 using std::cout; 7 using std::endl; 8 9 int squareByValue( int ); 10 void squareByReference( int & ); 11 12 int main() 13 { 14 int x = 2, z = 4; 15 16 cout << "x = " << x << " before squareByValue\n" << "Value returned by squareByValue: " << squareByValue( x ) << endl << "x = " << x << " after squareByValue\n" << endl; 20 21 cout << "z = " << z << " before squareByReference" << endl; 22 squareByReference( z ); 23 cout << "z = " << z << " after squareByReference" << endl; 24 25 return 0; 26 } 27 28 int squareByValue( int a ) 29 { 30 return a *= a; // caller's argument not modified 31 } Notice the use of the & operator The Hashemite University

17 The Hashemite University
32 33 void squareByReference( int &cRef ) 34 { 35 cRef *= cRef; // caller's argument modified 36 } x = 2 before squareByValue Value returned by squareByValue: 4 x = 2 after squareByValue z = 4 before squareByReference z = 16 after squareByReference The Hashemite University

18 The Hashemite University
Additional Notes This lecture covers the following material from the textbook: Fourth Edition Chapter 3: Sections 3.12, 3.13, 3.14, 3.17 The Hashemite University


Download ppt "C++ Programming Lecture 12 Functions – Part IV"

Similar presentations


Ads by Google