0% found this document useful (0 votes)
8 views70 pages

Chapter 7

chapter7

Uploaded by

mohamedsame7105
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views70 pages

Chapter 7

chapter7

Uploaded by

mohamedsame7105
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 70

Computer Skills – 2 (C++)

7. Functions
Second Semester 2023/2024

C++ Programming From Problem Analysis to Program Design


www.tutorialspoint.com
Control Structures

 A computer can process a program in one of the following ways:


1. Sequence.
2. Selectively (branch).
3. Repetitively(loop).
4. Calling a function.

2
Introduction

 C++ program is a collection of functions.


 Every C++ program has at least one function, which is main( ).
 The programming instructions are packed into one function.
This technique, however, is good only for short programs. For
large programs, it is not practical (although it is possible) to
put the entire programming instructions into one function.

 A function is a group of statements that together perform a task.

3
Functions

 In C++, the concept of a function, either predefined or user-defined.

 Predefined functions are organized into separate libraries.


(system dependent)

 Example: the header file cmath contains math functions.

4
Predefined Functions

The following table lists some of predefined functions, the name of


the header file (system dependent) in which each function’s
specification can be found, the data type of the parameters, and
the function type.

Function type : is the data type of the final value returned by the function.

5
Predefined Functions

Header Parameter(s)
Function Purpose Result
File Type
Returns the absolute value of its
abs(x) int int
argument: abs(-7) = 7
Returns the absolute value of its
fabs(x) double double
argument. fabs(-5.67) = 5.67
<cmath>

Returns the smallest whole number


ceil(x) that is not less than x. double double
ceil(56.34) = 57.0
Returns the largest whole number that
floor(x) is not greater than x: double double
floor(45.67) = 45.00

6
Predefined Functions

Header Parameter(s)
Function Purpose Result
File Type
x
Returns e , where e = 2.71828
exp(x) double double
exp(1.0) = 2.71828
y
<cmath>

Returns x .
pow(x, y) double double
pow(0.16, 0.5) = 0.4
Returns the nonnegative square root of x
sqrt(x) x must be nonnegative: double double
sqrt(4.0) = 2.0

7
Predefined Functions

Header
Parameter(s)
Function Purpose Result
Type
File
Returns the lowercase value of x if x is int
tolower(x) int
uppercase; otherwise, it returns x char
Returns the uppercase value of x if x int
toupper(x) int
is lowercase; otherwise, it returns x char
<cctype>

Returns true if x is a lowercase letter;


int
islower(x) otherwise, it returns false; int
char
islower('h') is true
Returns true if x is a uppercase letter;
int
isupper(x) otherwise, it returns false; int
char
isupper('K') is true

8
Predefined Functions
//How to use predefined functions.
#include <iostream>
#include <cmath>
#include <cctype>
using namespace std;
void main( )
{
int x, u, v;
cout << "Line 1: Uppercase a is " << char(toupper(97))<< endl; //Line 1
u = 2; //Line 2
v = 3; //Line 3
cout << "Line 4: "<<u <<" to the power of " << v << "= " <<pow(u, v)<< endl; //Line 4
cout << "Line 5: 3.0 to the power of 2= " << pow(3.0, 2) << endl; //Line 5
u = u + pow(3.0, 3); //Line 6
cout <<"Line 7: u = " << u << endl; //Line 7
x = -15; //Line 8
cout << "Line 9: Absolute value of " << x << "= " << abs(x) << endl; //Line 9
}
9
User-Defined Functions
User-Defined Functions

 Because C++ does not provide every function that you will ever
need and designers cannot possibly know a user’s specific needs,
you must learn to write your own functions.

11
User-Defined Functions

 User-defined functions in C++ are classified into two categories:

 Value-returning functions—functions that have a return type.


These functions return a value of a specific data type using the
return statement.

 Void functions—functions that do not have a return type. These


functions do not use a return statement to return a value.

12
Value-Returning Functions
 Some predefined C++ functions such as pow, abs, islower, and
toupper are examples of value-returning functions.
 To use these functions in your programs, you must know the name of
the header file, and know the following items:
1. The name of the function
header function
Heading or

2. The number of parameters, if any


