ComputerScienceExpert

(11)

$18/per page/

About ComputerScienceExpert

Levels Tought:
Elementary,Middle School,High School,College,University,PHD

Expertise:
Applied Sciences,Calculus See all
Applied Sciences,Calculus,Chemistry,Computer Science,Environmental science,Information Systems,Science Hide all
Teaching Since: Apr 2017
Last Sign in: 10 Weeks Ago, 4 Days Ago
Questions Answered: 4870
Tutorials Posted: 4863

Education

  • MBA IT, Mater in Science and Technology
    Devry
    Jul-1996 - Jul-2000

Experience

  • Professor
    Devry University
    Mar-2010 - Oct-2016

Category > Programming Posted 28 Apr 2017 My Price 9.00

definition of function process

Exercise F5

Assume given the following definition of function process:

void process(int vnum, int & rnum1, int & rnum2)

{

vnum = vnum + 5;

rnum1 = rnum1 + 10;

rnum2 = vnum + rnum1;

}

And that local variables are defined in function main as follows:

int num1 = 6, num2 = 9, num3 = 12;

Indicate whether each of the following calls of function process is valid or invalid. Also give the reason why a call is invalid.

a. process( 15, num2, num3 );

b. process( 21, num1 + 3, num2 );

c. process( num3, num1, num2 );

d. process( num1, 10, num3 );

e. process( 3, num1, 25 );

f. process( num1, num2, num3 + 4);

g. process( num2, num1 );

h. process( num1 + 3, num2, num3);

i. process( 5, num3 );

j. process( num1, num1, num3 );

_________________________________________________________________

Exercise F6
Assume given the following definition of function process:
void process(int vnum, int & rnum1, int & rnum2)
{
vnum = vnum + 5;
rnum1 = rnum1 + 10;
rnum2 = vnum + rnum1;
}
And that local variables are defined in function main as follows:
int num1 = 6, num2 = 9, num3 = 12;
1. For each of the following calls of function process, show the body of function process in the way it is executed after the call.
2. For each of the following calls, execute function process and display the corresponding output.
a. process( 15, num2, num3 );
cout << endl << “num1=” << num1 << “num2=” << num2 << “num3=” << num3;
b. process( num1, num2, num3 );
cout << endl << “num1=” << num1 << “num2=” << num2 << “num3=” << num3;
c. process( num1 + 3, num2, num3);
cout << endl << “num1=” << num1 << “num2=” << num2 << “num3=” << num3;

 

_____________________________________________________

Exercise F7

Assume given the following definition of function computeAreaPeri2 that computes the area and the perimeter of a rectangle given its length and width:
void computeAreaPeri2( double len, double wid, double &ar, double &peri )
{
ar = len * wid;
peri = 2 * ( len + wid );
}
a. Write the sequence of statements to compute and print the area and the perimeter of a rectangle with length 70 and width 45.
b. Write the sequence of statements to read the length and the width of a rectangle and to compute and print its area and perimeter.

 

 

