Chapter 6 Flashcards
modular programming
program broken up into manageable functions
function call
a statement that executes the function
Ex: { callFunction(); }
function definition
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.
identify the 4 function definitions from the function below
int main() {
statement;
}
return type: int
function name: main
parameter list: () this is empty
function body: { statement; }
void function
perform one or more statement and then terminate
function header
part of the function definition. declares the return type, function name, and parameter list
return statement
causes a function to end immediately
Please write out a value-returning function
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;
}
local variable
can only be accessed within the block of code it is defined
local variable lifetime
The timeframe the local variable exist. It only exists while the function is executing.
global variable
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)
What are three reason not to use a global variable?
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.
global constants
a named constant that is available
to every function in a program. Value will not change.
What does a local or parameter variable shadow mean?
The global variable is hidden by the local or parameter variable of the same name.
Static local variable
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;
default argument
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;
}
What value can be used for a default argument?
The value of a default argument must be a literal value or a named constant.
how do default arguments work when mixed with definied parameters?
- 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.
Reference Variables as Parameters
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.
reference variable
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;
}
Define the reference variable function prototype, call, and definition
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; }
What are the limitations of reference variables?
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
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.
// protoype
void calculate(double&, long, int = 47);
// header
void calculate(double &var1, long var2, int var3) {}
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.
// prototype
void compute(double, int = 5, long = 65536);
// header
void compute(double varD, int varI, long varL) { }
overload function
Two or more functions may have the same name, as long as their parameter lists are different.
function signature
name of the function and the data types of the function’s parameters in proper order
Ex: function(dataType)
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 <cstdlib></cstdlib>
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 «_space;“The function was called with “
«_space;“the following arguments:\n”
«_space;“var1: “ «_space;var1 «_space;endl
«_space;“var2: “ «_space;var2 «_space;endl;
}