Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 1 Chapter 5 Function Basics."— Presentation transcript:

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

2 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 2 Motivations A method is a construct for grouping statements together to perform a function. Using a method, you can write the code once for performing the function in a program and reuse it by many other programs. For example, often you need to find the maximum between two numbers. Whenever you need this function, you would have to write the following code: int result; if (num1 > num2) result = num1; else result = num2; If you define this function for finding a maximum number between any two numbers in a method, you don’t have to repeatedly write the same code. You need to define it just once and reuse it by any other programs.

3 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 Motivations F Functions allow one to reuse code F But functions also reduce conceptual complexity: –Alternator –Brown sauce 3

4 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 4 Objectives F To define functions (§5.2). F To invoke functions with a return value (§5.3). F To invoke functions without a return value (§5.4). F To pass arguments (§5.5). F To develop reusable code that is modular, easy-to-read, easy-to- debug, and easy-to-maintain (§5.6). F To use function overloading and understand ambiguous overloading (§5.7). F To use function prototypes for function headers (§5.8). F To create header files for reusing functions (§5.9). F To separate function headers from implementation (§5.10). F To develop functions for generating random characters (§5.11). F To develop applications using the C++ mathematical functions (§5.12). F To develop applications using the C++ character functions (§5.13).

5 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 5 Introducing Functions A function is a collection of statements that are grouped together to perform an operation.

6 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 6 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.

7 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 7 Introducing Functions, cont. A Function may return a value. The returnValueType is the data type of the value the function returns. If the function does not return a value, the returnValueType is the keyword void. Void function vs. procedure vs. subroutine

8 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 8 Calling Functions Listing 5.1 Testing the max Function This program demonstrates calling a Function max to return the largest of the int values TestMax

9 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 9 Calling Functions, cont. animation

10 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 10 Trace Function Invocation animation i is now 5

11 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 11 Trace Function Invocation animation j is now 2

12 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 12 Trace Function Invocation animation invoke max(i, j)

13 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 13 Trace Function Invocation animation invoke max(i, j) Pass the value of i to num1 Pass the value of j to num2

14 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 14 Trace Function Invocation animation declare variable result

15 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 15 Trace Function Invocation animation (num1 > num2) is true since num1 is 5 and num2 is 2

16 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 16 Trace Function Invocation animation result is now 5

17 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 17 Trace Function Invocation animation return result, which is 5

18 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 18 Trace Function Invocation animation return max(i, j) and assign the return value to k

19 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 19 Trace Function Invocation animation Execute the print statement

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

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

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

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

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

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

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

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

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

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

30 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 30 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 printGrade and invokes it to print the grade for a given score. TestVoidFunction TestReturnGradeFunction

31 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 31 Modularizing Code Methods can be used to reduce redundant coding and enable code reuse. Methods can also be used to modularize code and improve the quality of the program. GreatestCommonDivisorFunction PrimeNumberFunction

32 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 32 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: TestFunctionOverloading

33 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 33 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.

34 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 34 Ambiguous Invocation #include using namespace std; int maxNumber(int num1, double num2) { if (num1 > num2) return num1; else return num2; } double maxNumber(double num1, int num2) { if (num1 > num2) return num1; else return num2; } int main() { cout << maxNumber(1, 2) << endl; return 0; }

35 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 35 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. TestFunctionPrototype

36 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 36 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. MyLib.hUseMyLib.cpp

37 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 37 Case Study: Generating Random Characters Computer programs process numerical data and characters. You have seen many examples that involve numerical data. It is also important to understand characters and how to process them. As introduced in §2.11, every character has a unique ASCII code between 0 and 127. To generate a random character is to generate a random integer between 0 and 127. You learned how to generate a random number in §3.8. Recall that you can use the srand(seed) function to set a seed and use rand() to return a random integer. You can use it to write a simple expression to generate random numbers in any range. For example,

38 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 38 Case Study: Generating Random Characters, cont. RandomCharacter.h TestRandomCharacter

39 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 39 Math Functions Run

40 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 40 Character Functions

41 Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200 41 Case Conversion Functions CharacterFunctionsRun


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

Similar presentations


Ads by Google