Chapter 5 Functions Liang Introduction to C Programming


































































- Slides: 66

Chapter 5 Functions Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445 X 1

Objectives F F F F F To create functions, invoke functions, and pass arguments to a function (§ 5. 2 -5. 4). To understand the differences between pass-by-value and pass-byreference (§§ 5. 5 -5. 6). To use function overloading and understand ambiguous overloading (§ 5. 7). To use function prototypes for declaring function headers (§ 5. 8). To know how to use default arguments (§ 5. 9). To create header files for reusing functions (§ 5. 11). To determine the scope of local and global variables (§ 5. 13). To develop applications using the C++ mathematical functions (§ 5. 14). To design and implement functions using stepwise refinement (§ 5. 15). (Optional) To improve runtime efficiency using inline functions (§ 5. 16). Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445 X 2

Introducing Functions A function is a collection of statements that are grouped together to perform an operation. Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445 X 3

Introducing Functions, cont. • Function signature is the combination of the function name and the parameter list. • The variables defined in the function header are known as formal parameters. • When a function is invoked, you pass a value to the parameter. This value is referred to as actual parameter or argument. Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445 X 4

Introducing Functions, cont. • A Function may return a value. The return. Value. Type is the data type of the value the function returns. If the function does not return a value, the return. Value. Type is the keyword void. Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445 X 5

Calling Functions Listing 5. 1 Testing the max Function This program demonstrates calling a Function max to return the largest of the int values Test. Max Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445 X 6

int max(int num 1, int num 2) { int main() int result; { if (num 1 > num 2) result = num 1; int i = 5, j = 2; else result = num 2; int largest; return result; int largest if (i >=j)max(i, j); } largest = i; else largest= j; cout << "The maximum between " << i <<" and " << j << " is " << largest; return 0; } Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445 X 7

#include <iostream> using namespace std; //function definition int max(int num 1, int num 2) { int result; if (num 1 > num 2) result = num 1; else result = num 2; return result; } int main() { int i = 5, j = 2; int largest = max(i, j); cout << "The maximum between " << i <<" and " << j << " is " << largest; return 0; } Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445 X 8

animation Calling Functions, cont. Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445 X 9

animation Trace Function Invocation i is now 5 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445 X 10

animation Trace Function Invocation j is now 2 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445 X 11

animation Trace Function Invocation invoke max(i, j) Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445 X 12

animation Trace Function Invocation invoke max(i, j) Pass the value of i to num 1 Pass the value of j to num 2 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445 X 13

animation Trace Function Invocation declare variable result Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445 X 14

animation Trace Function Invocation (num 1 > num 2) is true since num 1 is 5 and num 2 is 2 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445 X 15

animation Trace Function Invocation result is now 5 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445 X 16

animation Trace Function Invocation return result, which is 5 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445 X 17

animation Trace Function Invocation return max(i, j) and assign the return value to k Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445 X 18

animation Trace Function Invocation Execute the print statement Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445 X 19

animation Trace Call Stack i is declared and initialized Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445 X 20

animation Trace Call Stack j is declared and initialized Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445 X 21

animation Trace Call Stack Declare k Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445 X 22

animation Trace Call Stack Invoke max(i, j) Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445 X 23

animation Trace Call Stack pass the values of i and j to num 1 and num 2 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445 X 24

animation Trace Call Stack (num 1 > num 2) is true Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445 X 25

animation Trace Call Stack Assign num 1 to result Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445 X 26

animation Trace Call Stack Return result and assign it to k Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445 X 27

animation Trace Call Stack Execute print statement Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445 X 28

void Functions The preceding section gives an example of a nonvoid function. This section shows how to declare and invoke a void function. Listing 5. 2 gives a program that declares a function named print. Grade and invokes it to print the grade for a given score. Test. Void. Function Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445 X 29

#include <iostream> using namespace std; 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'; } int main() { cout << "Enter a score: "; double score; cin >> score; cout << "The grade is "; print. Grade(score); return 0; } Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445 X 30

Pass by Value When you invoke a function with a parameter, the value of the argument is passed to the parameter. This is referred to as passby-value. If the argument is a variable rather than a literal value, the value of the variable is passed to the parameter. The variable is not affected, regardless of the changes made to the parameter inside the function. We will examine an interesting scenario in the following example, in which the parameters are changed in the function but the arguments are not affected. Test. Pass. By. Value Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445 X 31

#include <iostream> using namespace std; 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; return 0; } Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445 X 32

