Chapter 6 C++

Ace your homework & exams now with Quizwiz!

a function call is a statement that causes a function to execute

a function definition contains the statements that make up the function

a function is executed when it is

called

function call

calling function

exit() function

causes a program to terminate regardless of which function or control mechanism is executing

when an argument is passed into a parameter only a copy of the argument's value is passed

changes to the parameter do not affect the original argument

global variable

defined outside all functions and is accessible to all functions in its scope

divide and conquer

functions that break a problem into small manageable parts

a value returning function will use

int, double, bool, and other valid data types in its header

stub

is a dummy function that is called instead of the actual function it represents; usually displays a test message acknowledging that it was called and nothing more

driver

is a program that tests a function by simply calling it; if function accepts arguments the driver passes test data; if function returns a value the driver displays the return value on screen; allows you to see how the function performs in isolation from the rest of the program it will eventually be part of

when a reference parameter is used

it is said that the argument is passed by reference

the void function does not return a value

it uses the key word void as its return type in the function header

global constants

named constant that is a variable to every function in a program; their value cannot be changed during the program's execution; typically used to represent unchanging values that are needed throughout a program

default arguments

passed to parameters automatically if no argument is provided in the function call; are literal values or constants with an = operator in from of them, appearing after the data types listed in a function prototype; ex: void showArea (double=20, double=10); void showArea(double length=20.0, double width =10);

void functions

perform one or more statements, which follows terminate; Ex: void displayMessage() { cout << "Hello from the function display message.\n"; }

when an overloaded function is called C++ uses this to distinguish it from other functions with the same name

the function's signature

when creating a function

you must write its definition

unless you explicitly initialize numeric global variables they are automatically initialized to the value of

0

Function prototypes are terminated with a semicolon.

True

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 may also be called in

a hierarchical, or layered, fashion

one way of ensuring that the compiler has the information is to place the function definition before all calls to that function

another method is to declare the function with a function prototype

two or more functions may have the same name

as long as their parameter lists are different

a modular program is

broken up into functions that perform specific tasks

global variables

can make debugging difficult; functions that use global variables are usually dependent on those variables; they make a program hard to understand

the return statement

causes a function to end immediately

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

false

function headers are terminated with a semi colon

false

the function's return value is part of the signature

false

the values stored in the function's local nonstatic variables persist between function calls

false

when a function terminates it always branches back to main regardless of where it was called from

false

function prototypes are also known as

funciton declarations

using parameters you can design your own

functions that accept data this way; ex: void displayValue(int num) { cout << "The value is " << num << endl; } the integer variable definition inside the parentheses (int num); the variable num is a parameter

value-returning functions

functions that return a value ex: double x; x=pow(4.0,2.0); function calculates the value of 4 raised to the power of 2 and returns that value

stubs and drivers are

helpful tools for testing and debugging programs that use functions; they allow you to test the individual functions in a program in isolation from the parts of the program that call the functions

int myfunction(int x, double y) What is the function's signature?

myfunction (int, double)

function signature

name of the function and the data types of the function's parameters in he proper order

before the compiler encounters a call to a particular function it must already know the function's return type the number

of parameters it uses and the type of each parameter

the scope of a global variable

portion of the program from the variable definition to the end; means that a global variable can be accessed by all functions that are defined after the global variable is defined

function header

shows the function name, return type and parameter list; int main()

prototype example: void displayMessage();

the prototype looks similar to the function header except there is a semicolon at the end; the statement tells the compiler that the function has a void return type and uses no parameters

a function prototype eliminates the need to place a function definition before all calls

to the function

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

true

To use the exit function, you must include the cstdlib header file.

true

When the exit function is called, it causes the program to stop, regardless of which function contains the call.

true

a function's local variables exist and may be used as long as the program is running

true

if a function does not return a value the word void will appear as its return type

true

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

true

variables are visible only in the function in which they are defined

true

arguments

values that are sent into a function; ex: result = pow (2.0,4.0); the arguments are 2.0 and 4.0

local variables

variables that are defined inside a function and is not accessible outside the funtion

you can incorporate more than one parameter variable in a function header separated by commas

void showSum (int num1, int num2, int num 3)

#include <iostream> using namespace std; void message1 ( ) { cout << "Hello\n"; } void message2 ( ) { cout << "Goodbye\n"; message1 ( ); } int main ( ) { message2 ( ); return 0; }

what does it display? goodbye hello

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

passed by value

when only a copy of an argument is passed to a function

functions are ideal for use in menu-driven programs

when the user selects an item from a menu the program can call the appropriate function

reference variable

when used as a function parameter allows access to the original argument; is an alias for another variable; any changes made to the reference variable for which it is an alias; by using a reference variable as a parameter, a function my change a variable that is defined in another function; you have to place a & in front of the name; ex: void doubleNum (int &refVar) { refVar*=2 }

code reuse

write the code to perform a task once and then reusing it each time you need to perform the task

all function definitions have the following parts

~ return type: function can send a value to the part of the program that executed it; the return type is the data of the value that is sent from the function ~ name: give function descriptive name ~ parameter list: program can send data into function; the parameter list is a list of variables that hold the values being passed to the function ~ body: set of statement that perform the function's operation; they are enclosed in a set of braces


Related study sets

Module 3 PrepU Question Collection Assignment

View Set

Top Reasons for Business Failure

View Set

SBAC Week 5 Geometry Vocabulary Circles

View Set

Chapter 6: Asians/Asian Americans

View Set

MA Office Financial Management, Billing, Insurance

View Set

Lecture 18; The Hydrologic Cycle; Chapter 16

View Set