• Home
  • Textbooks
  • Starting out with C++: From Control Structures through Objects
  • Functions

Starting out with C++: From Control Structures through Objects

Tony Gaddis

Chapter 6

Functions - all with Video Answers

Educators


Chapter Questions

Problem 1

Why do local variables lose their values between calls to the function in which they are defined?

Check back soon!

Problem 2

What is the difference between an argument and a parameter variable?

Check back soon!
01:32

Problem 3

Where do you define parameter variables?

Harsh Gadhiya
Harsh Gadhiya
Numerade Educator

Problem 4

If you are writing a function that accepts an argument and you want to make sure the function cannot change the value of the argument, what do you do?

Check back soon!

Problem 5

When a function accepts multiple arguments, does it matter in what order the arguments are passed?

Check back soon!
01:42

Problem 6

How do you return a value from a function?

Nick Johnson
Nick Johnson
Numerade Educator
01:29

Problem 7

What is the advantage of breaking your application's code into several small procedures?

Jennifer Stoner
Jennifer Stoner
Numerade Educator

Problem 8

How would a static local variable be useful?

Check back soon!

Problem 9

Give an example where passing an argument by reference would be useful.

Check back soon!

Problem 10

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

Check back soon!

Problem 11

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

Check back soon!

Problem 12

Either a function’s _________ or its _________ must precede all calls to the function.

Check back soon!

Problem 13

Values that are sent into a function are called _________.

Check back soon!

Problem 14

Special variables that hold copies of function arguments are called _________.

Check back soon!

Problem 15

When only a copy of an argument is passed to a function, it is said to be passed by _________.

Check back soon!

Problem 16

A(n) _________ eliminates the need to place a function definition before all calls to the function.

Check back soon!

Problem 17

A(n) _________ variable is defined inside a function and is not accessible outside the function.

Check back soon!

Problem 18

_________ variables are defined outside all functions and are accessible to any function within their scope.

Check back soon!

Problem 19

_________ variables provide an easy way to share large amounts of data among all the functions in a program.

Check back soon!

Problem 20

Unless you explicitly initialize global variables, they are automatically initialized to _________.

Check back soon!

Problem 21

If a function has a local variable with the same name as a global variable, only the _________ variable can be seen by the function.

Check back soon!

Problem 22

_________ local variables retain their value between function calls.

Check back soon!

Problem 23

The _________ statement causes a function to end immediately.

Check back soon!

Problem 24

_________ arguments are passed to parameters automatically if no argument is provided in the function call.

Check back soon!

Problem 25

When a function uses a mixture of parameters with and without default arguments, the parameters with default arguments must be defined _________.

Check back soon!

Problem 26

The value of a default argument must be a(n) _________.

Check back soon!

Problem 27

When used as parameters, _________ variables allow a function to access the parameter’s original argument.

Check back soon!

Problem 28

Reference variables are defined like regular variables, except there is a(n) _________ in front of the name.

Check back soon!

Problem 29

Reference variables allow arguments to be passed by ____________.

Check back soon!

Problem 30

The _________ function causes a program to terminate.

Check back soon!

Problem 31

Two or more functions may have the same name, as long as their _________ are different.

Check back soon!

Problem 32

Examine the following function header, then write an example call to the function:
void showValue(int quantity)

Check back soon!
01:12

Problem 33

The following statement calls a function named half. The half function returns a value that is half that of the argument. Write the function.
result = half(number);

Brandon Fox
Brandon Fox
Numerade Educator
01:02

Problem 34

A program contains the following function:
int cube(int num)
{
return num * num * num;
}
Write a statement that passes the value 4 to this function and assigns its return valueto the
variable result.

Brandon Fox
Brandon Fox
Numerade Educator

Problem 35

Write a function named timesTen that accepts an argument. When the function is called, it should display the product of its argument multiplied by 10.

Check back soon!

Problem 36

A program contains the following function:
void display(int arg1, double arg2, char arg3)
{
cout << "Here are the values: "
<< arg1 << " " << arg2 << " "
<< arg3 << endl;
}
Write a statement that calls the procedure and passes the following variables to it:
int age;
double income;
char initial;

Check back soon!

Problem 37

Write a function named getNumber that uses a reference parameter variable to accept an integer argument. The function should prompt the user to enter a number in the range of 1 through 100. The input should be validated and stored in the parameter variable.

Check back soon!

Problem 38

True or False
T F Functions should be given names that reflect their purpose.

Check back soon!

Problem 39

True or False
T F Function headers are terminated with a semicolon.

Check back soon!

Problem 40

True or False
T F Function prototypes are terminated with a semicolon.

Check back soon!

Problem 41

True or False
T F If other functions are defined before main, the program still starts executing at function main.

Check back soon!

Problem 42

True or False
T F When a function terminates, it always branches back to main, regardless of where it was called from.

Check back soon!

Problem 43

True or False
T F Arguments are passed to the function parameters in the order they appear in the function call.

Check back soon!

Problem 44

True or False
T F The scope of a parameter is limited to the function that uses it.

Check back soon!

Problem 45

True or False
T F Changes to a function parameter always affect the original argument as well.

Check back soon!

Problem 46

True or False
T F In a function prototype, the names of the parameter variables may be left out.

Check back soon!

Problem 47

True or False
T F Many functions may have local variables with the same name.

Check back soon!

Problem 48

True or False
T F Overuse of global variables can lead to problems.

Check back soon!

Problem 49

True or False
T F Static local variables are not destroyed when a function returns.

Check back soon!

Problem 50

True or False
T F All static local variables are initialized to −1 by default.

Check back soon!

Problem 51

True or False
T F Initialization of static local variables only happens once, regardless of how many times the function in which they are defined is called.

Check back soon!

Problem 52

True or False
T F When a function with default arguments is called and an argument is left out, all arguments that come after it must be left out as well.

Check back soon!

Problem 53

True or False
T F It is not possible for a function to have some parameters with default arguments and some without.

Check back soon!

Problem 54

True or False
T F The exit function can only be called from main.

Check back soon!

Problem 55

True or False
T F A stub is a dummy function that is called instead of the actual function it represents.

Check back soon!

Problem 56

Each of the following functions has errors. Locate as many errors as you can.

void total(int value1, value2, value3)
{
return value1 + value2 + value3;
}

Check back soon!

Problem 57

Each of the following functions has errors. Locate as many errors as you can.

double average(int value1, int value2, int value3)
{
double average;
average = value1 + value2 + value3 / 3;
}

Check back soon!

Problem 58

Each of the following functions has errors. Locate as many errors as you can.

void area(int length = 30, int width)
{
return length * width;
}

Check back soon!

Problem 59

Each of the following functions has errors. Locate as many errors as you can.

void getValue(int value&)
{
cout << "Enter a value: ";
cin >> value&;
}

Check back soon!

Problem 60

Each of the following functions has errors. Locate as many errors as you can.

(Overloaded functions)
int getValue()
{
int inputValue;
cout << "Enter an integer: ";
cin >> inputValue;
return inputValue;
}
double getValue()
{
double inputValue;
cout << "Enter a floating-point number: ";
cin >> inputValue;
return inputValue;
}

Check back soon!