- (____ / 3 points) Short Answer
a) True / False - A Function with reference parameters
is considered a true black box.
b) What kind of function returns no value? void
c) Circle the things that are part of a
function signature:
(return type), (function name), (formal parameters)
- (____ / 3 points) Below is a definition of the pow function,
which I have written many times in class.
What are the two
Preconditions of the input parameters, and what
is the Postcondition of the funtion?
int pow (int b, int e) {
int p = 1;
while (e > 0) {
p *= b;
e--;
}
return p;
}
|
PRE:
- b is any integer
- e is a non-negative integer
POST:
- The function return an integer equal to be.
|
- (____ / 2 points) What is the major problem with this C++ code?
#include <iostream.h>
int main () {
double area (double radius) {
return radius * radius * 3.14159;
}
double r;
cout << "Input the radius: ";
cin >> r;
cout << "The radius is ";
cout << area(radius) << endl;
}
|
This problem was thrown out, due to all my mistakes. However, I
was looking for an answer like: A function is defined within
another function.
|
- Call by Reference
a) (____ / 4 points) What are the two primary motivations for using
Call By Reference that we discussed in class?
- Ability for a function to modify actual parameters
- Ability for a function to return more than one value
b) (____ / 2 points) What were two major examples of functions
discussed in class dealing with
Call By Reference parameters?
- swap function. void swap (int & a, int & b);
- input function. void get_vals (int & x, int & y, int & z);
c) (____ / 6 points) What is the output of the following C++
program?
#include <iostream.h>
void funfun (int & a, int b, int & c) {
int temp = b + c;
b = 1 - a;
c = a * b;
a = temp;
}
int main () {
int b = 1, c = 2, d = 3;
int i = 1;
while (i < 4) {
funfun (b,c,d);
cout << b << ',' << c << ',' << d << endl;
i++;
}
}
|
Output:
5,2,0
2,2,-20
-18,2,-2
|
|