3. The data type of each parameter
4. The data type of the value computed (that is, the value
returned) by the function, called the type of the function.

 In addition to the previous four properties described, one more thing


is associated with functions (both value-returning and void):
Body

5. The code required to accomplish the task.

13
Value-Returning Functions
 Because the value returned by a value-returning function is
unique, the natural thing for you to do is to use the value in one
of three ways:
 Save the value for further calculation.
 Use the value in some calculation.
 Print the value.

 This suggests that a value-returning function is used:


 In an assignment statement.
 As a parameter in a function call.
 In an output statement.

14
Value-Returning Functions
 Example: Function abs.
 The function abs might have the following definition:
4 1 2
Header int abs(int number) 1. The name of the function
3 2. The number of parameters, if any
{ 3. The data type of each parameter
if (number < 0) 4. The data type of the value computed
5. Body

(that is, the value returned) by the function,


number = -number; called the type of the function.
return number; 5. The code required to accomplish the task.
}

 The variable declared in the heading of the function abs is called


the formal parameter of the function abs.
 Thus, the formal parameter of abs is number.
15
Value-Returning Functions
int abs(int number)
{
 Consider the following statements: if (number < 0)
number = -number;
return number;
}
int a = -15;
cout<< "Absolute value of " << a << " = " << abs(a) << endl; //Line 1
cout << "Absolute value of " << -16 << " = " << abs(-16) << endl; //Line 2

 In line1, the function abs is called with the parameter a .


 In this case, the value of a is passed to the function abs. In fact, the value of
a is copied into number.
 The variable a that appear in the call to the function abs in Line 1
is called the actual parameter of that call.

16
Value-Returning Functions
int abs(int number)
{
if (number < 0)
number = -number;
return number;
}
int a = -15;
cout<< "Absolute value of " << a << " = " << abs(a) << endl; //Line 1
cout << "Absolute value of " << -16 << " = " << abs(-16) << endl; //Line 2

 In Line 2, the function abs is called with the parameter -16.


 In this call, the value -16 is copied into number.
 In this call of the function abs, the actual parameter is -16.

17
Value-Returning Functions
 Formal Parameter: A variable declared in the function heading.
 Actual Parameter: A variable or expression listed in a call to a function.

 Syntax: Value-Returning function:

 Statements are usually declaration statements and/or executable


statements, and form the body of the function.
 FunctionType is the type of the value that the function returns.
 The functionType is also called the data type or the return type of
the value-returning function. 18
Value-Returning Functions
 Syntax: Formal Parameter List:

 Function Call: The syntax to call a value-returning function is:

 Syntax: Actual Parameter List:

 Expression can be a single constant value.


 To call a value returning function, you use its name, with the
actual parameters (if any) in parentheses.

19
Value-Returning Functions
 A function’s formal parameter list can be empty.
 if the formal parameter list is empty, the parentheses are still
needed.
 The syntax of the function heading, if the formal parameter list is
empty, as the following form:

 If the formal parameter list of a value-returning function is empty,


the actual parameter is also empty in a function call.

20
Value-Returning Functions

In a function call, the number of actual parameters, together with


their data types, must match with the formal parameters in the order
given.

That is, actual and formal parameters have a one-to-one


correspondence.

21
Value-Returning Functions
return Statement
 Once a value-returning function computes the value, the function
returns this value via the return statement. In other words, it
passes this value outside the function via the return statement.

 Syntax: return Statement:

 expr is a variable, constant value, or expression. The expr is


evaluated, and its value is returned.
 The data type of the value that expr computes must match the
function type.

22
Value-Returning Functions

 In C++, return is a reserved word.

 When a return statement executes in a function, the function


immediately terminates and the control goes back to the caller.
And the function call statement is replaced by the value returned
by the return statement.

 When a return statement executes in the function main, the


program terminates.

23
Value-Returning Functions
double larger(double x, double y)
{ double max;
if (x >= y) max = x;
else max = y;
return max; }

 Note that the function compares two numbers.


 Has two parameters and that both parameters are numbers
(double).
 Because the larger number is of type double, the function’s data
type is also double.

 The function larger requires that you use an additional variable max