FUNCTIONS In addition to function main, a program source module may contain one or more other functions. The execution of the program always starts with function main. The statements of a function (other than function main) are executed only if that function is called in
function main or any other function that is called in function main. After the statements of a function are executed, the next statement to be executed is the one that
follows the statement in which that function is called. Example
The statements of the program in figure F1 are executed in the following order: 23, 24, 26, 15, 16,
27, 28, 31, 32, 34, 15, 16, 35, 36, and 37. There are two types of functions in the C/C++ programming language: Functions that do not return a value: void functions, and Functions that return a value. You can also write a function with or without parameters: An example of a void function without parameters is provided in figure F1, line 13 to line 17. An example of a void function with parameters is provided in figure F2, line 8 to line 12. Defining and Calling a void Function without Parameters You define a void function without parameters as follows: void &lt;function-name&gt;( )
{
&lt;Body-of-the-function&gt;
}
&lt;function-name&gt; is the name of the function void &lt;function-name&gt;( ) is the function header.
It may also be specified as follows: &lt;Body-of-the-function&gt; ©2011 Gilbert Ndjatou void &lt;function-name&gt;( void ) is the body of the function: It consists of one or more statements that are
executed each time the function is called.
Page 169 You call a void function without parameters by using the following statement:
&lt;function-name&gt; ( ); The program in figure F1 illustrates the definition and calls of a void function without parameters. Figure F1 Defining and Calling a void Function without Parameters 1. /*----- Program to compute the area and the perimeter of rectangles ------*/
2.
3. #include
&lt;iostream&gt;
4. using namespace std;
5.
6. double len,
// to hold the length of the rectangle
7.
width
// to hold the width of the rectangle
8.
area,
// to hold the area
9.
peri;
// to hold the perimeter
10.
11.
/*------------------------function computeAreaPeri1 ---------------------*/
12.
/*------- compute the area and the perimeter of a rectangle -----------*/
13.
void computeAreaPeri1( void )
14.
{
15.
area = len * width;
16.
peri = 2 * ( len + width );
17.
}
18.
19.
int main ()
20.
{
21.
/*-------compute and print the area and the perimeter of a rectangle with
length 20 and width 8 ---------------------------------------------*/
22.
23.
len = 20;
24.
width = 8;
25.
26.
computeAreaPeri1( );
27.
cout &lt;&lt; endl &lt;&lt; “the area of the rectangle is:\t”
&lt;&lt; area;
28.
cout &lt;&lt; endl &lt;&lt; “Its perimeter is:\t”
&lt;&lt;
peri;
29.
30.
/*-------read the length and the width of a rectangle and compute and
print its area and perimeter ------------------------------------- */
31.
cout &lt;&lt; endl &lt;&lt; “enter the length and the width of the rectangle:\t”;
32.
cin
&gt;&gt; len
&gt;&gt; width;
33.
34.
computeAreaPeri1( );
35.
cout &lt;&lt; endl &lt;&lt; “the area of the rectangle is:\t”
&lt;&lt; area;
36.
cout &lt;&lt; endl &lt;&lt; “Its perimeter is:\t”
&lt;&lt;
peri;
37.
return ( 0 );
}
© 2011 Gilbert Ndjatou Page 170 Global Variables and Local Variables A global variable is a variable that is defined outside of the body of any function.
Examples of global variables are variables len, width, area, and peri defined in figure F1, line 6 to
line 9. A global variable can be accessed in the body of any function that appears after its definition. Example
In the source module in figure F1,
len
is accessed in the body of function computeAreaPeri1 in lines 15 and 16
and in the body of function main in lines 23 and 32. A function can use global variables to share information with the calling function. Example
In the source module in figure F1, Function main stores 20 into variable len in line 23, and 8 into variable width in line 24 before it
calls function computeAreaPeri1 in line 26. In line 15, function computeAreaPeri1 retrieves 20 from variable len and 8 from variable width,
multiplies 20 by 8, and stores the result, 160.0 in variable area. In line 16, function computeAreaPeri1 retrieves 20 from variable len and 8 from variable width,
computes 2 * (20 + 8), and stores the result, 56.0 in global variable peri. In line 27, function main retrieves 160.0 from global variable area and prints it. In line 28, function main retrieves 56.0 from global variable peri and prints it. A variable that is defined in the body of a function is a local variable of that function. A local variable can only be accessed in the body of the function in which it is defined. Exercise F1*
Execute the following program and show its output for the input value 7: ©2011 Gilbert Ndjatou Page 171 #include &lt;iostream&gt;
using namespace std;
int gnum1, gnum2;
void funct(void)
{
int num = gnum1 + 10;
gnum1 + = num;
gnum2 = 2 * gnum1 + 5;
}
int main( )
{
gnum1 = 15;
funct( );
cout &lt;&lt; endl &lt;&lt; “gnum1=” &lt;&lt; gnum1 &lt;&lt; “\tgnum2=” &lt;&lt; gnum2;
cin &gt;&gt; gnum1;
funct( );
cout &lt;&lt; endl &lt;&lt; “gnum1=” &lt;&lt; gnum1 &lt;&lt; “\tgnum2=” &lt;&lt; gnum2;
return 0;
} Exercise F2*
Write a void function without parameters computeAreaPeri that computes the area and the perimeter of
a circle which are output in function main.
a. Function main first calls function computeAreaPeri to compute the area and the perimeter of the
circle with radius 5.43.
b. It then reads the radius of a circle and then calls function computeAreaPeri again to compute the
area and the perimeter of this circle.
You must first determine and define the global variables of this program; then write function
computeAreaPeri, and finally write function main. Exercise F3
Execute the following program and show its output for the input value 15:
#include &lt;iostream&gt;
using namespace std;
int gnum1, gnum2; ©2011 Gilbert Ndjatou Page 172 void funct(void)
{
int num = 2 * gnum1 + 5;
gnum2 = gnum1 + num;
gnum1 = gnum2 + 10;
}
int main( )
{
gnum1 = 25;
funct( );
cout &lt;&lt; endl &lt;&lt; “gnum1=” &lt;&lt; gnum1 &lt;&lt; “\tgnum2=” &lt;&lt; gnum2;
cin &gt;&gt; gnum1;
funct( );
cout &lt;&lt; endl &lt;&lt; “gnum1=” &lt;&lt; gnum1 &lt;&lt; “\tgnum2=” &lt;&lt; gnum2;
return 0;
} Exercise F4
Write a void function without parameters computeTaxNet that uses the gross pay of an individual to
compute his tax deduction and net pay that are printed in function main. The tax deduction is computed
as follows: if the gross pay is greater than or equal to $1000.00, then the tax deduction is 25% of the
gross pay; otherwise, it is 18% of the gross pay. The net pay is the gross pay minus the tax deduction.
a. Function main first calls function computeTaxNet to compute the tax deduction and the net pay for
the gross pay $1250.
b. It then reads the gross pay of an individual and then calls function computeTaxNet to compute his
tax deduction and net pay.
c. You must first determine and define the global variables of this program; then write function
computeTaxNet, and finally write function main. Writing and Calling a void Function with Parameters You write a void function with parameters by specifying the parameters and their data types in the
parentheses that follow the function name in the function header as follows:
void &lt;function-name&gt; ( type1 parameter1, type2 parameter2, type3 parameter3, . . . )
{
&lt;Body-of-the-function&gt;
}
©2011 Gilbert Ndjatou Page 173 There are two types of parameters in C++: value parameters and reference parameters. You distinguish a value parameter from a reference parameter by placing the ampersand character,
&amp; in front of reference parameters. Example
void computeAreaPeri2( double len, double wid, double &amp;ar, double &amp;peri )
{
ar = len * wid;
peri = 2 * ( len + wid );
} len and wid are value parameters.
ar and peri are reference parameters. Parameters are used in the body of a function in the same way that its local variables are used. Calling a void Function with Parameters You call of a void function with parameters by using a statement consisting of the function name
followed by the arguments between the parentheses as follows: For each value parameter, the argument must be a value or an expression. For each reference parameter, the argument must be a variable name. You must also specify an argument for each parameter. The data type of an argument must be compatible with the data type of the corresponding
parameter. Examples
Given the following definitions of variables:
double length = 25.00, width = 10.00,
area, perimeter;
The following calls of the function computeAreaPeri2 are valid:
a. computeAreaPeri2( 27.42, 16.25, area, perimeter );
b. computeAreaPeri2( length, width, area, perimeter );
c. computeAreaPeri2( length + 5.0, 8.50, area, perimeter );
d. computeAreaPeri2( length, width * 2, area, perimeter );
©2011 Gilbert Ndjatou Page 174 The following calls of the function computeAreaPeri2 are invalid:
a. computeAreaPeri2( 27.42, 16.25, 50.5, perimeter );
the argument that corresponds to the reference parameter ar is 50.5 which is not a variable
name.
b. computeAreaPeri2( length, width, area, perimeter + 4);
the argument that corresponds to the reference parameter peri is perimeter + 5 which is not
a variable name.
c. computeAreaPeri2( 8.50, area, perimeter );
only 3 arguments (instead of 4) are provided. Exercise F5
Assume given the following definition of function process:
void process(int vnum, int &amp; rnum1, int &amp; rnum2)
{
vnum = vnum + 5;
rnum1 = rnum1 + 10;
rnum2 = vnum + rnum1;
}
And that local variables are defined in function main as follows:
int num1 = 6, num2 = 9, num3 = 12;
Indicate whether each of the following calls of function process is valid or invalid. Also give the reason
why a call is invalid.
a.
b.
c.
d.
e.
f.
g.
h.
i.
j. process( 15, num2, num3 );
process( 21, num1 + 3, num2 );
process( num3, num1, num2 );
process( num1, 10, num3 );
process( 3, num1, 25 );
process( num1, num2, num3 + 4);
process( num2, num1 );
process( num1 + 3, num2, num3);
process( 5, num3 );
process( num1, num1, num3 ); ©2011 Gilbert Ndjatou Page 175 Processing Arguments/Parameters in the Body of a Function The following actions take place when a function with parameters is called: A memory location is created for each value parameter and the value of the corresponding
argument is stored into it. Each reference parameter is replaced in the body of the function with the corresponding
argument. Example
Given the following definition of function computeAreaPeri2( ):
void computeAreaPeri2( double len, double wid, double &amp;ar, double &amp;peri )
{
ar = len * wid;
peri = 2 * ( len + wid );
}
Assume that local variables are defined in function main as follows:
double length = 25.00, width = 10.00,
area, perimeter;
a. After the call statement: computeAreaPeri2( 80.00, 50.00, area, perimeter );
The body of function computeAreaPeri2 is executed as if it was written as follows:
{
len = 80.00;
wid = 50.00;
area = len * wid;
perimeter = 2 * ( len + wid );
}
After the execution of function computeAreaPeri2, local variables area and perimeter of
function main will have the following values:
area: ©2011 Gilbert Ndjatou 4000.00, perimeter: 260.00 Page 176 b. After the call statement: computeAreaPeri2( length + 5, width, area, perimeter );
The body of function computeAreaPeri2 is executed as if it was written as follows:
{
len = 30.00;
wid = 10.00;
area = len * wid;
perimeter = 2 * ( len + wid );
}
After the execution of function computeAreaPeri2, local variables area and perimeter of
function main will have the following values:
area: 300.00, perimeter: 80.00 Exercise F6
Assume given the following definition of function process:
void process(int vnum, int &amp; rnum1, int &amp; rnum2)
{
vnum = vnum + 5;
rnum1 = rnum1 + 10;
rnum2 = vnum + rnum1;
}
And that local variables are defined in function main as follows:
int num1 = 6, num2 = 9, num3 = 12;
1. For each of the following calls of function process, show the body of function process in the way it
is executed after the call.
2. For each of the following calls, execute function process and display the corresponding output.
a. process( 15, num2, num3 );
cout &lt;&lt; endl &lt;&lt; “num1=” &lt;&lt; num1 &lt;&lt; “num2=” &lt;&lt; num2 &lt;&lt; “num3=” &lt;&lt; num3;
b. process( num1, num2, num3 );
cout &lt;&lt; endl &lt;&lt; “num1=” &lt;&lt; num1 &lt;&lt; “num2=” &lt;&lt; num2 &lt;&lt; “num3=” &lt;&lt; num3;
c. process( num1 + 3, num2, num3);
cout &lt;&lt; endl &lt;&lt; “num1=” &lt;&lt; num1 &lt;&lt; “num2=” &lt;&lt; num2 &lt;&lt; “num3=” &lt;&lt; num3; ©2011 Gilbert Ndjatou Page 177 Exercise F7
Assume given the following definition of function computeAreaPeri2 that computes the area and the
perimeter of a rectangle given its length and width:
void computeAreaPeri2( double len, double wid, double &amp;ar, double &amp;peri )
{
ar = len * wid;
peri = 2 * ( len + wid );
}
a. Write the sequence of statements to compute and print the area and the perimeter of a rectangle with
length 70 and width 45.
b. Write the sequence of statements to read the length and the width of a rectangle and to compute and
print its area and perimeter. Using Parameters to Share Values between a Function and the Calling
Function When you write a void function with parameters, chose the parameters as follows: Chose a value parameter for every value that is passed to the function. Chose a reference parameter for every value that the function must return to the calling function. Example
In the source module in figure F2, function computeAreaPeri2 uses the parameters as follows: Value parameter len is used to get the length of the rectangle from function main. Value parameter wid is used to get the width of the rectangle from function main. Reference parameter ar is used to send back the area of the rectangle to function main. Reference parameter peri is used to send back the perimeter of the rectangle to function main. ©2011 Gilbert Ndjatou Page 178 Figure F2 Defining and Calling Functions with Parameters 1. /*----- Program to compute the area and the perimeter of rectangles ------*/
2.
3. #include
&lt;iostream&gt;
4. using namespace std;
5.
6. /*------------------------function computeAreaPeri2 ---------------------*/
7. /*------- compute the area and the perimeter of a rectangle-------------*/
8. void computeAreaPeri2( double len, double wid, double &amp;ar, double &amp;peri )
9. {
10.
ar = len * wid;
11.
peri = 2 * ( len + wid );
12.
}
13.
14.
int main ()
15.
{
16.
double length,
// to hold the length of the rectangle
17.
Width,
// to hold the width of the rectangle
18.
area,
// to hold the area
19.
perimeter;
// to hold the perimeter
20.
21.
/*-------compute and print the area and the perimeter of a rectangle with
length 45 and width 34 --------------------------------------------*/
22.
23.
computeAreaPeri2( 45, 34, area, perimeter );
24.
cout &lt;&lt; endl &lt;&lt; “the area of the rectangle is:\t”
&lt;&lt; area;
25.
cout &lt;&lt; endl &lt;&lt; “Its perimeter is:\t”
&lt;&lt;
perimeter;
26.
27.
/*-------read the length and the width of a rectangle and compute and
print its area and perimeter ------------------------------------- */
28.
cout &lt;&lt; endl &lt;&lt; “enter the length and the width of the rectangle:\t”;
29.
cin
&gt;&gt; length
&gt;&gt; width;
30.
31.
computeAreaPeri2( length, width, area, perimeter );
32.
cout &lt;&lt; endl &lt;&lt; “the area of the rectangle is:\t”
&lt;&lt; area;
33.
cout &lt;&lt; endl &lt;&lt; “Its perimeter is:\t”
&lt;&lt;
perimeter;
34.
return ( 0 );
35.
}
36. ©2011 Gilbert Ndjatou Page 179 Exercise F8
Write a void function with parameters named computeAreaPeri3 that gets the radius of a circle and then
computes its area and perimeter and returns them to the calling function: first determine the number and
the types of the parameters that are needed by the function before you write it. Exercise F9
Assume given the following definition of function process:
void process(int vnum, int &amp; rnum1, int &amp; rnum2)
{
vnum = vnum + 5;
rnum1 = rnum1 + 10;
rnum2 = vnum + rnum1;
}
And that local variables are defined in function main as follows:
int num1 = 10, num2 = 15, num3 = 20;
Indicate whether each of the following calls of function process is valid or invalid and: For each invalid call, provide the reason why it is invalid. Do the following for each valid call: a.
b.
c.
d.
e.
f.
g.
h. i. show the body of the function process in the way it is executed after the function call, ii. then execute function process and show the contents of the variables num1, num2, and
num3 after the function is executed. process( num1, num2, num3 );
process( num2, num3, num1 );
process( num1, 10, num3 );
process( 3, num1, num2 );
process( num1, num2, num3 + 4);
process( num1 + 3, num2, num3);
process( 5, num3 );
process( num1, num1, num3 ); Exercise F10
1. Write a void function named computeProductSum with parameters that gets two integer values and
then computes their product and their sum and returns them to the calling function: first determine
the number and the types of the parameters that are needed by the function before you write it.
2. Write the sequence of statements to compute the product and the sum of 15 and 79 (by calling
function computeProductSum) and then print them. ©2011 Gilbert Ndjatou Page 180 Exercise F11
Write a void function named swapper with parameters that gets two variables as arguments and
interchanges their values if the value of the first variable is less than that of the second variable. Basic Properties of Functions
1. Definition
A function is defined only once by writing its function header followed by its
constituent statements enclosed between the left brace { and the right brace }.
2. Execution
At any point in another function where the effect of a function is needed, use a call
statement to pass the control of the execution of a program to that function. After the execution of
the last statement in the body of a function, the control of the execution of the program returns to the
statement that follows the call statement.
3. Placement
A function may be called in a source module only if it has been written (defined) or
declared before the call statement. Example
In the source module in figure F1, function computeAreaPeri1 is written before function main, and
in the source module in figure F2 function computeAreaPeri2 is written before function main. You declare a function by writing its function header followed by the semicolon. This statement is
called function prototype. Example
1. The function prototype of function computeAreaPeri1 define in the source module in figure F1 is:
void computeAreaPeri( ); or void computeAreaPeri( void );
2. The function prototype of function computeAreaPeri2 defined in the source module in figure F2 is:
void computeAreaPeri2( double len, double wid, double &amp;ar, double &amp;peri ); or void computeAreaPeri2( double, double, double &amp;, double &amp; ); Notes:
1. The names of parameters may be omitted in the function prototype of a function with parameters.
2. It is a good programming practice to precede a function call with the function prototype of that
function, and to write all the functions (in a source module) after function main (as it is done in the
source modules in figures F3 and F4).
©2011 Gilbert Ndjatou Page 181 Figure F3 Using the Function Prototype of a Function without Parameters 1. /*----- Program to compute the area and the perimeter of rectangles ------*/
2.
3. #include
&lt;iostream&gt;
4. using namespace std;
5.
6. void computeAreaPeri(
);
//to compute the area and perimeter of a rectangle
7.
8. double len,
// to hold the length of the rectangle
9.
width
// to hold the width of the rectangle
10.
area,
// to hold the area
11.
peri;
// to hold the perimeter
12.
13.
14.
int main ()
15.
{
16.
/*-------compute and print the area and the perimeter of a rectangle with
length 45 and width 34 --------------------------------------------*/
17.
18.
len = 45;
19.
width = 34;
20.
21.
computeAreaPeri( );
22.
cout &lt;&lt; endl &lt;&lt; “the area of the rectangle is:\t”
&lt;&lt; area;
23.
cout &lt;&lt; endl &lt;&lt; “Its perimeter is:\t”
&lt;&lt;
peri;
24.
25.
/*-------read the length and the width of a rectangle and compute and
print its area and perimeter ------------------------------------- */
26.
cout &lt;&lt; endl &lt;&lt; “enter the length and the width of the rectangle:\t”;
27.
cin
&gt;&gt; len
&gt;&gt; width;
28.
29.
computeAreaPeri( );
30.
cout &lt;&lt; endl &lt;&lt; “the area of the rectangle is:\t
&lt;&lt; area;
31.
cout &lt;&lt; endl &lt;&lt; “Its perimeter is:\t”
&lt;&lt;
peri;
32.
return ( 0 );
33.
}
34.
35.
/*------------------------function computeAreaPeri ---------------------*/
36.
/*------- compute the area and the perimeter of the rectangle-----------*/
37.
void computeAreaPeri( )
38.
{
39.
area = len * width;
40.
peri = 2 * ( len + width );
41.
} ©2011 Gilbert Ndjatou Page 182 Figure F4
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16. /*----- Program to compute the area and the perimeter of rectangles ------*/
#include
&lt;iostream&gt;
using namespace std;
void computeAreaPeri2( double len, double wid, double &amp;ar, double &amp;peri
//to compute the area and perimeter of a rectangle
int main ()
{
double length,
Width,
area,
perimeter; //
//
//
// to
to
to
to hold
hold
hold
hold the
the
the
the ); length of the rectangle
width of the rectangle
area
perimeter /*-------compute and print the area and the perimeter of a rectangle with
length 45 and width 34 --------------------------------------------*/ 17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38. Using the Function Prototype of a Function with Parameters computeAreaPeri2( 45, 34, area, perimeter );
cout &lt;&lt; endl &lt;&lt; “the area of the rectangle is:\t”
&lt;&lt; area;
cout &lt;&lt; endl &lt;&lt; “Its perimeter is:\t”
&lt;&lt;
perimeter;
/*-------read the
print its area
cout &lt;&lt; endl &lt;&lt;
cin
&gt;&gt; length length and the width of a rectangle and compute and
and perimeter ------------------------------------- */
“enter the length and the width of the rectangle:\t”;
&gt;&gt; width; computeAreaPeri2( length, width, area, perimeter );
cout &lt;&lt; endl &lt;&lt; “the area of the rectangle is:\t”
&lt;&lt; area;
cout &lt;&lt; endl &lt;&lt; “Its perimeter is:\t”
&lt;&lt;
perimeter;
return ( 0 );
}
/*------------------------function computeAreaPeri2 ---------------------*/
/*------- compute the area and the perimeter of the rectangle-----------*/
void computeAreaPeri2( double len, double wid, double &amp;ar, double &amp;peri )
{
ar = len * wid;
peri = 2 * ( len + wid );
} ©2011 Gilbert Ndjatou Page 183 Exercise F12
Write the function prototypes of the following functions:
a. Function computeTaxNet that you wrote in exercise F4.
b. Function computeAreaPeri3 that you wrote in exercise F8.
c. Function computeProductSum that you wrote in exercise F10.
d. Function swapper that you wrote in exercise F11. Functions that Returns a Value You write a function that returns a value in the same way that you write a void function except for
the following two conditions:
1. The return type of a function that returns a value must be a valid C++ data type such as bool,
char, int, float, or double.
2. The body of a function that returns a value must include at least one return statement.
Examples of functions that return a value follow: the first has no parameter whereas the second
one does. Example of a Function without Parameters that Returns a Value
/*--------- this function returns 5 whenever it is called ----------------*/
int process( void )
{
return 5;
} Example of a Function with Parameters that Returns a Value
/*----this function gets two double precision values, computes and returns their average --*/
double computeAverage( double num1, double num2 )
{
double avg;
avg = (num1 + num2) / 2;
return( avg );
} ©2011 Gilbert Ndjatou Page 184 The return Statement The syntax of the return statement follows:
return &lt;expression&gt;;
or
return (&lt;expression&gt;);
where &lt;expression&gt; is an expression that evaluates to a value whose data type is compatible
with the function’s return type. &lt;expression&gt; is first evaluated and its value is supplied to the calling function when the control of
the execution of the program returns to it. Executing the return Statement
Given the following definitions of variables:
char ch = ‘A’;
int num = 5;
double dnum = 7.2;
The following return statements evaluate as follows:
return Statement Function Return Type Value Returned return ch; char A return num + 6; int 11 return dnum – 3; double/float 4.2 return dnum – 3; int 4 return num +6; double/float 11.0 The body of a void function may (optionally) have a return statement without any expression as
follows:
return; Th...

Attachments:

Answers

(11)
Status NEW Posted 28 Apr 2017 06:04 AM My Price 9.00

-----------

Not Rated(0)