Chapter 5 Function Basics Liang Introduction to Programming

Chapter 5 Function Basics Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 1

Call Stacks ØEach time a function is invoked, the system stores its arguments and variables in an area of memory, known as a stack. ØWhen a function calls another function, the caller’s stack space is kept intact, and new space is created to handle the new function call. ØWhen a function finishes its work and returns to its caller, its associated space is released. Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 2

Call Stacks Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 3

animation Trace Call Stack i is declared and initialized Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 4

animation Trace Call Stack j is declared and initialized Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 5

animation Trace Call Stack Declare k Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 6

animation Trace Call Stack Invoke max(i, j) Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 7

animation Trace Call Stack pass the values of i and j to num 1 and num 2 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 8

animation Trace Call Stack (num 1 > num 2) is true Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 9

animation Trace Call Stack Assign num 1 to result Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 10

animation Trace Call Stack Return result and assign it to k Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 11

animation Trace Call Stack Execute print statement Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 12

void Functions q. The preceding section gives an example of a value-returning function. This section shows how to declare and invoke a void function. A void function does not return a value. q. The following program declares a function named print. Grade and invokes it to print the grade for a given score. Test. Void. Function. cpp Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 13

#include <iostream> using namespace std; /** Print grade for the score */ void print. Grade(double score) { if (score < 0 || score > 100) { cout << "Invalid score"; return; } if (score >= 90. 0) cout << 'A'; else if (score >= 80. 0) cout << 'B'; else if (score >= 70. 0) cout << 'C'; else if (score >= 60. 0) cout << 'D'; else cout << 'F'; } Test. Void. Function. cpp Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 14

int main() { cout << "Enter a score: "; double score; cin >> score; Test. Void. Function. cpp cout << "The grade is "; print. Grade(score); return 0; } Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 15

Passing Parameters by Values q. When you invoke a function with a parameter, the value of the argument is passed to the parameter. This is referred to as pass-by-value. q. The variable is not affected, regardless of the changes made to the parameter inside the function. Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 16

#include <iostream> using namespace std; Passing Parameters by Values Increment. cpp void increment(int n) { n++; cout << "n inside the function is " << n << endl; } int main() { int x = 1; cout << "Before the call, x is " << x << endl; increment(x); cout << "after the call, x is " << x << endl; system("PAUSE"); return 0; } Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 17

Passing Parameters by Values #include <iostream> using namespace std; This program creates a function for swapping two variables. The values of the arguments are not changed after the function is invoked. void swap(int n 1, int n 2) { cout << "t. Inside the swap function" << endl; cout << "tt. Before swapping n 1 is " << n 1 << " n 2 is " << n 2 << endl; // Swap n 1 with n 2 int temp = n 1; n 1 = n 2; n 2 = temp; Test. Pass. By. Value. cpp cout << "tt. After swapping n 1 is " << n 1 << " n 2 is " << n 2 << endl; } Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 18

Passing Parameters by Values int main() { // Declare and initialize variables int num 1 = 1; Test. Pass. By. Value. cpp int num 2 = 2; cout << "Before invoking the swap function, num 1 is " << num 1 << " and num 2 is " << num 2 << endl; // Invoke the swap function to attempt to swap two variables swap(num 1, num 2); cout << "After invoking the swap function, num 1 is " << num 1 << " and num 2 is " << num 2 << endl; system("PAUSE"); return 0; } Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 19

Passing Parameters by References q. Using pass-by-value , the values in original parameters are not changed. qpass-by-reference shares the same value and hence the original value is actually changed. Test. Pass. By. Reference. cpp Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 20

#include <iostream> using namespace std; Passing Parameters by References Test. Pass. By. Reference. cpp // Swap two variables void swap(int &n 1, int &n 2) { cout << "t. Inside the swap function" << endl; cout << "tt. Before swapping n 1 is " << n 1 << " n 2 is " << n 2 << endl; // Swap n 1 with n 2 int temp = n 1; n 1 = n 2; n 2 = temp; cout << "tt. After swapping n 1 is " << n 1 << " n 2 is " << n 2 << endl; } Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 21

int main() { // Declare and initialize variables int num 1 = 1; int num 2 = 2; Test. Pass. By. Reference. cpp cout << "Before invoking the swap function, num 1 is " << num 1 << " and num 2 is " << num 2 << endl; // Invoke the swap function to attempt to swap two variables swap(num 1, num 2); cout << "After invoking the swap function, num 1 is " << num 1 << " and num 2 is " << num 2 << endl; system("PAUSE"); return 0; } Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 22

Overloading Functions q. The max function that was used earlier works only with the int data type. But what if you need to find which of two floatingpoint numbers has the maximum value? The solution is to create another function with the same name, max, but different parameters. q The following program creates three functions, the first finds the maximum integer, the second finds the maximum double, and the third finds the maximum among three double values. Test. Function. Overloading. cpp q Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 23

#include <iostream> using namespace std; Overloading Functions /** Return the max between two int values */ int max(int num 1, int num 2) { if (num 1 > num 2) Test. Function. Overloading. cpp return num 1; else return num 2; } /** Find the max between two double values */ double max(double num 1, double num 2) { if (num 1 > num 2) return num 1; else return num 2; } Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 24

/** Return the max among three double values */ double max(double num 1, double num 2, double num 3) { return max(num 1, num 2), num 3); } Test. Function. Overloading. cpp int main() { // Invoke the max function with int parameters cout << "The maximum between 3 and 4 is " << max(3, 4) << endl; // Invoke the max function with the double parameters cout << "The maximum between 3. 0 and 5. 4 is " << max(3. 0, 5. 4) << endl; // Invoke the max function with three double parameters cout << "The maximum between 3. 0, 5. 4, and 10. 14 is " << max(3. 0, 5. 4, 10. 14) << endl; system("PAUSE"); return 0; } Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 25

Ambiguous Invocation Sometimes there may be two or more possible matches for an invocation of a function, but the compiler cannot determine the most specific match. This is referred to as ambiguous invocation. Ambiguous invocation is a compilation error. Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 26

Ambiguous Invocation #include <iostream> using namespace std; int max. Number(int num 1, double num 2) { if (num 1 > num 2) return num 1; else return num 2; } double max. Number(double num 1, int num 2) { if (num 1 > num 2) return num 1; else return num 2; } int main() { cout << max. Number(1, 2) << endl; return 0; } Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 27
- Slides: 27