Pass by Value, cont. Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445 X 33

Reference Variables C++ provides a special type of variable, called a reference variable, which can be used as a function parameter to reference the original variable. A reference variable is an alias for another variable. Any changes made through the reference variable are actually performed on the original variable. To declare a reference variable, place the ampersand (&) in front of the name. For example, see Listing 5. 4. Test. Reference. Variable Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445 X 34

#include <iostream> using namespace std; int main() { int count = 1; int &ref. Count = count; ref. Count++; cout << "count is " << count << endl; cout << "ref. Count is " << ref. Count << endl; return 0; } Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445 X 35

Pass By Reference You can use a reference variable as a parameter in a function and pass a regular variable to invoke the function. The parameter becomes an alias for the original variable. This is known as passby-reference. When you change the value through the reference variable, the original value is actually changed. Test. Pass. By. Reference Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445 X 36

#include <iostream> using namespace std; 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; return 0; } Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445 X 37

using namespace std; // Swap two variables void swap(int &n 1, int &n 2) { int temp = n 1; n 1 = n 2; n 2 = temp; } int main() { int num 1 = 1; int num 2 = 2; // Invoke the swap function to attempt to swap two variables swap(num 1, num 2); system("pause"); return 0; } Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445 X 38

Overloading Functions The max function that was used earlier works only with the int data type. But what if you need to find which of two floating-point numbers has the maximum value? The solution is to create another function with the same name but different parameters, as shown in the following code: Test. Function. Overloading Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445 X 39

#include <iostream> using namespace std; /** Return the max between two int values */ int max(int num 1, int num 2) { if (num 1 > num 2) 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; } /** Return the max among three double values */ double max(double num 1, double num 2, double num 3) { 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; return max(num 1, num 2), num 3); } return 0; } Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445 X 40

Function Prototypes Before a function is called, it must be declared first. One way to ensure it is to place the declaration before all function calls. Another way to approach it is to declare a function prototype before the function is called. A function prototype is a function declaration without implementation. The implementation can be given later in the program. Test. Function. Prototype Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445 X 41

#include <iostream> using namespace std; // Function prototype int max(int num 1, int num 2); double max(double num 1, double num 2, double num 3); 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; return 0; } Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445 X 42

