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.