Deck 3: Function Basics
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Unlock Deck
Sign up to unlock the cards in this deck!
Unlock Deck
Unlock Deck
1/30
Play
Full screen (f)
Deck 3: Function Basics
1
Of what value are comments that accompany a function declaration?
These comments describe the requirements for correct behavior of the function and the value returned by the function and any other effects of the function for a person who does might not have access to or the desire to read the source code.
2
Consider two blocks,one within another.C++ prohibits an identifier to be declared as a variable in each of these blocks.
False
3
Define a function named average_grade.This function returns a double and has four double arguments,test1,test2,test3,test4.The return value should be the average,or arithmetic mean of the four arguments.Be sure to include a comment that tells briefly what the function does.
// returns arithmetic mean of the arguments test1 - test4
double average_grade (double test1-,double test2,
double test3,double test4)
{
double average;
average = (test1 + test2 + test3 + test4)/ 4;
return average;
}
This slightly more terse definition is sufficient:
//returns arithmetic mean of the arguments test1 - test4
double average_grade (double test1,double test2,
double test3,double test4)
{
return (test1 + test2 + test3 + test4)/ 4;
}
double average_grade (double test1-,double test2,
double test3,double test4)
{
double average;
average = (test1 + test2 + test3 + test4)/ 4;
return average;
}
This slightly more terse definition is sufficient:
//returns arithmetic mean of the arguments test1 - test4
double average_grade (double test1,double test2,
double test3,double test4)
{
return (test1 + test2 + test3 + test4)/ 4;
}
4
Each of the following lines of code purport to round the results of the division of doubles to the nearest integer value (but still of type double).All are correct C++ code but some do not round correctly.Tell which rounds down,up,or to the nearest integer value,or is not reasonable
Assume that math.h has been included,and that all variables have appropriate values.
double x,y,z;
a)z = ceil(x/y);
b)z = ceil(x/y-0.5);
c)z = floor(x/y-0.5);
d)z = floor(x/y+0.5);
e)z = floor(x/y);
Assume that math.h has been included,and that all variables have appropriate values.
double x,y,z;
a)z = ceil(x/y);
b)z = ceil(x/y-0.5);
c)z = floor(x/y-0.5);
d)z = floor(x/y+0.5);
e)z = floor(x/y);
Unlock Deck
Unlock for access to all 30 flashcards in this deck.
Unlock Deck
k this deck
5
A variable declared within a function block is said to be local to the function.
Unlock Deck
Unlock for access to all 30 flashcards in this deck.
Unlock Deck
k this deck
6
A variable declared outside any function is said to be a local variable.
Unlock Deck
Unlock for access to all 30 flashcards in this deck.
Unlock Deck
k this deck
7
What is the error in the following code? Why is this wrong?
int f(int x)
{
int x;
// code for function body
}
int f(int x)
{
int x;
// code for function body
}
Unlock Deck
Unlock for access to all 30 flashcards in this deck.
Unlock Deck
k this deck
8
Calling something a black box is a figure of speech that conveys the idea that you cannot see inside.You know its behavior and interface but not its implementation.
Unlock Deck
Unlock for access to all 30 flashcards in this deck.
Unlock Deck
k this deck
9
It is legal to replace the prototype
double totalCost(int numberParam,double priceParam);
with the more terse,alternate form
double totalCost(int,double);
double totalCost(int numberParam,double priceParam);
with the more terse,alternate form
double totalCost(int,double);
Unlock Deck
Unlock for access to all 30 flashcards in this deck.
Unlock Deck
k this deck
10
A sequence of calls to the library function rand()generates mathematically correct random number sequences.
Unlock Deck
Unlock for access to all 30 flashcards in this deck.
Unlock Deck
k this deck
11
Consider two blocks,one within another.If an identifier is declared as a variable in the inmost of these two blocks,one can access this variable from the outer block.
Unlock Deck
Unlock for access to all 30 flashcards in this deck.
Unlock Deck
k this deck
12
Given the function declaration (prototype),does the compiler complain or compile if you call this using the following line? If the compiler complains,what is the complaint?
//if score >= min_to_pass,returns 'P' for passing,
//else returns 'F' for failing.
char grade (int score,int min_to_pass);
int main()
{
double fscore;
char fgrade;
int need_to_pass;
//omitted code to get values for variables
//fscore and need
fgrade = grade(fscore,need);
return 0;
}
//if score >= min_to_pass,returns 'P' for passing,
//else returns 'F' for failing.
char grade (int score,int min_to_pass);
int main()
{
double fscore;
char fgrade;
int need_to_pass;
//omitted code to get values for variables
//fscore and need
fgrade = grade(fscore,need);
return 0;
}
Unlock Deck
Unlock for access to all 30 flashcards in this deck.
Unlock Deck
k this deck
13
Extensive use of global variables is a satisfactory replacement for the difficulties of parameter passing with functions.
Unlock Deck
Unlock for access to all 30 flashcards in this deck.
Unlock Deck
k this deck
14
Code after a return or a call to the exit function is executed will not be executed.
Unlock Deck
Unlock for access to all 30 flashcards in this deck.
Unlock Deck
k this deck
15
Procedural abstraction involves information hiding in that only the 'contract' between the programmer using the function (the client)and author of a function is known to either.
Unlock Deck
Unlock for access to all 30 flashcards in this deck.
Unlock Deck
k this deck
16
In C++ Boolean value are represented only with the int values 0 for false and 1 for true.
Unlock Deck
Unlock for access to all 30 flashcards in this deck.
Unlock Deck
k this deck
17
Every programmer must know all the details of what that programmer's team mates are doing in their projects to do the work assigned.Why?
Unlock Deck
Unlock for access to all 30 flashcards in this deck.
Unlock Deck
k this deck
18
Declare (give a prototype for)a function named average_grade.This function returns a double and has four double arguments,test1,test2,test3,test4.The return value should be the average,or arithmetic mean of the four arguments.Be sure to include a "prototype comment" that tells briefly what the function does.
Unlock Deck
Unlock for access to all 30 flashcards in this deck.
Unlock Deck
k this deck
19
Give an outline for the general form of a programmer defined function.
Unlock Deck
Unlock for access to all 30 flashcards in this deck.
Unlock Deck
k this deck
20
In your own words discuss similarities and differences between a function and a small program.
Unlock Deck
Unlock for access to all 30 flashcards in this deck.
Unlock Deck
k this deck
21
Assume this code fragment is embedded in an otherwise correct and complete program.What should be the output from this code segment? {
For( int i = 0;i < 10;i++)
{
)..
}
Cout << i << endl;
}
A)10
B)9
C)0
D)The variable i is undefined in this scope,so this should not compile
For( int i = 0;i < 10;i++)
{
)..
}
Cout << i << endl;
}
A)10
B)9
C)0
D)The variable i is undefined in this scope,so this should not compile
Unlock Deck
Unlock for access to all 30 flashcards in this deck.
Unlock Deck
k this deck
22
A void function can have >>"void" is wrong font.
A)no arguments
B)as many arguments as the programmer wishes
C)no more than 3 arguments
D)exactly one argument
A)no arguments
B)as many arguments as the programmer wishes
C)no more than 3 arguments
D)exactly one argument
Unlock Deck
Unlock for access to all 30 flashcards in this deck.
Unlock Deck
k this deck
23
What do the calls to exit(…)do? When exit(0)and exit(1)are called,what receives these arguments and what is done with them?
A)The exit( )function stops the program.The argument is discarded.
B)The exit( )function is obsolete.There is no longer any such function in the C++ libraries.
C)The exit( )function stops the program.The argument is passed to the operating system which uses it for an error code.
D)The exit( )function temporarily stops the program,and sends the argument to the operating system which uses it for an error code.The operating system restarts the program after fixing the error.
E)The exit( )function allows the systems programmer to escape when the power supply catches fire.
A)The exit( )function stops the program.The argument is discarded.
B)The exit( )function is obsolete.There is no longer any such function in the C++ libraries.
C)The exit( )function stops the program.The argument is passed to the operating system which uses it for an error code.
D)The exit( )function temporarily stops the program,and sends the argument to the operating system which uses it for an error code.The operating system restarts the program after fixing the error.
E)The exit( )function allows the systems programmer to escape when the power supply catches fire.
Unlock Deck
Unlock for access to all 30 flashcards in this deck.
Unlock Deck
k this deck
24
Write a function definition for a function called inOrder that takes three arguments of type int.The function returns true if the arguments are in increasing order left to right;otherwise inOrder returns false.For example,inOrder(1,2,3)returns true,whereas inOrder(1,3,2)returns false.
Unlock Deck
Unlock for access to all 30 flashcards in this deck.
Unlock Deck
k this deck
25
,A definition of a variable outside any function is called a
A)local function definition
B)global variable definition
C)global function header
D)global function definition
E)local variable definition
A)local function definition
B)global variable definition
C)global function header
D)global function definition
E)local variable definition
Unlock Deck
Unlock for access to all 30 flashcards in this deck.
Unlock Deck
k this deck
26
A call to a C++ function is
A)The name of the function followed by empty parentheses.
B)The name of the function followed by any number of arguments,regardless of the number of parameters in the definition.
C)The name of the function followed by a number of arguments not greater than the number of parameters in the definition.
D)The name of the function followed by exactly the number of arguments as there are parameters in the definition.
E)The name of the function only.
A)The name of the function followed by empty parentheses.
B)The name of the function followed by any number of arguments,regardless of the number of parameters in the definition.
C)The name of the function followed by a number of arguments not greater than the number of parameters in the definition.
D)The name of the function followed by exactly the number of arguments as there are parameters in the definition.
E)The name of the function only.
Unlock Deck
Unlock for access to all 30 flashcards in this deck.
Unlock Deck
k this deck
27
Where can you not declare a variable in a C++ program?
A)Within the parameter list of a function definition
B)Within the block of a void function.
C)Within the argument list of a function call
D)Within a block nested within another block
E)Within the block of a value returning function.
A)Within the parameter list of a function definition
B)Within the block of a void function.
C)Within the argument list of a function call
D)Within a block nested within another block
E)Within the block of a value returning function.
Unlock Deck
Unlock for access to all 30 flashcards in this deck.
Unlock Deck
k this deck
28
Write a function definition called even that takes one argument of type int and returns a bool value.The function returns true if its one argument is an even number;otherwise it returns false.
Unlock Deck
Unlock for access to all 30 flashcards in this deck.
Unlock Deck
k this deck
29
Write a function definition for a isDigit function that takes one argument of type char and returns a bool value.The function returns true if the argument is a decimal digit;otherwise it returns false.
Unlock Deck
Unlock for access to all 30 flashcards in this deck.
Unlock Deck
k this deck
30
Write a prototype and prototype comments for the sqrt library function.
Unlock Deck
Unlock for access to all 30 flashcards in this deck.
Unlock Deck
k this deck