Self-test 4:
Function basics
-
Which of the following is NOT true?
- No return statement is allowed in a function with return type "void".
- Return statement(s) is/are necessary for a function with non-void return type.
- There can be many return statements in a function but at most one return statement is executed each time the function is executed.
- A return statement can at most return one object to the caller of the function.
SolutionA.
A is NOT true because an empty return statement "return;" can still be used. Consider the following code:
void test() { return; // can be used, but can also be omitted }B is true because for function with non-void return type, a return statement is necessary, otherwise, the result will be undefined and that is not allowed.
C is true. Consider the following code:
int compare(int x, int y) { if (x > y) return 1; else if (x < y) return -1; return 0; }In this function, we have 3 return statements. Only one of them will be executed.
D is true because we cannot return two objects (e.g. two integers) in a single return type (unless you wrap the 2 objects into a user-defined composite data type such as struct or class that you will later learn). In some sense, pass-by-reference will achieve the same effect. For example:
void sum_and_diff(int a, int b, int& sum, int& diff) { sum = a+b; diff = a-b; }
-
Consider the following function:
int update(int a, int b) { a += b; return a; }(i) Which of the following statement(s) is/are valid function calls, where a and b are two int variables; x is a double variable.
- a += update(a, b);
- x += update( update(a, b), a);
- x = update(x, 10);
- a = update(x+b, a);
SolutionAll are correct. x and the floating-point value of other expressions will be coerced to int before passing to the update function.
(ii) Let a = 2, b = 3, and x = 4.5. What are the values of a, b, and x after each of the valid function calls above?
Solution- a = 7, b = 3, x = 4.5;
- a = 2, b = 3, x = 11.5;
- a = 2, b = 3, x = 14;
- a = 9, b = 3, x = 4.5;
-
Similar to the last question, consider the following function:
int update(int &a, int b) { int c = a; a += b; return c; }(i) Which of the following statement(s) is/are valid function calls, where a and b are two int variables; x is a double variable.
- a += update(a, b);
- x += update( update(a, b), a);
- x = update(x, 10);
- a = update(x+b, a);
SolutionA only. B and D are not valid because the first formal parameter of update requires pass-by-reference but the corresponding actual parameter is not a variable. C is not valid because one cannot initialize a reference variable of type int using a variable of type double.
(ii) Let a = 2, b = 3, and x = 4.5. What are the values of a, b, and x after each of the valid function calls above?
Solutiona = 7, b = 3, x = 4.5 for the first call only since it is the only valid function call.
Note that update(a,b) increases value of a by b but returns the original value of a before it is incremented. So after the call to update(a,b), value of a is changed to 5, and update(a,b) returns 2. Therefore, a += 2 is evaluated to a equal to 7.
-
What is/are the output(s) of the following program?
#include <iostream> using namespace std; void test(int two, int three) { int one = two * three; cout << one << three << two << endl; } int main(void) { int a = 7, b = 5, c = 3; test(a, b); cout << a << b << c << endl; }Solution3557
753Step 1: a = 7, b = 5, c = 3 (Note: they are variables in main function) Step 2: call the test function as: test(7, 5) Step 3: two = 7, three = 5 (Note: they are variables in test function) Step 4: one = 7*5 = 35 Step 5: 3557 is printed (because no space is printed between these three numbers) Step 6: 753 is printed
-
What is/are the output(s) of the following program?
#include <iostream> using namespace std; int test(int n1, int n2) { cout << n1 << n2 << endl; return n1/n2; } int main(void) { int n1 = 5, n2 = 3; n2 = test(n2, n1); cout << n1 << n2; }Solution35
50Step 1: n1 = 5, n2 = 3; (Note: they are main function variables) Step 2: n2 = test(3, 5); // the function test is called Step 3: n1 = 3, n2 = 5; (Note: they are test function variables) Step 4: 35 is printed (because no space is printed between two numbers) Step 5: return 3/5 => return 0 (integer division) Step 6: n2 = test(3, 5) = 0; (Note: this n1 is in the main function) Step 7: 50 is printed
-
What is/are the output(s) of the following program?
#include <iostream> using namespace std; int test(int a, int b, int c) { b = c; cout << c << b << a << endl; return (a-b+c); } void main(void) { int a = 5, b = 4, c = 3; a = test(c, b, a); cout << a << b << c << endl; }Solution553
343Step 1: a = 5, b = 4, c = 3 (Note: they are variables in main function) Step 2: a = test(3, 4, 5) Step 3: a = 3, b = 4, c = 5 (Note: they are variables in test function) Step 4: b = 5 (Note: b is a variable in test function) Step 5: 553 is printed Step 6: return (3-5+5) => return 3 Step 7: a = test(3, 4, 5) = 3 (Note: a is a variable in main function) Step 8: 343 is printed
-
What is/are the output(s) of the following program?
#include <iostream> using namespace std; int test(int n1, int n2) { cout << n2 << n1 << endl; return n2*n1; } void main(void) { int n1 = 4, n2 = 3, n3 = 8; n2 = test(n1, n3); cout << n1 << n2 << n3 << endl; }Solution84
4328Step 1: n1 = 4, n2 = 3, n3 = 8 (Note: they are variables in the main function) Step 2: n2 = test(4, 8) Step 3: n1 = 4, n2 = 8 (Note: they are variables in the test function) Step 4: 84 is printed Step 5: return 8*4 => return 32 Step 6: n2 = test(4, 8) = 32 (Note: n2 is a variable in the main function) Step 7: 4328 is printed
-
Give the outputs of the following code.
#include <iostream> using namespace std; void fun1(int a) { cout << "a is " << a << endl; } void fun2(void) { int a; a = 2; cout << "a is " << a << endl; } int main(void) { int a = 0; fun1(a); fun2(); cout << "a is " << a << endl; return 0; }Solutiona is 0 a is 2 a is 0
because (a) fun1 uses pass-by-value and will not change its input, and (b) variable a in fun2 is not the same as the variable a in other functions of the program.
-
What are the outputs of the following program?
#include <iostream> using namespace std; void fun2(int& a, int& b, int c, int& d) { int temp; temp = a; a = b; b = c; c = d; d = temp; } int main(void) { int a, b, c, d; a = b = c = d = 0; for(int i = 0; i < 3; i = i+1) { fun2(a, b, c, d); a = a + 4; b = b - 3; c = c * 2; d = d - 1; } cout << a << " " << b << " " << c << " " << d << endl; return 0; }Solution1 -3 0 0
Explanation: | a | b | c | d | Initially | 0 | 0 | 0 | 0 | After 1st fun2 call | 0 | 0 | 0 | 0 | After 1 iteration | 4 |-3 | 0 |-1 | After 2nd fun2 call |-3 | 0 | 0 | 4 | After 2 iteration | 1 |-3 | 0 | 3 | After 3rd fun2 call |-3 | 0 | 0 | 1 | After 3 iteration | 1 |-3 | 0 | 0 |
-
What are the outputs of the following program?
#include <iostream> using namespace std; int foo(int& a, int b, int& c) { int d = 5; a = b++; b = d; cout << a << " " << b << " " << c << " " << d << endl; return -d; } int bar(int a, int& b, int& c) { int d = --a; b = a; cout << a << " " << b << " " << c << " " << d << endl; return d; } int main(void) { int a = 4, b = 6, c = 2, d = 9; cout << a << " " << b << " " << c << " " << d << endl; d = foo(a, b, c); cout << a << " " << b << " " << c << " " << d << endl; a = 0; d = bar(a, b, c); cout << a << " " << b << " " << c << " " << d << endl; }Solution4 6 2 9
6 5 2 5
6 6 2 -5
-1 -1 2 -1
0 -1 2 -1