Chapter 6 - Functions Flashcards
Define: Code reuse
Write the code to perform a task ONCE, then reuse it each time you need to perform the task.
Define: Function call
A statement that causes a function to execute.
Define: Function Definition
Contains the statements that make up the function.
All function definitions have what 4 parts?
1) Return Type
2) Name
3) Parameter list
4) Body
Define: Return type (of a function)
#1 data type of the value that is sent FROM the function listed first int main() --> int is the RETURN TYPE
Define: Name (of a function)
#2 Should give each function a descriptive name - in general, same rules apply as to variables for naming int main() --> main is the NAME
Define: Parameter List (of a function)
#3 List of variables that hold the values being passed TO the function int main() --> () is the parameter list (it's empty!)
Define: Body (of a function)
The set of statements that perform the function's operation. enclosed in a set of braces int main() { BODY HERE }
Define: Function Header
The line in the definition that reads int main() (the first line)
Define: Void functions (they return….)
Some function simply perform one or more statements and then terminate, without returning a value. these are VOID functions. Example: void displayMessage() { cout << "hello from the function displayMessage. \n"; }
Notice: not return statement
void displayMessage() { cout << "hello from the function displayMessage. \n"; } What part is the function header? How would you "call" this function?
Function Header: void displayMessage()
Call: displayMessage();
Fuction prototypes are also known as …
Function declarations
You must place either the function definition or the function prototype _______ all calls to the functions.
What happens if you don’t?
BEFORE
Or else the program will not compile
What would a “function prototype” for the following function definition be? What’s different about it?
void displayMessage() { cout << "hello from the function displayMessage. \n"; }
void displayMessage(); - semicolon at the end
Tells compiler the function displayMessage has a void return type (doesn’t return a value) and uses no parameters.
When a function is called, the program may send _____ into the function.
VALUES
Values sent into a function are called ___________.
arguments
Define: Parameter
a special variable that holds a value being passed into a function.
For example:
void displayValue(int num)
{
court «_space;“The value is “ «_space;num «_space;endl;
}
The variable num is the parameter.
For this function definition, what would the prototype be that you could use at the top? void displayValue(int num) { court << "The value is " << num << endl; }
void displayValue(int);
How would you call the function of this prototype: void displayValue(int);
displayValue(5); (this could be any integer where the argument 5 is)
What could the function body be for the following function:
void displayValue(int);
void displayValue(int num) { cout << "The value is " << num << endl; }
WARNING!
When passing a variable as an argument, simply write the variable name inside the parenthese of the function call. Do not write the _____ _____ of the argument.
Data Type.
displayValue(int x); // Error!
Call should appear as:
displayValue(x); // Correct
If you pass an argument whose TYPE is not the same as the parameter’s type, the argument will be ________ or _________ automatically.
promoted or demoted
Ex: the following function call would be truncated (because the parameter is an integer not double):
displayValue(4.7);
WARNING!
Each parameter variable in a parameter list must have a _____ _____ listed before its name.
Provide an example.
Data Type: void showSum (int num1, num2, num3) // ERROR!
void showSum (int num1, int num2, int num3) // Correct
When a function with multiple parameters is called, the arguments are passed to the parameter in ______.
order.
example: showSum(5, 10, 15); passes 5, 10, 15 in that order ….
When an argument is passed into a parameter, only a _____ of the argument’s value is passed.
Changes to the parameter do not affect the ______ argument.
When an argument is passed into a parameter, only a copy of the argument’s value is passed.
Changes to the parameter do not affect the original argument.
The return Statement causes a function to …
end immediately
Functions that return a value are known as….
value-returning functions
The power function is an example of a ______-_____ function.
double x;
x = pow(4.0, 2.0);
value-returning function
double x;
x = pow(4.0, 2.0);
The 2nd line calls the pow function, passing 4.0 and 2.0 as arguments.
the function calculates the value of 4.0 raised to the power of 2.0 and returns the value, which is 16.0 and is assigned to the x variable by the = operator.
Although several arguments may be passed into a function, only one ______ may be returned from it.
value
int sum(int num1, int num2) What is the Return Type?
int
What is the general format of a return statement?
return expression;
The return expression can be any expression that has a _______, such as…..
The return expression can be any expression that has a value, such as a variable, literal, or mathematical expression.
A local variable is defined ______ a function and not accessible ______ the function.
A local variable is defined inside a function and not accessible outside the function.