1/17
Flashcards reviewing key concepts about functions in C++.
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
What are the key components of a C++ function?
A function starts with a return type and signature, including the function name and parameters. The function body is written as a sequence of statements in curly brackets.
What is a function prototype?
A function prototype describes the return type and function signature without revealing its implementation. It is a declaration that provides the compiler with the function name, return type, and parameter types.
How does the compiler use a function prototype?
The compiler uses the prototype to ensure the value returned by the function is used correctly, the function call contains the correct arguments, and the function definition matches the prototype.
What is 'pass by value'?
Pass by value copies the value of the parameters into the function, giving the function access only to the copies. Modifications within the function have no effect on the originals.
What is 'pass by reference' and what does it enable?
Pass by reference enables functions to modify the parameters passed in, using a reference to the original value for access and modification.
What is 'pass by pointer' and how does it allow modification of arguments?
Pass by pointer enables functions to modify the arguments passed to them by accessing the original value through its memory address using a pointer.
How is a reference declared and can it be re-assigned?
A reference acts like a synonym for a value and is declared by adding a & character to its type; it cannot be re-assigned.
What is a pointer and what can it be re-assigned to?
A pointer is a variable that holds a memory address and can be re-assigned to a different memory address.
In pass by value, why does modifying a parameter inside a function not affect the original variable?
With pass by value, modifying x in foo() has no effect on i in main() because x is a copy of i's value.
In pass by reference, why do changes to a parameter inside a function affect the original variable?
Because x is a reference to i, any changes to x in foo() also change i in main().
What is function overloading?
Functions can have the same name if they are distinguishable by the number of parameters or parameter types; this is called function overloading.
How does the compiler determine which overloaded function to call (name mangling)?
The compiler encodes each function identifier with the types of its parameters, allowing it to enable type-safe linkage via name mangling.
What are the components of the encoding (name mangling)?
Starts with __, then Z, length of function name, function name, types' first letter, R for reference.
How do functions return control back to the calling function?
When main() calls function foo(), it transfers control. The function returns control to main() when a value is returned or when it reaches the closing }.
What is an inline function and how does it reduce overhead?
C++ allows inline functions, which reduce function call overhead by inserting the code of the function directly into the calling function at compile time.
What are default arguments?
We can specify default arguments for a function, providing a default value to be passed to a parameter that can be overwritten if a value is passed into the function.
In what order should required and optional parameters be specified?
Required parameters must come before optional parameters with default arguments.
How do default argument values work when calling a function?
When boxVolume() is called without parameters, default values are used; when parameters are passed, the default values are overwritten.