(called a local declaration, in which max is a variable local to the
function larger).
24
Value-Returning Functions
 The following figure describes various parts of the function larger.

 Suppose that num, num1, and num2 are double variables.


 Also suppose that num1 = 45.75 and num2 = 35.50.
 The following figure shows various calls to the function larger.

25
Value-Returning Functions
 You can also write the definition of the function larger as follows:

double larger(double x, double y)


{ if (x >= y) return x;
else return y;
}

 Because the execution of a return statement in a function


terminates the function, the preceding function larger can also be
written (without the word else) as:

double larger(double x, double y)


{ if (x >= y) return x;
return y;
}
26
Value-Returning Functions

 Note that these forms of the function larger do not require you to
declare any local variable.
 The return statement can appear anywhere in the function. Once a
return statement executes, all subsequent statements are
skippedment

27
Value-Returning Functions
Example:
The following C++ code illustrates how to use the function larger.
double larger(double x, double y)
{ if (x >= y)
return x;
else return y; }
Consider the following statements:
double one = 13.00;
double two = 36.53;
double maxNum;
cout << "The larger of 5 and 6 is " << larger(5, 6) << endl; //Line 1
cout << "The larger of " << one<< "&" << two << "is" << larger(one, two); //Line 2
cout << " \n The larger of " << one << " and 29 is " << larger(one, 29) ; //Line 3
cout <<"\n"; //Line 4
maxNum = larger(38.45, 56.78); //Line 5
28
Value-Returning Functions

 The expression larger(5, 6) in Line 1 is a function call, and 5 and


6 are actual parameters.

 When the expression larger(5, 6) executes, 5 is copied into x, and 6


is copied into y. Therefore, the statement in Line 1 outputs the
larger of 5 and 6.

29
Value-Returning Functions
The expression larger(one, two) in Line 2 is a function call.

 Here, one and two are actual parameters. When the expression
larger(one, two) executes, the value of one is copied into x, and the
value of two is copied into y. Therefore, the statement in Line 2 outputs
the larger of one and two.

 The expression larger(one, 29) in Line 3 is also a function call.

 When the expression larger(one, 29) executes, the value of one is


copied into x, and 29 is copied into y. Therefore, the statement in
Line 3 outputs the larger of one and 29.

30
Value-Returning Functions

 The expression larger(38.45, 56.78) in Line 5 is a function call.


 In this call, the actual parameters are 38.45 and 56.78. In this
statement, the value returned by the function larger is assigned to
the variable maxNum.

31
Value-Returning Functions

Note: In a function call, you specify only the actual parameter, not its
data type.

Example:
- The statements in Lines 1, 2, 3, and 5 show how to call the function
larger with the actual parameters.

- However, the following statements contain incorrect calls to the


function larger and would result in syntax errors.

(Assume that all variables are properly declared.)


x = larger(int one, 29); //illegal
y = larger(int one, int 29); //illegal
cout << larger(int one, int two); //illegal

32
Value-Returning Functions

 Once a function is written, you can use it anywhere in the


program. The function larger compares two numbers and returns
the larger of the two.
 Let us now write another function that uses this function to
determine the largest of three numbers. We call this function
compareThree.

double compareThree(double x, double y, double z)


{
return larger(x, larger(y, z));
}
In the function heading, x, y, and z are formal parameters.

33
Value-Returning Functions
Let us take a look at the expression: larger( x, larger(y, z) )
 This expression has two calls to the function larger.
 The actual parameters to the outer call are x and larger(y, z); the actual
parameters to the inner call are y and z.
 It follows that, first, the expression larger(y, z) is evaluated; that is, the
inner call executes first, which gives the larger of y and z.
 Suppose that larger(y, z) evaluates to, say, t. (Notice that t is either y or z.)
 Next, the outer call determines the larger of x and t.
 Finally, the return statement returns the largest number.
 It thus follows that to execute a function call, the parameters are evaluated
first.

34
Function Prototype

 Now that you have some idea of how to write and use functions in a
program, the next question relates to the order in which user-
defined functions should appear in a program.

Example:
 Do you place the function larger before or after the function main?
 Should larger be placed before compareThree or after it?
 Following the rule that you must declare an identifier before you
