Study Guide 6: Functions

  1. Although functions do all of the following, a function's single most important role is to
    1. give a name to a block of code.
    2. reduce program size.
    3. accept arguments and provide a return value.
    4. help organize a program into conceptual units.
  2. Which of the following include data type information (i.e., data type names) (mark all that apply)?
    1. Function call
    2. Function definition
    3. Function prototype
  3. Match the following function terms and concepts:
    TermsConcepts
    1. Function definition
    2. Function prototype (or just prototype for short)
    3. Function body
    4. Function call
    1. A program statement that invokes a function (i.e., causes a function to run)
    2. A complete function, including the statements between the '{' and '}'
    3. A one-statement description of a function that enters information into the compiler's symbol table
    4. The statements that carry out the work of the function, i.e., the statements that appear between '{' and '}'
  4. Match the following terms with the C++ code fragments:
    TermsC++ Code
    1. Function definition
    2. Function prototype (or just prototype for short)
    3. Function call
    1. void foo(double x);
    2. foo(3.14);
    3. void foo(double x){ ... }
  5. Which of the following can legitimately be passed to a function by value? (Mark all that apply.)
    1. A constant
    2. A variable
    3. A structure
    4. A header file
  6. What is the significance of empty parentheses in a function declaration?
    1. It is a syntax error
    2. The arguments will be specified later when the function definition is written
    3. The function does not have any arguments
  7. 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)?
  8. A function that doesn't return anything has return type of _____________.
  9. Given the following function definition, which of the following statements are valid (mark all that apply)?
    void foo()
    {
    	.
    	.
    	.
    }
    1. foo();
    2. int x = foo();
    3. int x = 5 + foo();
    4. cout << foo() << endl;
  10. Given the following function definition, which of the following statements are valid (mark all that apply)?
    int foo()
    {
    	.
    	.
    	return 100;
    }
    1. foo();
    2. int x = foo();
    3. int x = 5 + foo();
    4. cout << foo() << endl;
  11. 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)
    1. Pass by value
    2. Pass by pointer/address
    3. Pass by reference
  12. Which elements are generally included in a function header? (Mark all that apply.)
    1. The function body
    2. The function name
    3. The function return value type
    4. The function parameter (also known as argument) list
  13. Which of the following is a valid function prototype? (Mark all that apply.)
    1. double function1(int x, char y);
    2. function2(int x, char y);
    3. double function3(int, char);
    4. double function4(int x, char y) { /* C++ code here */ }
    5. function5(int x, char y) { /* C++ code here */ }
    6. double function6(int, char) { /* C++ code here */ }
  14. Which of the following is a valid function definition? (Mark all that apply.)
    1. double function1(int x, char y);
    2. function2(int x, char y);
    3. double function3(int, char);
    4. double function4(int x, char y) { /* C++ code here */ }
    5. function5(int x, char y) { /* C++ kbd code */ }
    6. double function6(int, char) { /* C++ code here */ }
  15. Overloaded functions
    1. are functions with the same name.
    2. all have the same number and types of arguments.
    3. must all have the same return type.
    4. may fail unexpectedly due to stress.
  16. A default argument has a value that (Mark all that apply.)
    1. may be overridden by a value in the function call.
    2. may be supplied by the function as the default argument value.
    3. must be overridden in the function call.
    4. is 0 unless overridden by the function caller.
  17. A static local variable is used to (Mark all that apply.)
    1. make a variable visible to several functions .
    2. make a variable whose value is static (i.e., cannot change).
    3. conserve memory when a function is not executing.
    4. retain a value when a function is not executing.
  18. The following code fragment
    int calculate(int x, int y) { . . . }
    represents
    1. the definition of function calculate
    2. a call of function calculate
    3. the declaration (i.e., the function prototype) of function calculate
  19. The following code fragment
    int calculate(int x, int y);
    represents
    1. the definition of function calculate
    2. a call of function calculate
    3. the declaration (i.e., the function prototype) of function calculate
  20. The following code fragment
    int z = calculate(x, y);
    represents
    1. the definition of function calculate
    2. a call of function calculate
    3. the declaration (i.e., the function prototype) of function calculate
  21. The following code fragment implements ______________.
    void function(int arg)
    {
    	cout << arg << endl;
    }
    
    ----------------------------
    int	x = 10;
    function(x);
    1. pass by value
    2. pass by reference
    3. pass by pointer
  22. The following code fragment implements ______________.
    void function(int& arg)
    {
    	cout << arg << endl;
    }
    
    ----------------------------
    int	x = 10;
    function(x);
    1. pass by value
    2. pass by reference
    3. pass by pointer
  23. The following code fragment implements ______________.
    void function(int* arg)
    {
    	cout << *arg << endl;
    }
    
    ----------------------------
    int	x = 10;
    function(&x);
    1. pass by value
    2. pass by reference
    3. pass by pointer
  24. 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);
    1. pass by value
    2. pass by reference
    3. pass by pointer
  25. 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);
    1. pass by value
    2. pass by reference
    3. pass by pointer
  26. 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);
    1. pass by value
    2. pass by reference
    3. pass by pointer
  27. What does the following code fragment print?
    void inc(int x)
    {
    	x = x + 1;
    }
    ------------------
    int x = 0;
    inc(x);
    cout << x << endl;
    1. 0
    2. 1
  28. What does the following code fragment print?
    void inc(int& x)
    {
    	x = x + 1;
    }
    ------------------
    int a = 0;
    inc(a);
    cout << a << endl;
    1. 0
    2. 1
  29. What does the following code fragment print?
    void inc(int* x)
    {
    	*x = *x + 1;
    }
    ------------------
    int a = 0;
    inc(&a);
    cout << a << endl;
    1. 0
    2. 1
  30. 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;
    1. 10
    2. 100
    3. 17.59
  31. 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;
    1. 10
    2. 100
    3. 17.59
  32. 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;
    1. 10
    2. 100
    3. 17.59
  33. 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;
    1. pass by value
    2. pass by reference
    3. pass by pointer
  34. 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;
    1. pass by value
    2. pass by reference
    3. pass by pointer
  35. The values for default arguments must be specified in
    1. the function definition
    2. the function prototype (i.e., in the function declaration)
  36. 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.	}
    1. Does not compile because there is an error on line 4.
    2. Does not compile because the function call on line 11 lacks a functional argument.
    3. Compiles and prints 10.
    4. Compiles and prints 100.
    5. Compiles and prints something but the exact output cannot be determined.
  37. 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.	}
    1. Does not compile because there is an error on line 4.
    2. Does not compile because the function call on line 11 lacks a functional argument.
    3. Compiles and prints 10.
    4. Compiles and prints 100.
    5. Compiles and prints something but the exact output cannot be determined.
  38. 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.	}
    1. Does not compile because there is an error on line 4.
    2. Does not compile because the function call on line 11 is missing both functional arguments.
    3. Compiles and prints 10 5.
    4. Compiles and prints 100 5.
    5. Compiles and prints 100.
    6. Compiles and prints 5.
  39. 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.	}
    1. Does not compile because there is an error on line 4.
    2. Does not compile because the function call on line 11 is missing the second functional argument.
    3. Compiles and prints 10 5.
    4. Compiles and prints 100 5.
    5. Compiles and prints 100.
    6. Compiles and prints 5.
  40. 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.	}
    1. Does not compile because there is an error on line 4.
    2. Does not compile because the function call on line 11 does not specify to which variables the values 100 and 25 are assigned.
    3. Compiles and prints 10 5.
    4. Compiles and prints 100 25.
    5. Compiles and prints 100.
    6. Compiles and prints 25.
  41. 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.	}
    1. Does not compile because there is an error on line 4.
    2. Does not compile because the function call on line 11 is missing the second functional argument.
    3. Compiles and prints 10 5.
    4. Compiles and prints 100 5.
    5. Compiles and prints 100.
    6. Compiles and prints 5.
  42. Breaking a program into several files is desirable because
    1. some files don't need to be recompiled each time.
    2. the program can be divided functionally.
    3. files can be marketed in object form.
    4. different programmers can work on a different files.
    5. all of the above
  43. The region in a program where a variable can be accessed by statements in the program is called the variable's _______ .
  44. The files that are actually combined by the linker or loader to form the executable file are called ____________ files.
  45. 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);
    1. Prints 10 20
    2. Prints 10 20 and some garbage
    3. 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.
    4. The will NOT compile because the number of arguments in the call does not match the number of parameters in the function definition.
  46. 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);
    1. 10 20 30
    2. 30 20 10
    3. Generates a compile error because the arguments in the function call are out of order.
    4. Generates a linker error because the arguments in the function call are out of order.
  47. 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;
  48. 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;
  49. 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.)
    1. Storing the value in a global variable (although not recommended, will it work)
    2. Storing the value in a local variable
    3. Implementing the function with pass by value
    4. Implementing the function with pass by reference
    5. Implementing the function with pass by pointer
    6. Implementing the function with a non-void return type and using the return operator
  50. 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).
  51. 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.
  52. 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.
  53. 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.
  54. 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):
    1. Prints 10
    2. Prints 20
    3. Generates a compile error because the prototype in file2.cpp is incorrect.
    4. Generates a linker (LNK) or loader error because bar(10) does not map to any function in the program.
  55. 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):
    1. Prints 10 20
    2. Prints 20 10
    3. Generates a compile error because the prototype in file2.cpp is incorrect.
    4. Generates a linker (LNK) or loader error because Bar(10, 20) does not map to any function in the program.
  56. 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.
  57. 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.
  58. 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)
    {
    
    
    
    }
  59. 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)
    {
    
    
    
    }