C++ Primer - Chapter 6 (Functions) Flashcards
What does a function definition consist of?
A return type specifier, an identifier, a parameter list and a function body.
How is a function executed?
By using the call operator, ().
What is the term for the values supplied to the call operator?
Arguments.
What purpose do function arguments have?
To initialise the function parameters.
What is the purpose of the following function? int f(int val) { int ret = 1; while (val > 1) ret *= val--; return ret; }
To compute the factorial of val.
True or false? A function call causes the called function to be suspended and the execution of the calling function to begin.
False. Execution of the calling function is suspended and execution of the called function begins.
How can an empty parameter list be written implicitly and explicitly?
Implicitly:
void f1() { /* . . . / }
Explicitly:
void f2(void){ / . . . */ }
Where is the error in the following code? int f3(int v1, v2) { /* . . . */ } int f4(int v1, int v2) { /* . . . */ }
int f3(int v1, v2) { /* . . . */ } // This is an error; there is no type specifier for v2. int f4(int v1, int v2) { /* . . . */ }
What keyword typically indicates the end of a function?
The “return” keyword.
What is the difference between a parameter and an argument?
A parameter is a value defined through the function prototype and an argument is a value passed through a function call.
Indicate which of the following functions are in error and why. (a) int f() { string s; // . . . return s; } (b) f2(int i) { /* . . . */ } (c) int calc(int v1, int v1) { /* . . . */ } (d) double square(double x) return x * x;
(a) int f() { string s; . . . return s; } // Error; returns a string not an int. (b) f2(int i) { /* . . . */ } // Error; no return type. (c) int calc(int v1, int v1) {/* . . . */ } // Error; parameters have the same name. (d) double square(double x) return x * x; // Error; no braces around function body.
What is the lifetime of an object?
The time during the programs’s execution that the object exists.
What is an “automatic object”?
An object that exists only while a block is executing.
True or false? Parameters are not automatic objects.
False.
What value does an uninitialised variable of built-in type defined inside a function body take?
The value is undefined.
How can a local variable be created inside a function and be forced to persist across function calls?
By using the “static” keyword.
When is a local static object destroyed?
When the program terminates.
How is a local static object with no explicit initialiser initialised?
By value initialisation. Local static variables of built-in type will be initialised to zero.
Where can parameter names be omitted?
In function declarations or in a function definition when the corresponding parameter is unused.
What is another name for a function declaration?
A function prototype.
Is it an error to only declare a function?
No, not if the function is never used.
True or false? Parameter initialisation works in the same way that variable initialisation works.
True.
What is the term given to an argument when the corresponding parameter is a reference?
“Passed by reference.”
When is an argument “passed by value”?
When the argument value is copied in the initialisation of a function parameter.
Programmers accustomed to C often use pointer parameters to access objects outside a function body. Is this also true for C++?
No. C++ programmers tend to use reference parameters.
Can a low-level const be passed to a nonconst reference parameter?
No.
Name three common ways to manage pointer (to array) parameters.
- Using a marker to specify the extent of an array (e.g. the null character ‘\0’ in a string).
- Using the STL convention; passing a pointer to the first and one past the last element in the array.
- Explicitly passing a size parameter.
How can a function be declared such that it can take an unknown number of arguments if the parameters are all of the same type? What if the arguments differ?
Using an initialiser_list if the parameters are all of the same type and a variadic template if the parameter types differ.
True or false? The elements of an initialiser_list can be changed after initialisation.
False. They are always const values.
How must a list of values be passed to an initialiser_list parameter?
By list initialisation, e.g.
error_msg({“functionX”,”okay”});
When should ellipsis parameters only be used?
When interfacing C++ code to C code that uses a C library facility called varargs.
What is the only thing that a function with void return type can return?
The result of a function that also has void return type.
Is it a good idea to return a reference or pointer to a local object? Why?
No, because the local object will be deleted once it goes out of scope and access to the variable outside the function scope will be undefined.
Can the result of a function be returned using list initialisation?
Yes, in C++11, a braced list of values can be returned to list initialise the return value.
If the braced list is empty, the result will be value initialised.
Can a function return the value of a built-in type using list initialisation?
Yes, but the braced list may only contain one argument, and the returned value must not lead to a narrowing conversion.
When is it valid to return a reference? A reference to const?
When the variable is defined outside the function body, e.g. you return the value of an argument passed by reference.
Indicate whether the following function is legal. If so, explain what it does; if not, correct any errors and then explain it. int &get(int *arry, int index) { return arry[index]; } int main() { int ia[10]; for (int i = 0; i != 10; ++i) get(ia, i) = i; }
The code:
int &get(int *arry, int index) { return arry[index]; } int main() { int ia[10]; for (int i = 0; i != 10; ++i) get(ia, i) = i; }
is legal; it loops over the entries of an array and assigns the i-th entry the value i. This makes use of the fact that the return value of get(…) is a reference.
Is there a problem with the following code to compute the factorial of a function? If so, why?
int factorial(int val) { if (val != 0) return factorial(val-1) * val; return 1; }
If the argument to val is less than zero, this function will continue forever, otherwise it is fine.