chapter 6 - functions Flashcards

1
Q

modular programming

A
  • breaking a program up into smaller, manageable functions or modules
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

function

A
  • a collection of statements to perform a task
  • statements that make up a function
  • can have multiple parameters
  • an argument declaration must be listed in the function header ( ) for each parameter
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

motivation for modular programming

A
  • improves maintainability of programs
  • simplifies the process of writing programs
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

function call

A

statement causes a function to execute

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

function definition includes:

A
  • return type: data type of the value that function returns to the part of the program that called it
  • name: name of the function. function names follow same rules as variable
  • parameter list: variables containing values passed to the function
  • body: statements that perform the functions task, enclosed in { }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

function format

A

int main () {
cout &laquo_space;“Hello World\n; // function
return 0; // body
}

int = return type
main = function name
( ) = parameter list

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

function return type

A
  • if a function returns a value, the type of the value must be indicated
  • if a function does not return a value, its return type is void
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

calling a function

A
  • main can call any number of functions
  • functions can call other functions
  • value of argument is copied into parameter when the function is called
  • a parameter’s scope is the function which uses it
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

function prototypes

A
  • place function definition before calling function’s definition
  • place prototypes near top of program
  • program must include either prototype or full function definition before any call to the function
  • when using prototypes, can place function definitions is any order in source file
  • there must be a data type listed in the prototype ( )
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

how calling a function works

A
  • when called, program executes the body of the called function
  • after the function terminates, execution resumes in the calling function at point of call
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

what must the compiler know about a function

A
  • compiler must know the following about a function before it is called
    • name, return type, number of parameters, data type of each parameter
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

sending data into a function

A
  • can pass values into a function at time of call
  • values passed to functions are arguments
  • variables in a function that hold the values passed as arguments are parameters
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

other parameter terminology

A
  • a parameter can also be called a formal parameter or a formal argument
  • an argument can also be called an actual parameter or an actual argument
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

function call format

A

evenOrOdd (int);

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

function prototype format

A

int evenOrOdd (int);

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

function header format

A

int evenOrOdd (int num)

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

when passing multiple arguments . . .

A
  • the number of arguments in the call must match the prototype and definition
  • the first argument will be used to initialize the first parameter, the second argument to initialize the second parameter, n argument to initialize the n parameter
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

pass by value

A
  • when an argument is passed to a function, its value is copied into the parameter
  • changes to the parameter in the function does not affect the value of the argument
19
Q

functions can be used to . . .

A
  1. implement user choices from menu
  2. implement general-purpose tasks:
20
Q

higher-level functions

A
  • can call general-purpose functions, minimizing the total number of functions and speeding program dev. time
21
Q

return statement

A
  • used to end execution of a function
  • can be placed anywhere in a function
    • statements that follow the return statement will not be executed
  • can be used to prevent abnormal termination of program
  • in a void function without a return statement, the function ends at its last }
22
Q

value-returning function

A
  • a function can return a value or an expression back to the statement that called the function
  • return statement can be used to return a value or expression from function to the point of call
23
Q

value-returning function format

A

double sum (double num1, double num2) {
int result = num1 + num2;
return result;
} // or return num1 + num 2;
where total = sum( value1, value2);

double sum = return type
result = value being returned

24
Q

different return value methods

A
  • assign it to a variable
  • send it to cout
  • use it in an expression
25
Q

returning a boolean value

A
  • function can return true or false
  • declare return type in function prototype and heading as bool
  • function body must contain return statement(s) that return true or false
  • calling function can use return value in a relational expression
26
Q

local variable

A
  • variables defined inside a function are local to that function. they are hidden from the statements in other functions, which normally cannot access them.
  • other functions may have separate, distinct variables with the same name
  • not automatically initialized. must be initialized by programmer
27
Q

local variable lifetime

A
  • a function’s local variables exist only while the function is executing
28
Q

how does local variable lifetime work in a function

A
  • when the function begins, its local variables and its parameter variables are created in memory
  • when the function ends, the local variables and parameter variables are destroyed
  • any value stored in a local variable is lost between calls to the function in which the variable is declared
29
Q

global variable

A
  • any variable defined outside all the functions in a program
  • can be accessed by all functions that are defined after the global variable is defined
  • should be avoided because they make programs difficult to debug
  • any global variable created should be global constants
  • global variables are automatically initialized to 0 (numeric) or NULL (character) when the variable is defined
30
Q

global variable scope

A
  • the portion of the program from the variable definition to the end
31
Q

static local variables

A
  • unlike local variables, static local variables retain their contents between function calls
  • are defined and initialized only the first time the function is executed. 0 is the default initialization value
32
Q

default argument

A
  • an argument that is passed automatically to a parameter if the argument is missing on the function call
  • must be a constant declared in prototype:
    void evenOrOdd (int = 0);
  • can be declared in header if no prototype
  • multi-parameter functions may have default arguments for some or all of them
    int getSum (int, int = 0, int = 0);
33
Q

default argument rules/format

A
  • if not all parameters to a function have default values, the defaultless ones are declared first in the parameter list
    int getSum (int, int = 0, int = 0); // OK
    int getSum (int, int = 0, int) ; // WRONG
  • when an argument is omitted from a function call, all arguments after it must also be omitted
    sum = getSum (num1, num2); // OK
    sum = getSum (num1, , num3); // WRONG
34
Q

reference variables as parameters

A
  • a mechanism that allows a function to work with the original argument from the function call, not a copy of the argument
  • allows function to modify values stored in the calling environment
  • provides a way for the function to ‘return’ more than one value
35
Q

reference variable

A
  • an alias for another variable
  • defined with an ampersand (&)
  • changes to a reference variables are made to the variable it refers to
  • use reference variables to implement passing parameters by reference
36
Q

reference variable format

A

void getDimensions (int&, int&);
- each reference parameter must contain &
- space between type and & is unimportant
- must use & in both prototype and header

37
Q

reference variable rules

A
  1. argument passed to reference parameter must be a variable - cannot be an expression or constant
  2. use when appropriate - don’t use when argument should not be changed by function, or if function needs to return only 1 value
38
Q

overloading functions

A
  • have the same name but different parameter lists
  • can be used to create functions that perform the same task but take different parameter types or different number of parameters
  • compiler will determine which version of function to call by arguments and parameter lists
39
Q

exit ( ) function

A
  • terminates the execution of a program
  • can be called from any function
  • can pass an int value to operating system to indicate status of program termination
  • usually used for abnormal termination of program
  • requires cstdlib header file
40
Q

exit function format

A
  • exit (0);
  • cstdlib header defines two constants that are commonly passed, to indicate success or failure:
    exit (EXIT_SUCCESS);
    exit (EXIT_FAILURE);
41
Q

stub

A
  • a dummy function used in place of an actual function
  • usually displays a message indicating it was called. may also display parameters
42
Q

driver

A
  • a function that tests another function by calling it
  • various arguments are passed and return values are tested
43
Q

stubs and drivers are useful for . . .

A
  • useful for testing and debugging program and function logic and design