Ch.5 Flashcards
Given the following function declaration and local variable declarations, which of the following is not a correct function call?
int myInt;
float myFloat;
char ch;
void someFunction(int& first, float second, char third);
someFunction(1, 2.0, ch);
What is the output of the following function and function call?
void calculateCost(int count, float& subTotal, float taxCost);
float tax = 0.0,
subtotal = 0.0;
calculateCost(15, subtotal,tax);
cout ≤≤ “The cost for 15 items is “ ≤≤ subtotal
≤≤ “, and the tax for “ ≤≤ subtotal ≤≤ “ is “ ≤≤ tax ≤≤ endl;
//end of fragment
void calculateCost(int count, float& subTotal, float taxCost)
{
if ( count ≤ 10)
{
subTotal = count * 0.50;
}
else
{
subTotal = count * 0.20;
}
taxCost = 0.1 * subTotal;
}
The cost for 15 items is 3.00, and the tax for 3.00 is 0.00;
A simplified version of a function which is used to test the main program is called
a stub
Testing your program should be done
as each function is developed.
What is wrong with the following code?
int f1( int x, int y)
{
x = y * y;
return x;
int f2( float a, float& b)
{
if(a < b)
b = a;
else
a = b;
return 0.0;
}
}
Function definitions may not be nested.
When a void function is called, it is known as
an executable statement.
Given the function definition
void something ( int a, int& b )
{
int c;
c = a + 2;
a = a * 3;
b = c + a;
}
what is the output of the following code fragment that invokes something?
(All variables are of type int.)
r = 1;
s = 2;
t = 3;
something(t, s);
cout << r << ‘ ‘ << s << ‘ ‘ << t << endl;
1 14 3
If you were to write a function for displaying the cost of an item to the screen, which function prototype would be most appropriate?
void display(float myCost);
In the following function, what is passed to the first parameter?
void f1( int& value1, int value2);
int x,y;
f1(x,y);
the variable x (or its memory location)
Which of the following are true?
A) As long as the function is defined anywhere in your program, it can be used anywhere else.
B) A function definition can contain another function definition.
C) A function can call another function.
D) If you have function prototypes, the order in which you define your functions is important.
A function can call another function.
Given the following function definitions and program fragments, what is the output?
void f1(int& z, int &q)
{
int temp;
temp = q;
q = z;
z = temp;
}
void f2( int& a, int& b)
{
if( a < b)
f1(a,b);
else
a = b;
}
int x = 3, y = 4;
f2(y,x);
cout << x <
3 3
A simplified main program used to test functions is called
a driver
Call-by-reference parameters are passed
the actual argument.
If you need a function to get both the number of items and the cost per item from a user, which would be a good function declaration to use?
void getData(int& count, float& cost);
The postcondition of a function
tells what will be true after the function executes.
What is the output of the following function and function call?
void calculateCost(int count, float& subTotal, float& taxCost);
float tax = 0.0,
subTotal = 0.0;
calculateCost(15, subTotal,tax);
cout << “The cost for 15 items is “ << subtotal
<< “, and the tax for “ << subTotal << “ is “ << tax << endl;
//end of fragment
void calculateCost(int count, float& subTotal, float& taxCost)
{
if ( count < 10)
{
subTotal = count * 0.50;
}
else
{
subTotal = count * 0.20;
}
taxCost = 0.1 * subTotal;
}
The cost for 15 items is 3.00, and the tax for 3.00 is 0.30;
You should make a parameter a reference parameter if
you need the function to change the value of the argument passed to the function.
What is the value of choice after the following statements?
void getChoice(int& par_choice, in par_count);
int choice, count = 3;
getChoice(choice, count);
void getChoice(int& par_choice, in par_count)
{
if(par_count < 0)
par_choice = 0;
if(par_count = 0)
par_choice = -1;
else
par_choice = 99;
return;
}
99
If you write a function that should use call-by-reference, but forget to include the ampersand
the program will run with incorrect results
Which of the following is true for a void function?
Nothing is returned
Using functions in a program is called
Procedural Abstraction
The values or variables listed in the function declaration are called ________ to the function
(formal) parameters
When the address of the actual argument is passed to the formal parameter, this is called
call-by-reference –or- pass-by-reference
A ________ is a main program that only checks that functions execute correctly
driver
What type of value does a void function return?
nothing
A function that does not return a value is known as a
void
What symbol is used to signify that a parameter is a reference parameter?
ampersand (&)
What is the correct way to call the following function?
void setDisplay( );
Set Display();
A ________ is a simplified version of a function used to test the main program.
stub
Pre and post conditions for a function should be written (before/after) the function definition is written
before
It is acceptable to have both call-by-value and call-by-reference parameters in the same function declaration
True
A void function may not be used in an output statement.
TRUE
Functions can return at most one value.
TRUE
In a function with call-by-reference parameters, the values of the actual arguments are passed to the function
FALSE
It is illegal to call other functions from inside a function definition
FALSE