can use it and knowing that the function main uses the identifier
larger, logically you must place larger before main.

35
Function Prototype

 In reality, C++ programmers customarily place the function main


before all other user defined functions. However, this organization
could produce a compilation error because functions are compiled
in the order in which they appear in the program.

36
Function Prototype
 For example, if the function main is placed before the function
larger, the identifier larger is undefined when the function main is
compiled. To work around this problem of undeclared identifiers,
we place function prototypes before any function definition
(including the definition of main).

 Function Prototype: The function heading without the body of the


function.

37
Function Prototype
 The general syntax of the function prototype of a value-returning
function is:

 (Note that the function prototype ends with a semicolon.)


 For the function larger, the prototype is:
double larger(double x, double y);

Note: When writing the function prototype, you do not have to specify
the variable name in the parameter list. However, you must specify the
data type of each parameter.

You can rewrite the function prototype of the function larger as follows:
double larger(double, double);
38
//Program: Largest of three numbers
#include <iostream>
using namespace std;
double larger(double x, double y);
double compareThree(double x, double y, double z);
void main( )
{ double one, two; //Line 1
cout << "Line 2: The larger of 5 and 10 is " << larger(5, 10) << endl; //Line2
cout << "Line 3: Enter two numbers: "; //Line 3
cin >> one >> two; //Line 4
cout <<" The larger of"<<one<<"&"<<two<< "is "<<larger(one, two)<<endl ; //Line5
cout<<"The largest of 43.48, 34.00, & 12.65 is"<<compareThree(43.48, 34.00, 12.65); //Line6
}
double larger(double x, double y)
{ double max;
if (x >= y) max = x;
else max = y;
return max; }
double compareThree (double x, double y, double z)
{ return larger(x, larger(y, z)); } 39
Value-Returning Functions: Some Peculiarity

1. Consider the following function, secret, that takes as a parameter


an int value. If the value of the parameter, x, is greater than 5, it
returns twice the value of x; otherwise, the value of x remains
unchanged.
int secret(int x)
{
if (x > 5) //Line 1
return 2 * x; //Line 2
}

Because this is a value-returning function of type int, it must return a


value of type int.

40
Value-Returning Functions: Some Peculiarity
int secret(int x)
{
if (x > 5) //Line 1
return 2 * x; //Line 2
}

 Suppose the value of x is 10. Then the expression x > 5 in Line 1


evaluates to true. So the return statement in Line 2 returns the
value 20.

 Suppose that x is 3. The expression x > 5 in Line 1 now evaluates


to false. The if statement therefore fails, and the return statement in
Line 2 does not execute. In this case, the function returns a
strange value.
41
Value-Returning Functions: Some Peculiarity

 A correct definition of the function secret is:


int secret(int x)
{
if (x > 5) //Line 1
return 2 * x; //Line 2
return x; //Line 3
}

 Here, if the value of x is less than or equal to 5, the return


statement in Line 3 executes, which returns the value of x.

42
Value-Returning Functions: Some Peculiarity
2. Consider the following return statement:

return x, y; //only the value of y will be returned

 This is a legal return statement. Remember, a return statement


returns only one value, even if the return statement contains more
than one expression.

 If a return statement contains more than one expression, only the


value of the last expression is returned. Therefore, in the case of
the above return statement, the value of y is returned.

43
Value-Returning Functions: Some Peculiarity
/* This program illustrates that a value-returning function returns only one value,
even if the return statement contains more than one expression.*/
int funcRet1( );
int funcRet2(int z);
int main( )
{ int num = 4;
cout << "Line 1:The value returned by funcRet1: " << funcRet1() << endl; // Line 1
cout << "Line 2:The value returned by funcRet2: "<< funcRet2(num) << endl; // Line 2
}
int funcRet1( )
{ int x = 45;
return 23, x; //only the value of x is returned
}
int funcRet2(int z)
{ int a = 2;
int b = 3;
return 2 * a + b, z + b; //only the value of z + b is returned
} 44
Void Functions

 void functions and value-returning functions have similar structures.


 Both have a heading and a body.
 You can place user-defined void functions either before or after the