/** Return the max between two int values */ int max(int num 1, int num 2) { if (num 1 > num 2) 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; } /** 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); } Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445 X 43

Default Arguments C++ allows you to declare functions with default argument values. The default values are passed to the parameters when a function is invoked without the arguments. Default. Argument. Demo Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445 X 44

#include <iostream> using namespace std; void print. Area(double radius = 1) { double area = radius * 3. 14159; cout << "area is " << area << endl; } int main() { print. Area(); print. Area(4); return 0; } Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445 X 45

Reusing Functions by Different Programs One of the benefits of functions is for reuse. In the preceding sections, you declared functions and used them from the same program. To make the functions available for other programs to use, you need to place the functions in a separate file, called header file. By convention, the file has a. h extension. Programs use #include preprocessor directives to include header files in order to reuse the functions defined in the header file. Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445 X 46

#include <iomanip> using namespace std; void Print. Title() { cout<<"n "; for(int i=1; i<=50; i++) cout<<"*"; //for(int j=1; j<=15; j++) cout<<endl<<setw(17)<<"**"<<setw(48)<<"**"; cout<<endl<<setw(17)<<"**"<<setw(33)<<"C++ Programming Language"<<setw(15)<<"**"; cout<<endl<<setw(17)<<"**"<<setw(30)<<" name: Rose J. Tomas"<<setw(18)<<"**"; cout<<endl<<setw(17)<<"**"<<setw(30)<<" St. number: 20082293"<<setw(18)<<"**"; cout<<endl<<setw(17)<<"**"<<setw(31)<<" Programming Exercise"<<setw(17)<<"**"; cout<<endl<<setw(17)<<"**"<<setw(33)<<" Introduced To: Dr. Robinson"<<setw(15)<<"**"; cout<<endl<<setw(17)<<"**"<<setw(48)<<"**"; cout<<"n "; for(int i=1; i<=50; i++) cout<<"*"; cout<<"n"; //system("pause"); } Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445 X 47

#include <iostream> #include"Print. Title. h" using namespace std; int main() { Print. Title(); cout<<"n ***** This program is to calculate the factorial of an integer *****n"; // Prompt the user to enter an integer cout << "n Please Enter an integer: "; int n; cin >> n; double fact=1; for (int i=1; i<=n; i++) fact=fact*i; cout<< "n "<<n <<" ! = "<<fact<<endl; system("pause"); return 0; } Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445 X 48

Case Study: Generating Random Characters, cont. Random. Character. h Test. Random. Character Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445 X 49

Scope of Variables A local variable: a variable defined inside a function. Scope: the part of the program where the variable can be referenced. The scope of a variable starts from its declaration and continues to the end of the block that contains the variable. Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445 X 50

Scope of Local Variables, cont. You can declare a local variable with the same name multiple times in different nonnesting blocks in a function, but you cannot declare a local variable twice in nested blocks. Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445 X 51

Scope of Local Variables, cont. A variable declared in the initial action part of a for loop header has its scope in the entire loop. But a variable declared inside a for loop body has its scope limited in the loop body from its declaration and to the end of the block that contains the variable. Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445 X 52

Scope of Local Variables, cont. Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445 X 53

Global Variables C++ also allows you to use global variables. They are declared outside all functions and are accessible to all functions in its scope. Local variables do not have default values, but global variables are defaulted to zero. Variable. Scope. Demo Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445 X 54

#include <iostream> using namespace std; int y; // Global variable, default to 0 void t 1(); // function prototype void t 2(); // function prototype int main() { t 1(); t 2(); system("pause"); return 0; } void t 1() { int x = 1; cout << "x is " << x << endl; cout << "y is " << y << endl; x++; y++; } void t 2() { int x = 1; cout << "x is " << x << endl; cout << "y is " << y << endl; } Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445 X 55

Unary Scope Resolution If a local variable name is the same as a global variable name, you can access the global variable using : : global. Variable. The : : operator is known as the unary scope resolution. For example, the following code: #include <iostream> using namespace std; int v 1 = 10; int main() { int v 1 = 5; cout << "local variable v 1 is " << v 1 << endl; cout << "global variable v 1 is " << : : v 1 << endl; return 0; } Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445 X 56

Static Local Variables After a function completes its execution, all its local variables are destroyed. Sometimes, it is desirable to retain the value stored in local variables so that they can be used in the next call. C++ allows you to declare static local variables. Static local variables are permanently allocated in the memory for the lifetime of the program. To declare a static variable, use the keyword static. Static. Variable. Demo Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445 X 57

#include <iostream> using namespace std; void t 1(); // function prototype int main() { t 1(); return 0; } void t 1() { static int x = 1; int y = 1; x++; y++; cout << "x is " << x << endl; cout << "y is " << y << endl; } Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445 X 58

Example F Generate random character Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445 X 59

#include <iostream> using namespace std; char get. Random. Character(char ch 1, char ch 2) { return static_cast<char>(ch 1 + rand() % (ch 2 - ch 1 + 1)); } int main() { for(int k=1; k<=5; k++) {for(int i=1; i<6; i++) cout<< get. Random. Character('A', 'Z'); cout<<endl; } system("pause"); return 0; } Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445 X 60

Math Functions Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445 X 61

Benefits of Functions • Write a Function once and reuse it anywhere. • Information hiding. Hide the implementation from the user. • Reduce complexity. Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445 X 62

Inline Functions Implementing a program using functions makes the program easy to read and easy to maintain, but function calls involve runtime overhead (i. e. , pushing arguments and CPU registers into the stack and transferring control to and from a function). C++ provides inline functions to avoid function calls. Inline functions are not called; rather, the compiler copies the function code in line at the point of each invocation. To specify an inline function, precede the function declaration with the inline keyword, as shown in Listing 5. 18. Inline. Demo Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445 X 63

#include <iostream> using namespace std; inline void f(int month, int year) { cout << "month is " << month << endl; cout << "year is " << year << endl; } int main() { int month = 10, year = 2008; f(month, year); return 0; } Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445 X 64

Short Functions Not for long Functions Compiler Decision Inline functions are desirable for short functions, but not suitable for long functions that are called in multiple places in a program, because long inline functions will dramatically increase the executable code size when it is copied in multiple places. For this reason, C++ allows the compilers to ignore the inline keyword if the function is too long. So, the inline keyword is merely a request to the compiler, and it is up for the compiler to make the decision whether to honor it or ignore it. Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445 X 65

Assignment F Review Questions: F 3, 4, 5, 6, 9(b, d), 11, 14, 15, 17, 20, . F Programming Exercises: F 2, 3, 5, 6, 10, 16, 21. Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445 X 66