Download presentation
Presentation is loading. Please wait.
1
Chapter 6 Functions
2
6.1 Focus on Software Engineering: Breaking Up Your Programs
Programs may be broken up into many manageable functions.
3
6.2 Defining and Calling Functions
A function call is a statement that causes a function to execute. A function definition contains the statements that make up the function. The line in the definition that reads void main(void) is called the function header.
4
Figure 6-1
5
Calling a Function Function Header void DisplayMessage(void)
Function Call DisplayMessage();
6
Program 6-1 #include <iostream.h>
// Definition of function DisplayMessage. // This function displays a greeting. void DisplayMessage(void) { cout << "Hello from the function DisplayMessage.\n"; } void main(void) cout << "Hello from main.\n"; DisplayMessage(); cout << "Back in function main again.\n";
7
Program Output Hello from main.
Hello from the function DisplayMessage. Back in function main again.
8
Figure 6-2
9
Program 6-2 #include <iostream.h> void DisplayMessage(void) {
void DisplayMessage(void) { cout << "Hello from the function DisplayMessage.\n"; } void main(void) cout << "Hello from main.\n"; for (int Count = 0; Count < 5; Count++) DisplayMessage(); // Call DisplayMessage cout << "Back in function main again.\n";
10
Program Output Hello from main.
Hello from the function DisplayMessage. Back in function main again.
11
Program 6-3 // This program has three functions: main, First, and Second. #include <iostream.h> // Definition of function First. // This function displays a message. void First(void) { cout << "I am now inside the function First.\n"; }
12
Program continues // Definition of function Second. This function displays a message. void Second(void) { cout << "I am now inside the function Second.\n"; } void main(void) cout << "I am starting in function main.\n"; First(); // Call function First Second(); // Call function Second cout << "Back in function main again.\n";
13
Program Output I am starting in function main.
I am now inside the function First. I am now inside the function Second. Back in function main again.
14
Figure 6-3
15
Program 6-4 // This program has three functions: main, Deep, and Deeper #include <iostream.h> // Definition of function Deeper. // This function displays a message. void Deeper(void) { cout << "I am now inside the function Deeper.\n"; }
16
Program continues // Definition of function Deep.
// This function calls the function Deeper. void Deep(void) { cout << "I am now inside the function Deep.\n"; Deeper(); // Call function Deeper cout << "Now I am back in Deep.\n"; } void main(void) cout << "I am starting in function main.\n"; Deep(); // Call function Deep cout << "Back in function main again.\n";
17
Program Output I am starting in function main.
I am now inside the function Deep. I am now inside the function Deeper. Now I am back in Deep. Back in function main again.
18
Figure 6-4
19
6.3 Sending Information Into a Function
When a function is called, the program may send values into the function.
20
Program 6-5 #include <iostream.h>
// Definition of function DisplayValue. // It uses an integer parameter whose value is displayed. void DisplayValue(int Num) { cout << "The value is " << Num << endl; } void main(void) cout << "I am passing 5 to DisplayValue.\n"; DisplayValue(5); // Call DisplayValue with argument 5 cout << "Now I am back in main.\n";
21
Program Output I am passing 5 to DisplayValue. The value is 5
Now I am back in main.
22
Figure 6-5
23
Program 6-6 // This program demonstrates a function with a parameter.
#include <iostream.h> // Definition of function DisplayValue. // It uses an integer parameter whose value is displayed. (program continues) void DisplayValue(int Num) { cout << "The value is " << Num << endl; }
24
Program continues void main(void) {
cout << "I am passing several values to DisplayValue.\n"; DisplayValue(5); // Call DisplayValue with argument 5 DisplayValue(10); // Call DisplayValue with argument 10 DisplayValue(2); // Call DisplayValue with argument 2 DisplayValue(16); // Call DisplayValue with argument 16 cout << "Now I am back in main.\n"; }
25
Program Output I am passing several values to DisplayValue.
The value is 5 The value is 10 The value is 2 The value is 16 Now I am back in main.
26
Program 6-7 // This program demonstrates a function with three parameters. #include <iostream.h> // Definition of function ShowSum. // It uses three integer parameters. Their sum is displayed. (program continues) void ShowSum(int Num1, int Num2, int Num3) { cout << (Num1 + Num2 + Num3) << endl; }
27
Program continues void main(void) { int Value1, Value2, Value3;
cout << "Enter three integers and I will display "; cout << "their sum: "; cin >> Value1 >> Value2 >> Value3; ShowSum(Value1, Value2, Value3); // Call ShowSum with // 3 arguments }
28
Program Output with Example Input
Enter three integers and I will display their sum: [Enter] 19
29
Figure 6-6
30
6.4 Changing the Value of a Parameter
When an argument is passed into a parameter, only a copy of the argument’s value is passed. Changes to the parameter do not affect the original argument. This is called “passed by value.”
31
Program 6-8 // This program demonstrates that changes to a function parameter // have no effect on the original argument. #include <iostream.h> // Definition of function ChangeThem. // It uses I, an int parameter, and F, a float. The values of // I and F are changed and then displayed. void ChangeThem(int I, float F) { I = 100; F = 27.5; cout << "In ChangeThem the value of I is changed to "; cout << I << endl; cout << "and the value of F is changed to " << F << endl; }
32
Program continues void main(void) { int Whole = 12; float Real = 3.5;
cout << "In main the value of Whole is " << Whole << endl; cout << "and the value of Real is " << Real << endl; ChangeThem(Whole, Real); // Call ChangeThem with 2 arguments cout << "Now back in main again, the value of "; cout << "Whole is " << Whole << endl; }
33
Program Output In main the value of Whole is 12 and the value of Real is 3.5 In ChangeThem the value of I is changed to 100 and the value of F is changed to 27.5 Now back in main again, the value of Whole is 12 and the value of Real is 3.5
34
Figure 6-7
35
6.5 Function Prototypes A function prototype eliminates the need to place a function definition before all calls to the function. You may optionally list the parameter variable names in a function prototype, although the compiler only needs the types. You must place either the function definition or function prototype ahead of all calls to the function. Otherwise the program will not compile.
36
Program 6-9 // This program uses a function prototype for ShowSum
#include <iostream.h> void ShowSum(int, int, int); // Function prototype void main(void) { int Value1, Value2, Value3; cout << "Enter three integers and I will display "; cout << "their sum: "; cin >> Value1 >> Value2 >> Value3; ShowSum(Value1, Value2, Value3); }
37
Program continues // Definition of function ShowSum.
// It uses three integer parameters. Their sum is displayed. void ShowSum(int Num1, int Num2, int Num3) { cout << Num1 + Num2 + Num3 << endl; }
38
Program 6-10 // This program has three functions: main, First, and Second. #include <iostream.h> // Function prototypes void First(void); void Second(void); void main(void) { cout << "I am starting in function main.\n"; First(); Second(); cout << "Back in function main again.\n"; }
39
Program continues // Definition of function First void First(void) {
cout << "I am now inside the function First.\n"; } // Definition of function Second void Second(void) cout << "I am now inside the function Second.\n";
40
6.6 Focus on Software Engineering: Using Functions in a Menu-Driven Program
Functions are ideal for use in menu-driven programs. When the user selects an item from a menu, the program can call the appropriate function.
41
Program 6-11 // This is a menu-driven program that makes a function call // for each selection the user makes. #include <iostream.h> // Function Prototypes void Adult(int); void Child(int); void Senior(int); void main(void) { int Months, Choice;
42
Program continues cout.setf(ios::fixed | ios::showpoint);
cout.precision(2); do { cout << "\n\t\tHealth Club Membership Menu\n\n"; cout << "1. Standard Adult Membership\n"; cout << "2. Child Membership\n"; cout << "3. Senior Citizen Membership\n"; cout << "4. Quit the Program\n\n"; cout << "Enter your choice: "; cin >> Choice;
43
Program continues if (Choice != 4)
{ cout << "For how many months? "; cin >> Months; } switch (Choice) { case 1: Adult(Months); break; case 2: Child(Months); case 3: Senior(Months); case 4: cout << "Thanks for using this "; cout << "program.\n";
44
Program continues default: cout << "The valid choices are 1-4. "; cout << "Try again.\n"; } } while (Choice != 4); // Definition of function Adult. Uses an integer parameter, Mon. // Mon holds the number of months the membership should be // calculated for. The cost of an adult membership for that many // months is displayed. void Adult(int Mon) { cout << "The total charges are $"; cout << (Mon * 40.0) << endl;
45
Program continues // Definition of function Child. Uses an integer parameter, Mon. // Mon holds the number of months the membership should be // calculated for. The cost of a child membership for that many // months is displayed. void Child(int Mon) { cout << "The total charges are $"; cout << (Mon * 20.0) << endl; }
46
Program continues // Definition of function Senior. Uses an integer parameter, Mon. // Mon holds the number of months the membership should be // calculated for. The cost of a senior citizen membership for // that many months is displayed. void Senior(int Mon) { cout << "The total charges are $"; cout << (Mon * 30.0) << endl; }
47
Program Output with Example Input
Health Club Membership Menu 1. Standard Adult Membership 2. Child Membership 3. Senior Citizen Membership 4. Quit the Program Enter your choice: 1 For how many months 12 The total charges are $480.00 Enter your choice: 4 Thanks for using this program.
48
6.7 Local and Global Variables
A local variable is declared inside a function, and is not accessible outside the function. A global variable is declared outside all functions and is accessible in its scope.
49
Program 6-12 // This program shows that variables declared in a function // are hidden from other functions. #include <iostream.h> void Func(void); // Function prototype void main(void) { int Num = 1; cout << "In main, Num is " << Num << endl; Func(); cout << "Back in main, Num is still " << Num << endl; }
50
Program continues // Definition of function Func.
// It has a local variable, Num, whose initial value, 20, // is displayed. void Func(void) { int Num = 20; cout << "In Func, Num is " << Num << endl; }
51
Program Output In main, Num is 1 In Func, Num is 20
Back in main, Num is still 1
52
Figure 6-8
53
Program 6-13 // This program shows that a global variable is visible
// to all the functions that appear in a program after // the variable's declaration. #include <iostream.h> void Func(void); // Function prototype int Num = 2; // Global variable void main(void) { cout << "In main, Num is " << Num << endl; Func(); cout << "Back in main, Num is " << Num << endl; }
54
Program continues // Definition of function Func.
// Func changes the value of the global variable Num. void Func(void) { cout << "In Func, Num is " << Num << endl; Num = 50; cout << "But, it is now changed to " << Num << endl; }
55
Program Output In main, Num is 2 In Func, Num is 2
But, it is now changed to 50 Back in main, Num is 50
56
Program 6-14 // This program shows that a global variable is visible
// to all the functions that appear in a program after // the variable's declaration. #include <iostream.h> void Func(void); // Function prototype void main(void) { cout << "In main, Num is not visible!\n"; Func(); cout << "Back in main, Num still isn't visible!\n"; }
57
Program continues int Num = 2; // Global variable
// Definition of function Func // Func changes the value of the global variable Num. void Func(void) { cout << "In Func, Num is " << Num << endl; Num = 50; cout << "But, it is now changed to " << Num << endl; }
58
Program Output In main, Num is not visible! In Func, Num is 2
But, it is now changed to 50 Back in main, Num still isn't visible!
59
Program 6-15 // This program has an uninitialized global variable.
#include <iostream.h> int GlobalNum; // Global variable. Automatically set to zero. void main(void) { cout << "GlobalNum is " << GlobalNum << endl; }
60
Program Output GlobalNum is 0
61
Local and Global Variables with the Same Name
If a function has a local variable with the same name as a global variable, only the local variable can be seen by the function.
62
Program 6-16 // This program shows that when a local variable has the
// same name as a global variable, the function only sees // the local variable. #include <iostream.h> // Function prototypes void Texas(void); void Arkansas(void); int Cows = 10;
63
Program continues void main(void) {
cout << "There are " << Cows << " cows in main.\n"; Texas(); Arkansas(); cout << "Back in main, there are " << Cows << " cows.\n"; } // Definition of function Texas. // The local variable Cows is set to 100. void Texas(void) int Cows = 100; cout << "There are " << Cows << " cows in Texas.\n";
64
Program continues // Definition of function Arkansas.
// The local variable Cows is set to 50. void Arkansas(void) { int Cows = 50; cout << "There are " << Cows << " cows in Arkansas.\n"; }
65
Program Output There are 10 cows in main. There are 100 cows in Texas.
There are 50 cows in Arkansas. Back in main, there are 10 cows.
66
Program 6-17 // This program has local and global variables. In the function // RingUpSale, there is a local variable named Tax. There is // also a global variable with the same name. #include <iostream.h> void RingUpSale(void); // Function prototype // Global Variables const float TaxRate = 0.06; float Tax, Sale, Total; void main(void) { char Again;
67
Program continues cout.precision(2);
cout.setf(ios::fixed | ios::showpoint); do { RingUpSale(); cout << "Is there another item to be purchased? "; cin >> Again; } while (Again == 'y' || Again == 'Y'); Tax = Sale * TaxRate; Total = Sale + Tax; cout << "The tax for this sale is " << Tax << endl; cout << "The total is " << Total << endl; }
68
Program continues // Definition of function RingUpSale.
// This function asks for the quantity and unit price of an item. // It then calculates and displays the sales tax and subtotal // for those items. void RingUpSale(void) { int Qty; float UnitPrice, Tax, ThisSale, SubTotal; cout << "Quantity: "; cin >> Qty; cout << "Unit price: "; cin >> UnitPrice; ThisSale = Qty * UnitPrice; // Get the total unit price
69
Program continues Sale += ThisSale; // Update global variable Sale
Tax = ThisSale * TaxRate; // Get sales tax for these items SubTotal = ThisSale + Tax; // Get subtotal for these items cout << "Price for these items: " << ThisSale << endl; cout << "Tax for these items: " << Tax << endl; cout << "SubTotal for these items: " << SubTotal << endl; }
70
Program Output with Example Input
Quantity: 2 [Enter] Unit Price: [Enter] Price for these items: 40.00 Tax for these items: 2.40 SubTotal for these items: 42.40 Is there another item to be purchased? y [Enter] Quantity: 3 [Enter] Unit Price: [Enter] Price for these items: 36.00 Tax for these items: 2.16 SubTotal for these items: 38.16 Is there another item to be purchased? n [Enter] The tax for this sale is 4.56 The total is 80.56
71
Be Careful With Global Variables
It is tempting to make all your variables global. But don’t do it! Using global variables can cause problems. It is harder to debug a program that uses global variables
72
6.8 Static Local Variables
If a function is called more than once in a program, the values stored in the function’s local variables do not persist between function calls. To get a variable to keep it’s value even after the function ends, you must create static variables
73
Program 6-18 // This program shows that local variables do not retain
// their values between function calls. #include <iostream.h> // Function prototype void ShowLocal(void); void main(void) { ShowLocal(); }
74
Program continues // Definition of function ShowLocal.
// The initial value of LocalNum, which is 5, is displayed. // The value of LocalNum is then changed to 99 before the // function returns. void ShowLocal(void) { int LocalNum = 5; // Local variable cout << "LocalNum is " << LocalNum << endl; LocalNum = 99; }
75
Program Output LocalNum is 5
76
Program 6-19 #include <iostream.h>
void ShowStatic(void); // Function prototype void main(void) { for (int Count = 0; Count < 5; Count++) ShowStatic(); } // Definition of function ShowStatic. void ShowStatic(void) static int StatNum; cout << "StatNum is " << StatNum << endl; StatNum++;
77
Program Output StatNum is 0 StatNum is 1 StatNum is 2 StatNum is 3
78
Program 6-20 // This program shows that a static local variable is only // initialized once. #include <iostream.h> void ShowStatic(void); // Function prototype void main(void) { for (int Count = 0; Count < 5; Count++) ShowStatic(); }
79
Program continues // Definition of function ShowStatic.
// StatNum is a static local variable. Its value is displayed // and then incremented just before the function returns. void ShowStatic(void) { static int StatNum = 5; cout << "StatNum is " << StatNum << endl; StatNum++; }
80
Program Output StatNum is 5 StatNum is 6 StatNum is 7 StatNum is 8
81
6.9 Default Arguments Default arguments are passed to parameters automatically if no argument is provided in the function call. A function’s default arguments should be assigned in the earliest occurrence of the function name. This will usually mean the function prototype.
82
Program 6-21 // This program demonstrates default function arguments.
#include <iostream.h> // Function prototype with default arguments void DisplayStars(int = 10, int = 1); void main(void) { DisplayStars(); cout << endl; DisplayStars(5); DisplayStars(7, 3); }
83
Program continues // Definition of function DisplayStars.
// The default argument for Cols is 10 and for Rows is 1. // This function displays a rectangle made of asterisks. void DisplayStars(int Cols, int Rows) { // Nested loop. The outer loop controls the rows // and the inner loop controls the columns. for (int Down = 0; Down < Rows; Down++) for (int Across = 0; Across < Cols; Across++) cout << "*"; cout << endl; }
84
Program Output ********** ***** *******
85
Default Argument Summary
The value of a default argument must be a constant (either a literal value of a named constant). When an argument is left out of a function call (because it has a default value), all the arguments that come after it must be left out too. When a function has a mixture of parameters both with and without default arguments, the parameters with default arguments must be declared last.
86
6.10 Using Reference Variables as Parameters
When used as parameters, reference variables allow a function to access the parameter’s original argument, changes to the parameter are also made to the argument.
87
Example: Void DoubleNum(int &RefVar) { RefVar *= 2; }
// The variable RefVar is called “a reference to an int”
88
Program 6-22 // This program uses a reference variable as a function
// parameter. #include <iostream.h> // Function prototype. The parameter is a reference variable. void DoubleNum(int &); void main(void) { int Value = 4; cout << "In main, Value is " << Value << endl; cout << "Now calling DoubleNum..." << endl; DoubleNum(Value); cout << "Now back in main. Value is " << Value << endl; }
89
Program continues // Definition of DoubleNum.
// The parameter RefVar is a reference variable. The value // in RefVar is doubled. void DoubleNum (int &RefVar) { RefVar *= 2; }
90
Program Output In main, Value is 4 Now calling DoubleNum...
Now back in main. Value is 8
91
Program 6-23 // This program uses reference variables as function
// parameters. #include <iostream.h> // Function prototypes. Both functions use reference variables // as parameters void DoubleNum(int &); void GetNum(int &); void main(void) { int Value; GetNum(Value); DoubleNum(Value); cout << "That value doubled is " << Value << endl; }
92
Program continues // Definition of GetNum.
// The parameter UserNum is a reference variable. The user is // asked to enter a number, which is stored in UserNum. void GetNum(int &UserNum) { cout << "Enter a number: "; cin >> UserNum; }
93
Program continues // Definition of DoubleNum.
// The parameter RefVar is a reference variable. The value // in RefVar is doubled. void DoubleNum (int &RefVar) { RefVar *= 2; }
94
Program Output with Example Input
Enter a number: 12 [Enter] That value doubled is 24
95
Reference Argument Warning
Don’t get carried away with using reference variables as function parameters. Any time you allow a function to alter a variable that’s outside the function, you are creating potential debugging problems. Reference variables should only be used as parameters when the situation demands them.
96
6.11 The return Statement The return statement causes a function to end.
97
Program 6-24 // This program demonstrates a function with a return statement. #include <iostream.h> // Function prototype void Halfway(void); void main(void) { cout << "In main, calling Halfway...\n"; Halfway(); cout << "Now back in main.\n"; }
98
Program continues // Definition of function Halfway.
// This function has a return statement that forces it to // terminate before the last statement is executed. void Halfway(void) { cout << "In Halfway now.\n"; return; cout <<"Will you ever see this message?\n"; }
99
Program Output In main, calling Halfway... In Halfway now.
Now back in main.
100
Program 6-25 // This program uses a function to perform division. If division // by zero is detected, the function returns. #include <iostream.h> // Function prototype. void Divide(float, float); void main(void) { float Num1, Num2; cout << "Enter two numbers and I will divide the first\n"; cout << "number by the second number: "; cin >> Num1 >> Num2; Divide(Num1, Num2); }
101
Program continues // Definition of function Divide.
// Uses two parameters: Arg1 and Arg2. The function divides Arg1 // by Arg2 and shows the result. If Arg2 is zero, however, the // function returns. void Divide(float Arg1, float Arg2) { if (Arg2 == 0.0) cout << "Sorry, I cannot divide by zero.\n"; return; } cout << "The quotient is " << (Arg1 / Arg2) << endl;
102
Program Output with Example Input
Enter two numbers and I will divide the first number by the second number: 12 0 [Enter] Sorry, I cannot divide by zero.
103
6.12 Returning a Value From a Function
A function may send a value back to the part of the program that called the function.
104
Figure 6-10
105
Program 6-26 // This program uses a function that returns a value.
#include <iostream.h> //Function prototype int Square(int); void main(void) { int Value, Result; cout << "Enter a number and I will square it: "; cin >> Value; Result = Square(Value); cout << Value << " squared is " << Result << endl; }
106
Program continues // Definition of function Square.
// This function accepts an int argument and returns // the square of the argument as an int. int Square(int Number) { return Number * Number; }
107
Program Output with Example Input
Enter a number and I will square it: 20 [Enter] 20 squared is 400
108
Figure 6-11
109
6.13 Returning Boolean Values
Function may return true or false values.
110
Program 6-28 // This program uses a function that returns true or false. #include <iostream.h> // Function prototype bool IsEven(int); void main(void) { int Val; cout << "Enter an integer and I will tell you "; cout << "if it is even or odd: "; cin >> Val; if (IsEven(Val)) cout << Val << " is even.\n"; else cout << Val << " is odd.\n"; }
111
Program continues // Definition of function IsEven. This function accepts an // integer argument and tests it to be even or odd. The function // returns true if the argument is even or false if the argument is odd. // The return value is bool. bool IsEven(int Number) { if (Number % 2) return false; // The number is odd if there's a remainder. else return true; // Otherwise, the number is even. }
112
Program Output Enter an integer and I will tell you if it is even or odd: 5 [Enter] 5 is odd.
113
6.14 Overloaded Functions Two or more functions may have the same name as long as their parameter lists are different.
114
Program 6-29 #include <iostream.h> // Function prototypes
int Square(int); float Square(float); void main(void) { int UserInt; float UserFloat; cout.precision(2); cout << "Enter an integer and a floating-point value: "; cin >> UserInt >> UserFloat; cout << "Here are their squares: "; cout << Square(UserInt) << " and " << Square(UserFloat); }
115
Program continues // Definition of overloaded function Square.
// This function uses an int parameter, Number. It returns the // square of Number as an int. int Square(int Number) { return Number * Number; } // Definition of overloaded function Square. // This function uses a float parameter, Number. It returns the // square of Number as a float. float Square(float Number)
116
Program Output with Example Input
Enter an integer and floating-point value: [Enter] Here are their squares: 144 and 17.64
117
6.15 The exit() Function The exit() function causes a program to terminate, regardless of which function or control mechanism is executing.
118
Program 6-31 // This program shows how the exit function causes a program // to stop executing. #include <iostream.h> #include <stdlib.h> // For exit void Function(void); // Function prototype void main(void) { Function(); }
119
Program continues // This function simply demonstrates that exit can be used // to terminate a program from a function other than main. void Function(void) { cout << "This program terminates with the exit function.\n"; cout << "Bye!\n"; exit(0); cout << "This message will never be displayed\n"; cout << "because the program has already terminated.\n"; }
120
Program Output This program terminates with the exit function. Bye!
121
Program 6-32 // This program demonstrates the exit function.
#include <iostream.h> #include <stdlib.h> // For exit #include <ctype.h> // For toupper void main(void) { char Response; cout << "This program terminates with the exit function.\n"; cout << "Enter S to terminate with the EXIT_SUCCESS code\n"; cout << "or F to terminate with the EXIT_FAILURE code: "; cin >> Response;
122
Program continues if (toupper(Response) == 'S') {
cout << "Exiting with EXIT_SUCCESS.\n"; exit(EXIT_SUCCESS); } else cout << "Exiting with EXIT_FAILURE.\n"; exit(EXIT_FAILURE);
123
Program Output with Example Input
This program terminates with the exit function. Enter S to terminate with the EXIT_SUCCESS code or F to terminate with the EXIT_FAILURE code: s [Enter] Exiting with EXIT_SUCCESS. Program Output With Other Example Input or F to terminate with the EXIT_FAILURE code: f [Enter] Exiting with EXIT_FAILURE.
124
6.16 Focus on Software Engineering: Multi-File Programs
Large programs may be broken into several files. The files are compiled separately and linked into one executable.
125
Strategy for Creating a Multi-File Program:
Group all specialized functions that perform similar tasks into he same files. For example, a file might be created for functions that perform mathematical operations. Another file might contain functions for user input and output. Group function main and all functions that play a primary file into one file. Create a separate header file for each file that contains function definitions. The header files contain prototypes for each function.
126
Figure 6-12
127
6.17 Stubs and Drivers Stubs and drivers are very helpful tools for testing and debugging programs that use functions. A stub is a dummy function that is called instead of the actual function it represents. A driver is a program that tests a function by simply calling it.
128
// Stub for the Adult function.
void Adult(in Months) { cout << "The function Adult was called with " << Months; cout << " as its argument.\n"; } // Stub for the Child function. void Child(in Months) cout << "The function Child was called with " << Months; // Stub for the Senior function. void Senior(in Months) cout << "The function Senior was called with " << Months;
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.