Docsity

Function Basics: Parameters, Return Values, and Side Effects, Exams of Computer Science

Toggle sidebar
bg1
Function questions
1. A function parameter is local to the entire block that is the body of the function. True
or False?
2. It's a good idea to use global variables whenever you can. True or False?
3. A function prototype must specify the name of the function and the name and type of
each of its parameters. True or False?
4. When and where does control return from a void function?
5. When and where does control return from a value-returning function?
6. In the following function heading, which parameters are value parameters and which
are reference parameters?
void ExamPrep( string& name, int age, float & salary, char level)
7. What, if anything, is wrong with the following function?
void Square( int & x)
{
x = x * x;
return x;
}
8. What, if anything, is wrong with the following function?
void Power( int x, int y)
{
int result;
result = 1;
while ( y > 0 )
{
result = result * x;
y--;
}
}
9. Functions can be called form other functions in addition to main. True or False?
10. A value-returning function can have just one return statement. True or False?
pf2

Partial preview of the text

Download Function Basics: Parameters, Return Values, and Side Effects and more Exams Computer Science in PDF only on Docsity!

Function questions

  1. A function parameter is local to the entire block that is the body of the function. True or False?
  2. It's a good idea to use global variables whenever you can. True or False?
  3. A function prototype must specify the name of the function and the name and type of each of its parameters. True or False?
  4. When and where does control return from a void function?
  5. When and where does control return from a value-returning function?
  6. In the following function heading, which parameters are value parameters and which are reference parameters? void ExamPrep( string& name, int age, float & salary, char level)
  7. What, if anything, is wrong with the following function? void Square( int & x) { x = x * x; return x; }
  8. What, if anything, is wrong with the following function? void Power( int x, int y) { int result; result = 1; while ( y > 0 ) { result = result * x; y--; } }
  9. Functions can be called form other functions in addition to main. True or False?
  10. A value-returning function can have just one return statement. True or False?
  1. Identify the side effect in the following program #include string a; int w; bool GetYesOrNo(); int main() { cout << " Enter name:"; cin >> a; w = 0; cout <<"Are there weight data?"; if (GetYesOrNo()) { cout <<"Enter weight:"; cin >> w; } cout << "name is " << a << " weight is " << w <<endl; } bool GetYesOrNo() { cout << " Enter yes or no: "; cin >> a; return a = = "yes"; }
  2. What if anything is wrong with the following function? bool Greater (int a, int b) { if (a > b) return true; }
  3. Write a function that returns the largest value in a file that's supposed to contain all ints.