4 - Parameters and Overloading Flashcards
There are two basic kinds of parameters. They are …
- Call-by-value parameters
2. Call-by-reference parameters
What is a call-by-value parameter?
Call-by-value parameters take only the value of the argument that is plugged in.
What is a call-by-reference parameter?
With a call-by-reference parameter, the argument is a variable and the variable itself is plugged in; therefore, the variable’s value can be changed by the function invocation. A call-by-value parameter is indicated by using the ampersand &.
Can you mix call-by-reference and call-by-value parameters?
yes! void goodStuff (int& par1, int par2, double& part3);
When calling a function with call-by-reference parameters, what is actually given to the function?
A list of the memory locations associated with each parameter name.
What is overloading?
Giving two (or more) function definitions for the same function name.
How does the compiler know which overloaded function to use?
When there is a function call, the compiler uses the function definition whose number of formal parameters and types of formal parameters match the arguments in the function call.
Can you overload a function name by giving two definitions that differ only in the type of value returned?
No.
Can you overload based on call-by-value versus call-by-reference parameter differences?
No.
What is a function’s signature?
A function’s signature is the function name with the sequence of types in the parameter list, not including the const keyword and not including the ampersand, &.
What are the rules the compiler uses for resolving which of multiple overloaded function definitions it should use?
- Exact match.
2. Matching using automatic type conversion.
What is a default argument?
You can specify a default argument for one or more call-by-value parameters in a function. If the corresponding argument is omitted, the it is replaced by the default argument.
What is the syntax for default arguments?
void showVolume (int length, int width = 1, int height = 1);
The values are given in the function declaration, and not in the function definition.
Where must the default arguments be positioned?
They must be in the rightmost positions. If you have more than one default argument, then when the function is involved, you must omit arguments starting from the right.
What is the assert macro?
The assert macro is used like a void canton that takes one call-by-value parameter of type bool. When the assert macro is invoked, its assertion argument is evaluated. If it evaluates to true, nothing happens. If it evaluates to false, then the program ends and an error message is issued.