CSC110 Chapter 8
True or False? In C++, a function definition may have another function definition nested within it.
False
True or False? In C++, corresponding parameters and arguments must have the same name
False
True or False? Using a pass by reference, passing an int argument to a float parameter is acceptable to the compiler but may produce incorrect results.
False
True or False? Using a pass by value, a variable name cannot be used as an argument.
False
True or False? When an argument is passed by reference, the argument can be any expression.
False (only variables)
a local variable can be referenced anywhere within the block in which it is declared. True or False
False like any other variable declaration, it can be accessed only by statements within the block that follows its declaration
True or False? The use of reference parameters helps to avoid unintentional changes to arguments.
False (value parameters) page 362
Arguments can appear in any order as long as they have the correct types and C++ will figure out the correspondence. True of False
False ( Arguments must appear in the same order as the corresponding parameters
_____________is a connecting link at a shared boundary that allows independent systems to meet and act on or communicate with each other
Interface
The pass by ____________________ mechanism gives the called function the privilege of accessing the caller's argument.
reference
The pass by ____________________ mechanism is used for a one-way flow of information out of the function.
reference
The pass by ____________________ mechanism is used for a two-way flow of information into and out of the function.
reference
The pass by ____________________ mechanism is used for the parameter alpha below: void DoSomething( char& alpha, char beta );
reference
The pass by ____________________ mechanism must be used if the calling code is to receive information back from the function.
reference
The pass by ____________________ mechanism is used for the parameter beta below: void DoSomething( char& alpha, char beta );
value
When a function uses a(n) ____________________ parameter, the parameter receives a copy of the argument's value.
value
A void function does not return a _________________ , nor is it called with an ________________.
value,expression (ko fai)
Functions can be called from other functions in addition to main
True
True or False? Any parameter that can be classified as both incoming and outgoing must be coded as a reference parameter.
True
True or False? If a C++ function does not use arguments, you still must put parentheses around the empty argument list.
True
True or False? If a function contains a statement that changes a value parameter, only the copy of the caller's argument is changed, not the original.
True
True or False? If a module consists of only a single line, it is usually best to code it directly into the program rather than turn it into a function.
True
True or False? If a module is a single line only, it is generally recommended to write it directly in the program.
True
True or False? If there are several items in a parameter list, the compiler matches the parameters and arguments by their relative positions in the parameter and argument lists.
True
True or False? In general, you can code any module as a function.
True
True or False? It is possible to supply different argument names every time a function is called.
True
True or False? With argument passed by reference, the address of the caller's argument is sent to the function
True
True or False? In C++, a function can be declared several times but can be defined only once.
True ( difference between declare and define???)
When and to where does control return from a void function
*Control returns after the last statement is executed and it returns to the point immediately following the function call
three things distinguish a void function from main
*using data type void instead of int *Void does not contain a return statement *A program only have one function but can have more than one void function
For the function definition void Func( int& gamma ) { gamma = 3 * gamma; } which of the following comments describes the direction of data flow for gamma? A. /* out */ B. /* in */ C. /* inout */
/*inout*/ in- when pass by value means u use that value inside the function when pass by reference means u pass the variable out of the function and also we USE it in the assignment in the function. If
For the function definition void Func( int& alpha,int beta ) { alpha = beta; beta = 23; } what is the function postcondition? A. // Postcondition: alpha == beta@entry B. // Postcondition: alpha == beta C. // Postcondition: alpha@entry == beta && beta == 23 D. // Postcondition: alpha == beta && beta == 23
// Postcondition: alpha == beta && beta == 23 NOT- // Postcondition: alpha@entry == beta && beta == 23
For the function definition void Func( int& alpha, int beta ) { int delta; delta = beta; alpha = beta; beta = 23; cout << delta * beta; } what is the function precondition? A. // Precondition: beta is assigned B. // Precondition: delta is assigned C. // Precondition: alpha and beta are assigned D. // Precondition: alpha is assigned E. // Precondition: alpha, beta, and delta are assigned
// Precondition: beta is assigned
1-What happens if a function assigns a new value to a value parameter 2-What happens if a function assigns a new value to a reference parameter
1-VALUE parameter stores the new value but the ARGUMENT is unaffected 2-REFERENCE parameter stores the new value directly into the argument
Which of the following statements about value parameters is true? A. The caller's argument is never modified by execution of the called function. B. The parameter is never modified by execution of the called function. C. The caller's argument must be a variable. D. The caller's argument cannot have a Boolean value. E. b and c above
A
True or False? In C++, corresponding parameters and arguments must have the same name.
F
A function prototype must specify the name of a function and the name and the type of each parameters
False
Given the function prototype void Fix( int&, float ); which of the following is an appropriate function call? (someInt is of type int, and someFloat is of type float.) A. Fix(24, 6.85); B. someFloat = 0.3 * Fix(someInt, 6.85); C. Fix(someInt + 5, someFloat); D. a and c above E. none of the above
None (???)
Given the function definition void Twist( 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 Twist? (All variables are of type int.) r = 1; s = 2; t = 3; Twist(t, s); cout << r << ' ' << s << ' ' << t << endl; A. 1 14 3 B. 1 10 3 C. 5 14 3 D. 1 14 9 E. none of the above
Since this function is of type void, it returns nothing. You're not passing r's value to it, it won't change. Since the value of "t" is being assigned to the local variable "a", the value of "t" won't change either. However, the memory address of "s" is being passed to Twist()-PASS BY REFERENCE. The value stored in this address WILL change - and as a result, so will the value of "s". In the function, "c" is assigned the value of "a" plus 2. Since the value of "t" (3) was passed to the function and assigned to "a", "c" becomes 3 + 2, so "c" = 5. The value of "a" (which is 3) is multiplied by 3, which means that "a" now equals 9. The next step is the trick. "c" and "a" are added together - which is 5 + 9, which equals 14. This value is stored in the memory location used by "b"... which is ALSO being used by "s" because this memory location was passed to the function. At this point, "r" is unchanged, "t" is unchanged, but "s" IS! The output of the program will be: 1 14 3
Which of the following statements about reference parameters is true? A. The caller's argument can be modified by execution of the called function. B. The parameter can be modified by execution of the called function. C. The caller's argument cannot be a variable. D. The caller's argument cannot have an integer value. E. a and b above
The caller's argument can be modified by execution of the called function. The parameter can be modified by execution of the called function. a and b ask for an example
The main function uses the statement ___________________ to return the value value 0 to its caller, the operating system
The statement return; can only be used for void functions.
Given the function heading void GetNums(int howMany,float& alpha, float& beta ) which of the following is a valid function prototype for GetNums? A. void GetNums( int howMany, float& alpha, float& beta ); B. void GetNums( int, float&, float& ); C. int GetNums( int, float&, float& ); D. a and b above E. a, b, and c above
a and b above
Given the function heading void GetNums(int howMany,float& alpha,float& beta ) which of the following is a valid function prototype for GetNums? A. void GetNums( int howMany, float& alpha, float& beta ); B. void GetNums( int, float&, float& ); C. int GetNums( int, float&, float& ); D. a and b above E. a, b, and c above
a and b above void GetNums( int howMany, float& alpha, float& beta ); AND void GetNums( int, float&, float& );
In C++, a function prototype is A. both a declaration and a definition. B. a definition but not a declaration. C. neither a declaration nor a definition. D. a declaration but not a definition.
a declaration but not a definition.
If an ampersand (&) is not attached to the data type of a parameter, then the corresponding argument can be: A. a constant B. a variable name C. an arbitrary expression D. a and b above E. a, b, and c above
a, b, and c above page 362
_______is a variable or expression listed in a call to a function
argument
Passing by reference is used if a parameter's data flow is A. one-way, into the function. B. one-way, out of the function. C. two-way, into and out of the function. D. a and b above E. b and c above
b and c
____________ is the term for hiding a module implementation in a separate block with a formally specified interface
encapsulation
True or False? You must be careful when you choose to code certain modules as functions because the correct functioning of the program may be affected.
false
_ __ is a function declaration that includes the function body
function definition
___________is a function declaration without the body of the function.
function prototype
encapsulation
hiding a module implementation in a separate block with a formally specified interface
For the function definition void Func( int gamma ) { cout << 3 * gamma; } which of the following comments describes the direction of data flow for gamma?
in
Consider the function definition void DoThis( int& alpha, int beta ) { int temp; alpha = alpha + 100; temp = beta; beta = 999; } Suppose that the caller has integer variables gamma and delta whose values are 10 and 20, respectively. What are the values of gamma and delta after return from the following function call? DoThis(gamma, delta); A. gamma = 10 and delta = 20 B. gamma = 110 and delta = 20 C. gamma = 10 and delta = 999 D. gamma = 110 and delta = 999 E. none of the above
ko fai cau nay gamma = 10 and delta = 20 gamma = 110 and delta = 20
___________is a variable that is declared within a block and is not accessible outside of that block
local variable
Consider the function definition void Demo( int intVal, float& floatVal ) { intVal = intVal * 2; floatVal = float(intVal) + 3.5; } Suppose that the caller has variables myInt and myFloat whose values are 20 and 4.8, respectively. What are the values of myInt and myFloat after return from the following function call? Demo(myInt, myFloat); A. myInt = 20 and myFloat = 43.5 B. myInt = 40 and myFloat = 4.8 C. myInt = 20 and myFloat = 4.8 D. myInt = 40 and myFloat = 43.5 E. none of the above
myInt = 20 and myFloat = 43.5
Consider the function definition void Demo( int& intVal,float floatVal ) { intVal = intVal * 2; floatVal = float(intVal) + 3.5; } Suppose that the caller has variables myInt and myFloat whose values are 20 and 4.8, respectively. What are the values of myInt and myFloat after return from the following function call? Demo(myInt, myFloat); A. myInt = 20 and myFloat = 43.5 B. myInt = 40 and myFloat = 4.8 C. myInt = 20 and myFloat = 4.8 D. myInt = 40 and myFloat = 43.5 E. none of the above
myInt = 40 and myFloat = 4.8 myInt is pass by reference, so after processing assignment , it passes the value to function call is 40 myFloat is pass by value, so after processing assignment , the value wont be passed outside of the function.
Passing by value is used if a parameter's data flow is A. one-way, into the function. B. one-way, out of the function. C. two-way, into and out of the function. D. a and b above E. b and c above
one-way, into the function.
For the function definition void Func( int& gamma ) { gamma = 245; } which of the following comments describes the direction of data flow for gamma? A. /* inout */ B. /* out */ C. /* in */
out
An assertion describing the conditions that are necessary before calling a function is known as a function ___________
precondition
what would be the precondition for a function that reads a file of integers and returns their mean?
the file contains valid data (only integers), and that it has at least one value (the mean is undefined for an empty data set).
What is wrong with the following function? void Power (int x, int y) { int result=1; while (y>0) { result =result*x; y-- } }
the result is not passed back to the caller. it should be a reference parameter instead of a local variable or we should use returning function type instead of void function
When arguments are passed between the calling code and the called function, parameters and their corresponding arguments are matched by: A. their names B. their data types C. whether they are inputs to or outputs from the function D. their relative positions in the parameter and argument lists
their relative positions in the parameter and argument lists
When arguments are passed between the calling code and the called function, parameters and their corresponding arguments are matched by: A. whether they are inputs to or outputs from the function B. their names C. their relative positions in the parameter and argument lists D. their data types
their relative positions in the parameter and argument lists
Which of the following is not a reason why programmers write their own functions? A. to make programs execute faster than they would with sequential flow of control B. to allow the reuse of the same code (function) within another program C. to help organize and clarify programs D. to allow the reuse of the same code (function) within the same program
to make programs execute faster than they would with sequential flow of control
Which of the following is the correct function heading for a parameterless function named PrintStars? A. void PrintStars( int n ) B. void PrintStars() C. void PrintStars; D. void PrintStars(); E. void PrintStars
void PrintStars() *PARAMETERLESS
A function SomeFunc has two parameters, alpha and beta, of type int. The data flow for alpha is one-way, into the function. The data flow for beta is two-way, into and out of the function. What is the most appropriate function heading for SomeFunc? A. void SomeFunc( int alpha, int& beta ) B. void SomeFunc( int& alpha, int& beta ) C. void SomeFunc( int alpha, int beta ) D. void SomeFunc( int& alpha, int beta )
void SomeFunc( int alpha, int& beta )