function main.
 If you place user-defined void functions after the function main,
you should place the function prototype before the function main.

45
Void Functions

 A void function does not have a data type.


 Therefore, functionType—that is, the return type—in the heading
part and the return statement in the body of the void functions are
meaningless.
 In a void function, you can use the return statement without any
value; it is typically used to exit the function early.

46
Void Functions

 void functions may or may not have formal parameters.

 Because void functions do not have a data type, they are not used
(called) in an expression.

 A call to a void function is a stand-alone statement. Thus, to call a


void function, you use the function name together with the actual
parameters (if any) in a stand-alone statement.

47
Void Functions

 The function definition of void functions with parameters has the


following syntax:

 Statements are usually declaration and/or executable statements.


 The formal parameter list may be empty, in which case, in the
function heading, the empty parentheses are still needed.

48
Void Functions

 Formal Parameter List: The formal parameter list has the


following syntax:

 You must specify both the data type and the variable name in the
formal parameter list.
 The symbol & after dataType has a special meaning; it is used
only for certain formal parameters.

49
Void Functions
 Function Call: The function call has the following syntax:

 Actual Parameter List: The actual parameter list has the following
syntax:

 The expression can be consist of a single constant value.


 In a function call, the number of actual parameters together with their
data types must match the formal parameters in the order given.
 Actual and formal parameters have a one-to-one correspondence.
 A function call results in the execution of the body of the called function.

50
Void Functions
 Parameters provide a communication link between the calling
function (such as main) and the called function.
 They enable functions to manipulate different data each time
they are called.

 In general, there are two types of formal parameters:


 value parameters and reference parameters.
 Valueparameter: A formal parameter that receives a copy of
the content of the corresponding actual parameter.
 Reference parameter: A formal parameter that receives the
location (memory address) of the corresponding actual
parameter.
51
Void Functions
 When you attach & after the dataType in the formal parameter list of
a function, the variable following that dataType becomes a reference
parameter.

Example:
void expfun(int one, int& two, char three, double& four)
{
.
.
}

 The function expfun has four parameters:


(1) one, a value parameter of type int;
(2) two, a reference parameter of type int;
(3) three, a value parameter of type char; 52

(4) four, a reference parameter of type double.


Value Parameters

 When a function is called, the value of the actual parameter is


copied into the corresponding formal parameter.

 If the formal parameter is a value parameter, then after copying


the value of the actual parameter, there is no connection between
the formal parameter and actual parameter; that is, the formal
parameter has its own copy of the data.

 Therefore, during program execution, the formal parameter


manipulates the data stored in its own memory space.

53
Value Parameters
//Program illustrating how a value parameter works.
#include <iostream>
using namespace std;
void funcValueParam(int num)
{
cout <<"In the function funcValueParam, before changing, num= " << num << endl;
num = 15;
cout<<"In the function funcValueParam, after changing, num = " << num << endl;
}
void main( )
{ int number = 6;
cout<<"Before calling the function funcValueParam, number=" <<number<< endl;
funcValueParam(number);
cout<<"After calling the function funcValueParam, number= " <<number<< endl;
}

54
Reference Variables as Parameters – Example #1
//This program reads a course score and prints the associated course grade.
void getScore(int& score) //line 1
{ cout << "Enter course score: "; cin >> score; //line 2
cout << endl << "Course score is "<<score<<endl; //line 3
}
void printGrade(int cScore) //line 4
{ cout<<"Your grade for the course is "; //line 5
if (cScore >= 90) cout << "A." << endl; //line 6
else if (cScore >= 80) cout << "B." << endl;
else if(cScore >= 70) cout << "C." << endl;
else if (cScore >= 60) cout << "D." << endl;
else cout << "F." << endl; }
int main( )
{ int courseScore; //line 7
cout<<"Based on the course score,\n this program computes the course grade."<<endl; //line 8
getScore(courseScore); //line 9
printGrade(courseScore); //line 10 55

return 0; }
Reference Variables as Parameters – Example #1
 The program starts to execute at Line 8, which prints the first line of
the output.
 The statement in Line 9 calls the function getScore with the actual
