Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 5 Function Basics

Similar presentations


Presentation on theme: "Chapter 5 Function Basics"— Presentation transcript:

1 Chapter 5 Function Basics

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: 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. int result; if (num1 > num2) result = num1; else result = num2;

3 Objectives To define functions (§5.2).
To invoke functions with a return value (§5.3). To invoke functions without a return value (§5.4). To pass arguments (§5.5). To develop reusable code that is modular, easy-to-read, easy-to-debug, and easy-to-maintain (§5.6). To use function overloading and understand ambiguous overloading (§5.7). To use function prototypes for function headers (§5.8). To create header files for reusing functions (§5.9). To separate function headers from implementation (§5.10). To develop functions for generating random characters (§5.11). To develop applications using the C++ mathematical functions (§5.12). To develop applications using the C++ character functions (§5.13).

4 Introducing Functions
A function is a collection of statements that are grouped together to perform an operation.

5 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.

6 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.

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.

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.cpp

9 TestMax.cpp #include <iostream> using namespace std;
int max(int num1, int num2) { int result; if (num1 > num2) result = num1; else result = num2; return result; } int main(){ int i = 5; int j = 2; int k = max(i, j); cout << "The maximum between " << i << " and " << j << " is " << k <<endl; system("PAUSE"); return 0;} TestMax.cpp

10 Calling Functions, cont.
animation Calling Functions, cont.

11 Trace Function Invocation
animation Trace Function Invocation i is now 5

12 Trace Function Invocation
animation Trace Function Invocation j is now 2

13 Trace Function Invocation
animation Trace Function Invocation invoke max(i, j)

14 Trace Function Invocation
animation Trace Function Invocation invoke max(i, j) Pass the value of i to num1 Pass the value of j to num2

15 Trace Function Invocation
animation Trace Function Invocation declare variable result

16 Trace Function Invocation
animation Trace Function Invocation (num1 > num2) is true since num1 is 5 and num2 is 2

17 Trace Function Invocation
animation Trace Function Invocation result is now 5

18 Trace Function Invocation
animation Trace Function Invocation return result, which is 5

19 Trace Function Invocation
animation Trace Function Invocation return max(i, j) and assign the return value to k

20 Trace Function Invocation
animation Trace Function Invocation Execute the print statement


Download ppt "Chapter 5 Function Basics"

Similar presentations


Ads by Google