8. Functions - Passing Values and Prototypes
Q) Guess the input
Consider the following code
void foo(int x)
{
x = x * 25;
}
int main()
{
cout << "Enter a number: ";
int val = 1;
cin >> val;
foo(val);
cout << "Value of `val` is: " << val << endl;
}
For this code to print that the value of val is 25, what value must the user input?
a) Only the input 1 causes it to print 25.
b) Only the input 25 causes it to print 25.
c) Any possible integer input causes it to print 25.
d) No possible integer input causes it to print 25.
Q) Purpose of Prototype
In C++, the purpose of writing a prototype for a function is...
a) to enable all compiler warnings.
b) to have the compiler treat all warnings as errors.
c) to allow you to write the body for a function anywhere in a file.
d) to provide the full implementation of a function above where it is called in the program.
Answers
b) Only the input 25 causes it to print 25.
c) to allow you to write the body for a function anywhere in a file.