chapter 6 - functions Flashcards
1
Q
modular programming
A
- breaking a program up into smaller, manageable functions or modules
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
3
Q
motivation for modular programming
A
- improves maintainability of programs
- simplifies the process of writing programs
4
Q
function call
A
statement causes a function to execute
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 { }
6
Q
function format
A
int main () {
cout «_space;“Hello World\n; // function
return 0; // body
}
int = return type
main = function name
( ) = parameter list
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
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
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 ( )
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
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
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
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
14
Q
function call format
A
evenOrOdd (int);
15
Q
function prototype format
A
int evenOrOdd (int);
16
Q
function header format
A
int evenOrOdd (int num)
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