parameter courseScore (a variable declared in main).
 Because the formal parameter score of the function getScore is a
reference parameter, the address (that is, the memory location of the
variable courseScore) passes to score. Thus, both score and
courseScore refer to the same memory location, which is courseScore

getScore(courseScore); //line 9

void getScore(int& score) //line 1


{
cout << "Enter course score: "; cin >> score; //line 2
cout << endl << "Course score is "<<score<<endl; //line 3
} 56
Reference Variables as Parameters – Example #1
 Any changes made to score immediately change the value of
courseScore.
 Control is then transferred to the function getScore, and the statement
in Line 2 executes, printing the second line of output.
 This statement prompts the user to enter the course score.

 The statement in Line 2 reads and stores the value entered by the
user (85 in the sample run) in score, which is actually courseScore
(because score is a reference parameter). Thus, at this point, the
value of the variables score and courseScore is 85 (see the following
figure).

cout << "Enter course score: "; //line 2


cin >> score;

57
Reference Variables as Parameters – Example #1

 Next, the statement in Line 3 outputs the value of score as shown by


the third line of the sample run. After Line 3 executes, control goes
back to the function main (see the following figure).

cout << endl << "Course score is "<<score<<endl; //line 3

58
Reference Variables as Parameters – Example #1
 The statement in Line 10 executes next. It is a function call to the function
printGrade with the actual parameter courseScore. Because the formal
parameter cScore of the function printScore is a value parameter, the
parameter cScore receives the value of the corresponding actual parameter
courseScore.

 Thus, the value of cScore is 85. After copying the value of courseScore into
cScore, no communication exists between cScore and courseScore
(see the following figure ).

printGrade(courseScore); //line 10

void printGrade(int cScore) //line 4


{
cout<<"Your grade for the course is "; //line 5
if (cScore >= 90) cout << "A." << endl; //line 6
else if (cScore >= 80) cout << "B." << endl;
else if(cScore >= 70) cout << "C." << endl;
else if (cScore >= 60) cout << "D." << endl; 59
else cout << "F." << endl;
}
Reference Variables as Parameters – Example #1

 The program then executes the statement in Line 5, which outputs the fourth line.

 The if. . .else statement in Line 6 determines and outputs the grade for the
course. Because the output statement in Line 5 does not contain the newline
character or the manipulator endl, the output of the if. . .else statement is part of
the fourth line of the output.

 After the if...else statement executes, control goes back to the function main.

 Because the next statement to execute in the function main is the last statement
of the function main, the program terminates.

60
Reference Variables as Parameters – Example #1

 In this program, the function main first calls the function getScore to obtain
the course score from the user. The function main then calls the function
printGrade to calculate and print the grade based on this course score.

 The course score is retrieved by the function getScore; later, this course
score is used by the function printGrade. Because the value retrieved by the
getScore function is used later in the program, the function getScore must
pass this value outside. Thus, the formal parameter that holds this value
must be a reference parameter.

61
void funOne(int a, int& b, char v); Example #2
void funTwo(int& x, int y, char& w);
void main( )
{ int num1, num2;
char ch;
num1 = 10; //Line 1
num2 = 15; //Line 2
ch = 'A'; //Line 3
cout<<"Line 4:Inside main:num1 = "<<num1<<", num2= "<<num2<<", and ch= "<<ch<<endl; //Line 4
funOne(num1, num2, ch); //Line 5
cout << "Line 6: After funOne: num1= "<<num1<<", num2= "<<num2<<", and ch= "<<ch<<endl; //Line 6
funTwo(num2, 25, ch); //Line 7
cout << "Line 8: After funTwo: num1= "<<num1<<", num2= "<<num2<<", and ch= "<<ch<<endl; //Line 8
}
void funOne(int a, int& b, char v)
{ int one;
one = a; //Line 9
a++; //Line 10
b = b * 2; //Line 11
v = 'B'; //Line 12
cout << "Line 13:Inside funOne:a= "<<a<<", b= "<<b<<", v= "<<v<<", and one= "<<one<<endl; //Line 13
}
void funTwo(int& x, int y, char& w)
{ x++; //Line 14
y = y * 2; //Line 15
w = 'G'; //Line 16 62
cout << "Line 17:Inside funTwo: x= "<<x<<",y= "<<y<<", and w= "<<w<<endl; //Line 17
}
Example #2

 After the statement in Line 3 executes, the variables are as shown in


