chapter 6 - functions Flashcards
(43 cards)
modular programming
- breaking a program up into smaller, manageable functions or modules
function
- 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
motivation for modular programming
- improves maintainability of programs
- simplifies the process of writing programs
function call
statement causes a function to execute
function definition includes:
- 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 { }
function format
int main () {
cout «_space;“Hello World\n; // function
return 0; // body
}
int = return type
main = function name
( ) = parameter list
function return type
- 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
calling a function
- 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
function prototypes
- 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 calling a function works
- when called, program executes the body of the called function
- after the function terminates, execution resumes in the calling function at point of call
what must the compiler know about a function
- compiler must know the following about a function before it is called
- name, return type, number of parameters, data type of each parameter
sending data into a function
- 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
other parameter terminology
- 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
function call format
evenOrOdd (int);
function prototype format
int evenOrOdd (int);
function header format
int evenOrOdd (int num)
when passing multiple arguments . . .
- 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
pass by value
- 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
functions can be used to . . .
- implement user choices from menu
- implement general-purpose tasks:
higher-level functions
- can call general-purpose functions, minimizing the total number of functions and speeding program dev. time
return statement
- 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 }
value-returning function
- 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
value-returning function format
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
different return value methods
- assign it to a variable
- send it to cout
- use it in an expression