Chapter 6 Flashcards

(29 cards)

1
Q

modular programming

A

program broken up into manageable functions

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

function call

A

a statement that executes the function

Ex: { callFunction(); }

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

function definition

A

contains the statements that make up the function.

return type: data type of the value that is sent from the functions

name: same principle as variable names

parameter lists: list of variables that hold the values being passed to the function

body: enclosed in a set of braces and is a set of statements that perform the function’s operation.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

identify the 4 function definitions from the function below

int main() {

statement;
}

A

return type: int
function name: main
parameter list: () this is empty
function body: { statement; }

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

void function

A

perform one or more statement and then terminate

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

function header

A

part of the function definition. declares the return type, function name, and parameter list

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

return statement

A

causes a function to end immediately

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Please write out a value-returning function

A

A value-returning function will use int, double, bool, or any other valid data type in its
header. Here is an example of a function that returns an int value:

int sum(int num1, int num2) {
return num1 + num2;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

local variable

A

can only be accessed within the block of code it is defined

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

local variable lifetime

A

The timeframe the local variable exist. It only exists while the function is executing.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

global variable

A

any variable defined outside all the functions in a program.

Unless you explicitly initialize global variables, they are automatically initialized
to zero(numeric variables) or NULL(character variables)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What are three reason not to use a global variable?

A

Global variables make debugging difficult. Any statement could change the value of a global variable making it difficult to find out where the value was changed.

**Functions that use global variables are usually dependent on those variables. ** To reuse a function in a different program, you will most likely need to redesign it to not rely on the global variable.

Global variables make a program hard to understand. A global variable can be modified by any statement in the program. If you are to understand any part of the program that uses a global variable, you have to be aware of all the other parts of the
program that access the global variable.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

global constants

A

a named constant that is available
to every function in a program. Value will not change.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What does a local or parameter variable shadow mean?

A

The global variable is hidden by the local or parameter variable of the same name.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Static local variable

A

is not destroyed when a function returns. Exist for the lifetime of the program. Can only be initialized once in the program.

static dataType variableName;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

default argument

A

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

usually specified in the function prototype but can also be specified in the function header

Ex: void showArea(double = 20.0, double 10.0);
or
void showArea(double length = 20.0, double width = 10.0){
statement;
}

17
Q

What value can be used for a default argument?

A

The value of a default argument must be a literal value or a named constant.

18
Q

how do default arguments work when mixed with definied parameters?

A
  • When an argument is left out of a function call (because it has a default value), all the arguments that come after it must be left out too.
  • When a function has a mixture of parameters both with and without default arguments, the parameters with default arguments must be declared last.
19
Q

Reference Variables as Parameters

A

When used as parameters, reference variables allow a function to access the parameter’s original argument. Changes to the parameter are also made to the argument.

20
Q

reference variable

A

when used as a function parameter, allows access to the original argument.

A reference variable is an alias for another variable. Any changes made to the reference variable are actually performed on the variable for which it is an alias. By using a reference variable as a parameter, a function may change a variable that is defined in another function.

void doubleNum(int &refVar)
{
refVar *= 2;
}

21
Q

Define the reference variable function prototype, call, and definition

A

The ampersand must appear in both the prototype and the header of any function that uses a reference variable as a parameter. It does not appear in the function call.

// function prototype
void function(int&);
// function call
function(var);
// function definition
void function(int &var) { statement; }

22
Q

What are the limitations of reference variables?

A

Cannot use nonvariable argument, such as a literal, a constant, or an expression, into a reference parameter, an
error will result.

doubleNum(5); // error
doubleNum(userNum + 10) // error

23
Q

Write the prototype and header for a function called calculate.

The function should have three parameters: an int, a reference to a double, and a long (not necessarily in that order.) Only the int parameter should have a default argument, which is 47.

A

// protoype
void calculate(double&, long, int = 47);

// header
void calculate(double &var1, long var2, int var3) {}

24
Q

Write the prototype and header for a function called compute.

The function should have three parameters: an int, a double, and a long (not necessarily in that order). The int parameter should have a default argument of 5, and the long parameter should have a default argument of 65536. The double parameter should not have a default argument.

A

// prototype
void compute(double, int = 5, long = 65536);

// header
void compute(double varD, int varI, long varL) { }

25
overload function
Two or more functions may have the same name, as long as their parameter lists are different.
26
function signature
name of the function and the data types of the function's parameters in proper order Ex: function(dataType)
27
exit() function
causes a program to terminate, regardless of which function or control mechanism is executing. takes in an int which signals if the program exited successfully or due to failure. exit(EXIT_SUCCESS) or exit(0); exit(EXIT_FAILURE) or exit(1); must include
28
stub
A stub is a dummy function that is called instead of the actual function it represents. It usually displays a test message acknowledging that it was called, and nothing more. Stubs are very helpful tools for testing and debugging programs that use functions. void func(double var1, int var2) { cout << "The function was called with " << "the following arguments:\n" << "var1: " << var1 << endl << "var2: " << var2 << endl; }
29