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 <<” “ << y << endl;
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.