Intro to Functions

Ace your homework & exams now with Quizwiz!

The ceil function in the C++ standard library takes a single value x and returns the smallest integer that is greater than or equal to x. Which of the following is true about ceil(12.6)?

The argument is 12.6, and the return value is 13.

What is the problem with the code snippet below? string val() { string result = "candy"; return; } int main(){ cout << val() << endl; return 0; }

The function val does not have a return value.

Which of the following is the best choice for a return type from a function that prompts users to enter their credit card number?

string

What is the output if the function call is testmyval(6) in the following code snippet? void testmyval(int nval) { if (nval > 0) { testmyval(nval - 2); } cout << nval << " "; }

0 2 4 6

What is the output of the following code snippet? int my_fun(int perfect) { return ((perfect -1) * (perfect -1)); } int main () { for (int i = 0; i < 4; i++) { cout << my_fun(i) << " "; } return 0; }

1 0 1 4

What is the output of the following code snippet? int pow(int base, int power) { int result = 1; for (int i = 0; i < power; i++) { result = result * base; } return result; } int main() { cout << pow(pow(2, 2), 2) << endl; return 0; }

16

What is the output of the following code snippet? int absval(int a) { if (a < 0) { return -a; } else { return a; } } int main() { cout << absval(-2); return 0; }

2

What is the output of the following code snippet? int black_box(int a) { int val; if (a <= 0) { val = 1; } else { val = a + black_box(a - 2); } return val; } int main() { cout << black_box(4); return 0; }

7

Which of the following is true about function return statements?

A function can hold multiple return statements, but only one return statement executes in one function call.

Consider the function shown below. int do_it(int x, int y) { int val = 0; if (x < y) { val = x + 1; } else { val = y - 1; } // the last statement goes here } What should be the last statement of this function?

A return statement

Consider this function comment. Which of the following options is recommended in your textbook? /** Computes the area of a rectangle. @param width the width of the rectangle @return the area of the rectangle */ double area(double width, double height) { double area = width * height; return area; }

Both parameters should be described.

How are variables passed as input to a function?

By using arguments

In an accounting application, you discover several places where the total profit, a double value, is calculated. Which of the following should be done to improve the program design?

Consider writing a function that returns the total profit as a double value.

Which of the following is true about functions?

Function can have multiple parameters and can return one return value.

In the following code snippet, what is the scope of variable b? void func1() { int i = 0; double b = 0; } void func2() { } int main() { func1(); func2(); return 0; }

It can be used only in func1().

A function uses a reference parameter when

It is designed to update a variable supplied as an argument

Which of the following is correct about a global variable?

It is visible to all the functions declared after it.

One advantage of designing functions as black boxes is that

Many programmers can work on the same project without knowing the internal implementation details of functions

The term "Black Box" is used with functions because

Only the specification matters; the implementation is not important.

Consider this function comment. Which of the following options is recommended in your textbook? /** Computes the area of a cuboid. @param1 width the width of the cuboid @param2 height the height of the cuboid @param3 length the length of the cuboid @return the area of the cuboid */ double area(double width, double height, double length) { double result = width * height * length; return result; }

The @param1, @param2, @param3 should be just @param.

For the given code snippet, which of the following statements is true? double raise(double rate) { double new_pay_rate = rate * 1.1; return new_pay_rate; } int main() { double rate = 40.0; double new_pay_rate = 0.0; new_pay_rate = raise(rate); cout << "Pay rate: " << new_pay_rate << endl; return 0; }

The code snippet executes and displays "Pay rate: 44.0".

If a function is declared to return "void", then which statement below is true?

The function needs a return statement with no return value

What is the syntax error in the following function definition? string parameter(double r) { double result; result = 2 * 3.14 * r; return result; }

The value that is returned does not match the specified return type

What is the syntax error in the following function definition? string area(double r) { double a; a = 3.14 * r * r; return r * r; }

The value that is returned does not match the specified return type.

Why are the @ symbols used in these function comments? /** Computes the area of a rectangle. @param width the width of the rectangle @return the area of the rectangle */ double area(double width, double height) { double area = width * height; return area; }

They are special symbols for the Doxygen documentation-generator utility tool.

The purpose of a function that returns "void" is

To package a repeated task as a function even though the task does not yield a value

When should comments be written to describe functions?

Whenever you write a function

Consider a function named calc, which accepts two numbers as integers and returns their sum as an integer. Which of the following is the correct statement to call the function calc?

int sum = calc(2, 3);

For a program that reads city names repeatedly from the user and calculates the distance from Head Office, which of the following would be a good design based on stepwise refinement?

Write one function that reads city name and calculates distance.

A recursive function is one which:

calls itself.

Suppose you need to write a function that calculates the area of a rectangle. Which of the following is the best choice for the declaration of this function?

double area(double w, double h)

Consider a function named avg, which accepts four numbers as integers and returns their average as a double. Which of the following is the correct statement to call the function avg?

double average = avg(2, 3, 4, 5);

Which of the following is the correct first line of a function definition named calc_average that accepts three int parameters and returns a double?

double calc_average(int a, int b, int c)

Which of the following is the correct first line of a function definition named calc_sum that accepts four int parameters and returns a double?

double calc_sum(int a, int b, int c, int d)

Suppose you need to write a function that calculates the volume of rectangular boxes. Which of the following is the best choice for the declaration of this function?

double volume(double w, double h, double l)


Related study sets

Cognitive Psychology Exam 4 / Final

View Set

ATI Targeted Med Surg - Cardiovascular

View Set