CIS 1111 Chapter 6

Ace your homework & exams now with Quizwiz!

What will the following program display? 1 #include <iostream> 2 using namespace std; 3 4 void message1() 5 { 6 cout << "Hello\n"; 7 } 8 9 void message2() 10 { 11 cout << "Goodbye\n"; 12 message1(); 13 } 14 15 int main() 16 { 17 message2(); 18 return 0; 19 }

Goodbye Hello`

Write the definition of a function printDottedLine, which has no parameters and doesn't return anything. The function prints a single line consisting of 5 periods (terminated by a new line character)

void printDottedLine() { cout << ".....\n";

Write the definition of a function printGrade, which has a char parameter and returns nothing. The function prints on a line by itself the message string Grade: followed by the char parameter (printed as a character) to standard output. Don't forget to put a new line character at the end of your line. Thus, if the value of the parameter is 'A', the function prints out Grade: A

void printGrade(char grade){cout << "Grade: " << grade << "\n";}

Write the definition of a function printLarger, which has two int parameters and returns nothing. The function prints the larger value of the two parameters on a single line by itself. (For purposes of this exercise, the "larger" means "not the smaller".)

void printLarger ( int a , int b ) { if ( a > b ) cout << a << endl ; else cout << b << endl ; }

Write a statement that declares a prototype for a function printTodaysDate, which has no parameters and doesn't return anything.

void printTodaysDate();

Write the function prototype for a function called showScores with a parameter list containing four integer variables and a return type of void.

void showScores(int, int, int, int);

Write the function prototype for a function called showSquare. The function should have a single parameter variable of the int data type and a return type of void.

void showSquare(int);

Look at the following function definition: int doSomething(int number) { return number * 2; } Given the function definition, what will the following statement display? cout << doSomething(10);

20

Look at the following function definition: void displayValue(int value) { cout << value << endl; } What would be displayed if the value 5.2 was passed as an argument to the function when it is called in a program?

5

It is necessary to write the name of a parameter variable in a function prototype.

False

The phrase "divide and conquer" means that all of the programmers on a team should be divided and work in isolation.

False

What happens when a return statement is executed in a void function?

The function immediately terminates, and control returns to the statement that called the function.

Functions should be given names that reflect their purpose.

True

Look at the following function definition: double getGrossPay(int hoursWorked, double payRate) { return payRate * hoursWorked; } What will the function return?

a double containing the product of payRate times hoursWorked

printLarger is a function that accepts two int parameters and returns no value.Two int variables, sales1 and sales2, have already been declared and initialized.Write a statement that calls printLarger, passing it sales1 and sales2.

printLarger(sales1, sales2);

Which of the following is a valid return type for a value-returning function?

all of these choices (int, double, bool)`

Look at the following function definition: void displayGreeting(){cout << "Hello!";} Write a statement that calls the function.

displayGreeting();

Look at the following function definition: int doSomething(int number) { return number * 2; } What is the name of the function?

doSomething

Look at the following function prototype: double getGrossPay(int, double); What is its return type?

double

Write a statement that declares a prototype for a function powerTo, which has two parameters. The first is a double and the second is an int. The function returns a double.

double powerTo (double, int);

add is a function that accepts two int parameters and returns their sum. Two int variables, euroSales and asiaSales, have already been declared and initialized. Another int variable, eurasiaSales, has already been declared. Write a statement that calls add to compute the sum of euroSales and asiaSales and store this value in eurasiaSales.

eurasaiaSales = add(euroSales, asaiaSales)

A parameter variable's storage location in memory is the same as that of the original argument.

false

Function headers are terminated with a semicolon.

false

When a function terminates, it always branches back to main, regardless of where it was called from.

false

This is the part of a function definition that shows the function name, return type, and parameter list.

function header

Look at the following function definition:double getGrossPay(int hoursWorked, double payRate){return payRate * hoursWorked;}Write a statement that will call the function passing the values 20 and 12.75 as arguments.

getGrossPay (20, 12.75);

Look at the following function definition: int doSomething(int number) { return number * 2; } What type of data does the function return?

int

Write a statement that declares a prototype for a function add, which has two int parameters and returns an int.

int add (int, int);

Write the definition of a function half, which receives an integer parameter and returns an integer that is half the value of the parameter. (Use integer division!)So if the parameter's value is 7, the function returns the value 3.If the parameter's value happens to be 44, the functions returns the value 22.

int half (int value) {return value/2};

Write the definition of a function max that has three int parameters and returns the largest.

int max(int x,int y,int z) { if (x >= z and x >= y){ return (x);} else if (y >= x and y >= z){ return (y);} else{ return (z);} }

Write the definition of a function oneLess, which receives an int parameter and returns an int that is one less than the value of the parameter. So if the parameter's value is 7, the function returns the value 6. If the parameter's value happens to be 44, the functions returns the value 43.

int oneLess (int amount) { return amount -1; } OR int oneLess (int x) {return x-1;}

printErrorDescription is a function that accepts one int parameter and returns no value.Write a statement that calls the function printErrorDescription, passing it the value 14.

printErrorDescription(14);

printTodaysDate is a function that accepts no parameters and returns no value. Write a statement that calls printTodaysDate.

printTodaysDate();

A program contains the following function definition: int cube(int number){return number * number * number;} Write a statement that calls this function, passing the value 4 as an argument, and assigns the function's return value to the variable result.

result = cube (4);

Write a statement that calls a function named showSquare, passing the value 10 as an argument.

showSquare(10);

A value-returning function must have a return statement.

true

Either a function's definition or its prototype must precede all calls to the function.

true

Function prototypes are terminated with a semicolon.

true

If a function doesn't return a value, the word void will appear as its return type.

true

If other functions are defined before main, the program still starts executing at function main.

true

The scope of a parameter is limited to the body of the function that uses it.

true

Functions that return a value are known as this:

value-returning functions


Related study sets

Ch 15: Management of Patients with Oncologic Disorders (3)

View Set

Heartsaver Adult and Pediatric First Aid Test

View Set

Chapter 50: Assessment and Management of Patients With Biliary Disorders

View Set

Horizontal Projectiles and Projectile Motion

View Set

OMIS 320. Exam 3. SELU. Henderson.

View Set