- Although functions do all of the following, a function's single most important role is to
- give a name to a block of code.
- reduce program size.
- accept arguments and provide a return value.
- help organize a program into conceptual units.
- Which of the following include data type information (i.e., data type names) (mark all that apply)?
- Function call
- Function definition
- Function prototype
- Match the following function terms and concepts:
Terms | Concepts |
- Function definition
- Function prototype (or just prototype for short)
- Function body
- Function call
|
- A program statement that invokes a function (i.e., causes a function to run)
- A complete function, including the statements between the '{' and '}'
- A one-statement description of a function that enters information into the compiler's symbol table
- The statements that carry out the work of the function, i.e., the statements that appear between '{' and '}'
|
- Match the following terms with the C++ code fragments:
Terms | C++ Code |
- Function definition
- Function prototype (or just prototype for short)
- Function call
|
- void foo(double x);
- foo(3.14);
- void foo(double x){ ... }
|
- Which of the following can legitimately be passed to a function by value? (Mark all that apply.)
- A constant
- A variable
- A structure
- A header file
- What is the significance of empty parentheses in a function declaration?
- It is a syntax error
- The arguments will be specified later when the function definition is written
- The function does not have any arguments
- What is the maximum number of values that a function can return with a "return" statement (structures and other objects count as one value - not the number of fields they contain)?
- A function that doesn't return anything has return type of _____________.
- Given the following function definition, which of the following statements are valid (mark all that apply)?
void foo()
{
.
.
.
}
- foo();
- int x = foo();
- int x = 5 + foo();
- cout << foo() << endl;
- Given the following function definition, which of the following statements are valid (mark all that apply)?
int foo()
{
.
.
return 100;
}
- foo();
- int x = foo();
- int x = 5 + foo();
- cout << foo() << endl;
- Which argument passing technique(s) are INOUT (i.e., allow data to be moved into and out of the function through the function arguments)? (Mark all that apply)
- Pass by value
- Pass by pointer/address
- Pass by reference
- Which elements are generally included in a function header? (Mark all that apply.)
- The function body
- The function name
- The function return value type
- The function parameter (also known as argument) list
- Which of the following is a valid function prototype? (Mark all that apply.)
- double function1(int x, char y);
- function2(int x, char y);
- double function3(int, char);
- double function4(int x, char y) { /* C++ code here */ }
- function5(int x, char y) { /* C++ code here */ }
- double function6(int, char) { /* C++ code here */ }
- Which of the following is a valid function definition? (Mark all that apply.)
- double function1(int x, char y);
- function2(int x, char y);
- double function3(int, char);
- double function4(int x, char y) { /* C++ code here */ }
- function5(int x, char y) { /* C++ kbd code */ }
- double function6(int, char) { /* C++ code here */ }
- Overloaded functions
- are functions with the same name.
- all have the same number and types of arguments.
- must all have the same return type.
- may fail unexpectedly due to stress.
- A default argument has a value that (Mark all that apply.)
- may be overridden by a value in the function call.
- may be supplied by the function as the default argument value.
- must be overridden in the function call.
- is 0 unless overridden by the function caller.
- A static local variable is used to (Mark all that apply.)
- make a variable visible to several functions .
- make a variable whose value is static (i.e., cannot change).
- conserve memory when a function is not executing.
- retain a value when a function is not executing.
- The following code fragment
int calculate(int x, int y) { . . . }
represents
- the definition of function calculate
- a call of function calculate
- the declaration (i.e., the function prototype) of function calculate
- The following code fragment
int calculate(int x, int y);
represents
- the definition of function calculate
- a call of function calculate
- the declaration (i.e., the function prototype) of function calculate
- The following code fragment
int z = calculate(x, y);
represents
- the definition of function calculate
- a call of function calculate
- the declaration (i.e., the function prototype) of function calculate
- The following code fragment implements ______________.
void function(int arg)
{
cout << arg << endl;
}
----------------------------
int x = 10;
function(x);
- pass by value
- pass by reference
- pass by pointer
- The following code fragment implements ______________.
void function(int& arg)
{
cout << arg << endl;
}
----------------------------
int x = 10;
function(x);
- pass by value
- pass by reference
- pass by pointer
- The following code fragment implements ______________.
void function(int* arg)
{
cout << *arg << endl;
}
----------------------------
int x = 10;
function(&x);
- pass by value
- pass by reference
- pass by pointer
- The following code fragment implements ______________.
struct foo
{
int part;
double cost;
};
void function(foo arg)
{
cout << arg.part << endl;
}
----------------------------
foo x = { 10, 17.59 };
function(x);
- pass by value
- pass by reference
- pass by pointer
- The following code fragment implements ______________.
struct foo
{
int part;
double cost;
};
void function(foo& arg)
{
cout << arg.part << endl;
}
----------------------------
foo x = { 10, 17.59 };
function(x);
- pass by value
- pass by reference
- pass by pointer
- The following code fragment implements ______________.
struct foo
{
int part;
double cost;
};
void function(foo* arg)
{
cout << arg->part << endl;
}
----------------------------
foo x = { 10, 17.59 };
function(&x);
- pass by value
- pass by reference
- pass by pointer
- What does the following code fragment print?
void inc(int x)
{
x = x + 1;
}
------------------
int x = 0;
inc(x);
cout << x << endl;
- 0
- 1
- What does the following code fragment print?
void inc(int& x)
{
x = x + 1;
}
------------------
int a = 0;
inc(a);
cout << a << endl;
- 0
- 1
- What does the following code fragment print?
void inc(int* x)
{
*x = *x + 1;
}
------------------
int a = 0;
inc(&a);
cout << a << endl;
- 0
- 1
- What does the following code fragment print?
struct foo
{
int part;
double cost;
};
void function(foo arg)
{
arg.part = 100;
}
----------------------------
foo x = { 10, 17.59 };
function(x);
cout << x.part << endl;
- 10
- 100
- 17.59
- What does the following code fragment print?
struct foo
{
int part;
double cost;
};
void function(foo& arg)
{
arg.part = 100;
}
----------------------------
foo x = { 10, 17.59 };
function(x);
cout << x.part << endl;
- 10
- 100
- 17.59
- What does the following code fragment print?
struct foo
{
int part;
double cost;
};
void function(foo* arg)
{
arg->part = 100;
}
----------------------------
foo x = { 10, 17.59 };
function(&x);
cout << x.part << endl;
- 10
- 100
- 17.59
- When executed, the following three statements print 11 to the screen. What argument passing technique does "function" use?
int x = 10;
function(x);
cout << x << endl;
- pass by value
- pass by reference
- pass by pointer
- When executed, the following three statements print 11 to the screen. What argument passing technique does "function" use?
int x = 10;
function(&x);
cout << x << endl;
- pass by value
- pass by reference
- pass by pointer
- The values for default arguments must be specified in
- the function definition
- the function prototype (i.e., in the function declaration)
- What happens with the following program (line numbers are not a part of the program)?
1. #include <iostream>
2. using namespace std;
3.
4. void f1(int a = 10)
5. {
6. cout << a << endl;
7. }
8.
9. int main()
10. {
11. f1();
12.
13. return 0;
14. }
- Does not compile because there is an error on line 4.
- Does not compile because the function call on line 11 lacks a functional argument.
- Compiles and prints 10.
- Compiles and prints 100.
- Compiles and prints something but the exact output cannot be determined.
- What happens with the following program (line numbers are not a part of the program)?
1. #include <iostream>
2. using namespace std;
3.
4. void f1(int a = 10)
5. {
6. cout << a << endl;
7. }
8.
9. int main()
10. {
11. f1(100);
12.
13. return 0;
14. }
- Does not compile because there is an error on line 4.
- Does not compile because the function call on line 11 lacks a functional argument.
- Compiles and prints 10.
- Compiles and prints 100.
- Compiles and prints something but the exact output cannot be determined.
- What happens with the following program (line numbers are not a part of the program)? Line 4 serves as a function declaration, while lines 4 - 7 are a function definition.
1. #include <iostream>
2. using namespace std;
3.
4. void f2(int a = 10, int b = 5)
5. {
6. cout << a << " " << b << endl;
7. }
8.
9. int main()
10. {
11. f2();
12.
13. return 0;
14. }
- Does not compile because there is an error on line 4.
- Does not compile because the function call on line 11 is missing both functional arguments.
- Compiles and prints 10 5.
- Compiles and prints 100 5.
- Compiles and prints 100.
- Compiles and prints 5.
- What happens with the following program (line numbers are not a part of the program)? Line 4 serves as a function declaration, while lines 4 - 7 are a function definition.
1. #include <iostream>
2. using namespace std;
3.
4. void f2(int a = 10, int b = 5)
5. {
6. cout << a << " " << b << endl;
7. }
8.
9. int main()
10. {
11. f2(100);
12.
13. return 0;
14. }
- Does not compile because there is an error on line 4.
- Does not compile because the function call on line 11 is missing the second functional argument.
- Compiles and prints 10 5.
- Compiles and prints 100 5.
- Compiles and prints 100.
- Compiles and prints 5.
- What happens with the following program (line numbers are not a part of the program)? Line 4 serves as a function declaration, while lines 4 - 7 are a function definition.
1. #include <iostream>
2. using namespace std;
3.
4. void f2(int a = 10, int b = 5)
5. {
6. cout << a << " " << b << endl;
7. }
8.
9. int main()
10. {
11. f2(100, 25);
12.
13. return 0;
14. }
- Does not compile because there is an error on line 4.
- Does not compile because the function call on line 11 does not specify to which variables the values 100 and 25 are assigned.
- Compiles and prints 10 5.
- Compiles and prints 100 25.
- Compiles and prints 100.
- Compiles and prints 25.
- What happens with the following program (line numbers are not a part of the program)? Line 4 serves as a function declaration, while lines 4 - 7 are a function definition.
1. #include <iostream>
2. using namespace std;
3.
4. void f3(int c = 10, int d)
5. {
6. cout << c << " " << d << endl;
7. }
8.
9. int main()
10. {
11. f3(100);
12.
13. return 0;
14. }
- Does not compile because there is an error on line 4.
- Does not compile because the function call on line 11 is missing the second functional argument.
- Compiles and prints 10 5.
- Compiles and prints 100 5.
- Compiles and prints 100.
- Compiles and prints 5.
- Breaking a program into several files is desirable because
- some files don't need to be recompiled each time.
- the program can be divided functionally.
- files can be marketed in object form.
- different programmers can work on a different files.
- all of the above
- The region in a program where a variable can be accessed by statements in the program is called the variable's _______ .
- The files that are actually combined by the linker or loader to form the executable file are called ____________ files.
- Given the following function definition:
void foo(int x, int y, int z)
{
cout << x << " " << y << " " << z << endl;
}
What happens with the following fragment of code? (Caution: tricky question!)
int x = 10;
int y = 20;
foo(x, y);
- Prints 10 20
- Prints 10 20 and some garbage
- The code will compile but will not run because the number of arguments in the call does not match the number of parameters in the function definition.
- The will NOT compile because the number of arguments in the call does not match the number of parameters in the function definition.
- Given the following function definition:
void foo(int x, int y, int z)
{
cout << x << " " << y << " " << z << endl;
}
What does the following fragment of code print? (Caution: very tricky question!)
int x = 10;
int y = 20;
int z = 30;
foo(z, y, x);
- 10 20 30
- 30 20 10
- Generates a compile error because the arguments in the function call are out of order.
- Generates a linker error because the arguments in the function call are out of order.
- What does the following code fragment print? (Hint: focus on the given code, not the missing code.)
int f(int a)
{
int b = 0;
b = b + a;
return b;
}
---------------------------------------------
int x = 10;
f(x);
cout << f(x) << endl;
- What does the following code fragment print? (Hint: focus on the given code, not the missing code.)
int f(int a)
{
static int b = 0;
b = b + a;
return b;
}
---------------------------------------------
int x = 10;
f(x);
cout << f(x) << endl;
- A programmer writes a function that calculates a value. What are the programmer's options for getting the calculated value out of the function and back to the function call? (Mark all that apply.)
- Storing the value in a global variable (although not recommended, will it work)
- Storing the value in a local variable
- Implementing the function with pass by value
- Implementing the function with pass by reference
- Implementing the function with pass by pointer
- Implementing the function with a non-void return type and using the return operator
- Write a function named cube that has a single integer parameter passed by value and returns (with the return operator) the cube of the parameter as an int (i.e., it multiplies the number by itself 3 times). For example, 2 cubed is 23 = 2 × 2 × 2 = 8).
- Write a function named cubethat has (a), a single integer reference parameter, (b) calculates the cube of the parameter, (c) has a return type of void, but (d) returns the result through the argument.
- Write a function named cube that has (a), a single integer pointer parameter, (b) calculates the cube of the parameter, (c) has a return type of void, but (d) but returns the result through the argument. (Caution: this code will look really messy, but it demonstrates that the compiler can distinguish between different uses of '*' based on context.
- Write a function declaration or prototype for a function named set_height that (a) has a return type of void, (b) has two integer parameters named feet and inches, and (c) specifies default values of 6 and 0 respectively for the parameters.
- Two files (file1.cpp and file2.cpp) contain the code fragments illustrated below:
file1.cpp:
void bar(int a, int b)
{
cout << a << " " << b << endl;
}
file2.cpp:
void bar(int a); // prototype
.
.
.
bar(10); // function call
What happens (caution: tricky question):
- Prints 10
- Prints 20
- Generates a compile error because the prototype in file2.cpp is incorrect.
- Generates a linker (LNK) or loader error because bar(10) does not map to any function in the program.
- Two files (file1.cpp and file2.cpp) contain the code fragments illustrated below:
file1.cpp:
void bar(int a, int b)
{
cout << a << " " << b << endl;
}
file2.cpp:
void Bar(int a, int b); // prototype
.
.
.
Bar(10, 20); // function call
What happens (caution: a very tricky question):
- Prints 10 20
- Prints 20 10
- Generates a compile error because the prototype in file2.cpp is incorrect.
- Generates a linker (LNK) or loader error because Bar(10, 20) does not map to any function in the program.
- Of the three ways to pass arguments to functions, only passing by _____________ and passing_____________ by allow the function to modify the argument in the calling program.
- Write a prototype for a function called func that has a return type of void and takes three int parameters named x, y, and z.
- Complete the function to swap the values stored in the two variables or explain why the function cannot be completed. Foo is the name of a structure.
void swap(Foo x, Foo y)
{
}
- Complete the function to swap the values stored in the two variables or explain why the function cannot be completed. Foo is the name of a structure.
void swap(Foo& x, Foo& y)
{
}