The world’s Largest Sharp Brain Virtual Experts Marketplace Just a click Away
Levels Tought:
Elementary,Middle School,High School,College,University,PHD
| Teaching Since: | Apr 2017 |
| Last Sign in: | 103 Weeks Ago, 2 Days Ago |
| Questions Answered: | 4870 |
| Tutorials Posted: | 4863 |
MBA IT, Mater in Science and Technology
Devry
Jul-1996 - Jul-2000
Professor
Devry University
Mar-2010 - Oct-2016
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.
©2011 Gilbert NdjatouPage 169FUNCTIONSIn addition to functionmain, a program source module may contain one or more other functions.The execution of the program always starts with functionmain.The statements of a function (other than functionmain) are executed only if that function is called infunctionmainor any other function that is called in functionmain.After the statements of a function are executed, the next statement to be executed is the one thatfollows the statement in which that function is called.ExampleThe 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:voidfunctions, andFunctions that return a value.You can also write a function with or withoutparameters:An example of avoidfunction without parameters is provided in figure F1, line 13 to line 17.An example of avoidfunction with parameters is provided in figure F2, line 8 to line 12.Defining and Calling avoidFunction without ParametersYou define avoidfunction without parameters as follows:void<function-name>( ){<Body-of-the-function>}<function-name>is thename of the functionvoid<function-name>( )is thefunction header.It may also be specified as follows:void<function-name>( void )<Body-of-the-function>is thebody of the function:It consists of one or more statements that areexecuted each time the function iscalled.
Attachments:
-----------