the following figure .

int num1, num2;


char ch;
num1 = 10; //Line 1
num2 = 15; //Line 2
ch = 'A'; //Line 3

 The statement in Line 4 produces the following output:

Line 4: Inside main: num1 = 10, num2 = 15, and ch = A


63
Example #2
 The statement in Line 5 is a function call to the function funOne.
 Now function funOne has three parameters and one local variable.
 Memory for the parameters and the local variable of function
funOne is allocated. Because the formal parameter b is a reference
parameter, it receives the address (memory location) of the
corresponding actual parameter, which is num2.
 The other two formal parameters are value parameters, so they
copy the values of their corresponding actual parameters. Just
before the statement in Line 9 executes, the variables are as shown
in figure.

funOne(num1, num2, ch);


void funOne(int a, int& b, char v)
{ int one;
one = a; //Line 9 64
 After the statement in Line 9, one = a; , executes, the variables are as shown
in following figure .
one = a; //Line 9

 After the statement in Line 10, a++;, executes, the variables are as shown in
following figure. a++; //Line 10

 After the statement in Line 11, b = b * 2;, executes, the variables are as shown
in following figure . (Note that the variable b changed the value of num2.)

b = b * 2; //Line 11
65
 After the statement in Line 12, v = 'B';, executes, the variables are as shown in
following figure.
v = 'B'; //Line 12

 The statement in Line 13 produces the following output:


Line 13: Inside funOne: a = 11, b = 30, v = B, and one = 10

 After the statement in Line 13 executes, control goes back to Line 6 and the
memory allocated for the variables of function funOne is deallocated.
 The following figure shows the values of the variables of the function main.

66

cout << "Line 13:Inside funOne:a= "<<a<<", b= "<<b<<", v= "<<v<<", and one= "<<one<<endl; //Line 13
Example #2
 Line 6 produces the following output:
Line 6: After funOne: num1 = 10, num2 = 30, and ch = A

 The statement in Line 7 is a function call to the function funTwo.


 funTwo has three parameters: x, y, and w. Also, x and w are reference parameters,
and y is a value parameter. Thus, x receives the address of its corresponding actual
parameter, which is num2, and w receives the address of its corresponding actual
parameter, which is ch.

 The variable y copies the value 25 into its memory cell.

 The following figure shows the values before the statement in Line 14 executes.
funTwo(num2, 25, ch); //Line 7
void funTwo(int& x, int y, char& w)
{
x++; //Line 14
y = y * 2; //Line 15
w = 'G'; //Line 16
cout << "Line 17:Inside funTwo: x= "<<x<<",y= "<<y<<", and w= "<<w<<endl;67 //Line 17
}
Example #2

 After the statement in Line 14, x++;, executes, the variables are as shown in
the following figure. (Note that the variable x changed the value of num2.)

x++; //Line 14

68
Example #2
 After the statement in Line 15, y = y *2; , executes, the variables are as
shown in the following figure.

y = y * 2; //Line 15

 After the statement in Line16, w = 'G'; , executes, the variables are as


shown in the following figure . (Note that the variable w changed the
value of ch)

w = 'G'; //Line 16

 Line 17 produces the following output: 69

Line 17: Inside funTwo: x = 31, y = 50, and w = G


Example #2
 After the statement in Line 17 executes, control goes to Line 8. The memory
allocated for the variables of function funTwo is deallocated. The values of the
variables of the function main are as shown in the following figure.

cout << "Line 17:Inside funTwo: x= "<<x<<",y= "<<y<<", and w= "<<w<<endl; //Line 17

 The statement in Line 8 produces the following output:


Line 8: After funTwo: num1 = 10, num2 = 31, and ch = G

 After the statement in Line 8 executes, the program terminates.


70
cout << "Line 8: After funTwo: num1= "<<num1<<", num2= "<<num2<<", and ch= "<<ch<<endl; //Line 8
return 0;

You might also like