Podcast
Questions and Answers
In C++, what is the main difference between a function's parameters and its arguments?
In C++, what is the main difference between a function's parameters and its arguments?
Parameters are variables in the function definition, while arguments are the actual values passed when the function is called.
Explain the purpose of a 'function prototype' in C++ and why it is important.
Explain the purpose of a 'function prototype' in C++ and why it is important.
A function prototype declares a function's name, return type, and parameters. It's important because it allows the compiler to check calls to the function before its actual definition is encountered.
What is a 'header guard,' and why is it used in C++ header files?
What is a 'header guard,' and why is it used in C++ header files?
A header guard is a preprocessor directive that prevents a header file from being included more than once in a single compilation unit, avoiding redefinition errors.
Explain why C++ is considered a 'statically typed' language in the context of function argument checking.
Explain why C++ is considered a 'statically typed' language in the context of function argument checking.
Describe what happens to a non-reference parameter when passed to a function in C++.
Describe what happens to a non-reference parameter when passed to a function in C++.
Explain the key advantage of using reference parameters over non-reference parameters when passing large objects to a function.
Explain the key advantage of using reference parameters over non-reference parameters when passing large objects to a function.
In the context of C++ functions, what is the significance of the const
keyword when applied to a non-reference parameter?
In the context of C++ functions, what is the significance of the const
keyword when applied to a non-reference parameter?
Explain what happens when a function attempts to return a reference/pointer to a local object that was created within the function.
Explain what happens when a function attempts to return a reference/pointer to a local object that was created within the function.
Describe the purpose of 'default arguments' in a C++ function declaration, and explain a limitation related to their placement.
Describe the purpose of 'default arguments' in a C++ function declaration, and explain a limitation related to their placement.
Explain the difference between an object's 'scope' and its 'lifetime' in C++.
Explain the difference between an object's 'scope' and its 'lifetime' in C++.
What are 'automatic objects' in C++, and how does their lifetime differ from 'local static objects'?
What are 'automatic objects' in C++, and how does their lifetime differ from 'local static objects'?
Describe what a 'temporary object' is in C++ and provide an example scenario where one might be created by the compiler.
Describe what a 'temporary object' is in C++ and provide an example scenario where one might be created by the compiler.
Explain the purpose of an 'inline function' in C++ and what benefit it aims to provide.
Explain the purpose of an 'inline function' in C++ and what benefit it aims to provide.
Describe the special implicit parameter available to member functions in C++ classes, and what this parameter represents.
Describe the special implicit parameter available to member functions in C++ classes, and what this parameter represents.
Explain the purpose of a 'const member function' in C++ classes and what restrictions it enforces.
Explain the purpose of a 'const member function' in C++ classes and what restrictions it enforces.
What is an 'overloaded function' in C++, what is the major rule that governs creating them, and explain a circumstance where function overloading should be avoided?
What is an 'overloaded function' in C++, what is the major rule that governs creating them, and explain a circumstance where function overloading should be avoided?
Outline the three general steps that occur during 'overload resolution' when the compiler encounters a call to an overloaded function.
Outline the three general steps that occur during 'overload resolution' when the compiler encounters a call to an overloaded function.
Explain what 'candidate functions' and 'viable functions' are in the context of overload resolution in C++.
Explain what 'candidate functions' and 'viable functions' are in the context of overload resolution in C++.
What is result of an 'ambiguous call' during overload resolution in C++, and why does it occur?
What is result of an 'ambiguous call' during overload resolution in C++, and why does it occur?
List the typical order of argument-type conversions that the C++ compiler considers (in descending order of preference) during overload resolution.
List the typical order of argument-type conversions that the C++ compiler considers (in descending order of preference) during overload resolution.
Explain the purpose of function pointers in C++ and why parentheses are necessary when declaring them.
Explain the purpose of function pointers in C++ and why parentheses are necessary when declaring them.
Explain why a function return type cannot be a function, but can be a pointer to a function in C++.
Explain why a function return type cannot be a function, but can be a pointer to a function in C++.
Describe what a 'lambda expression' is in C++ and how it relates to inline functions.
Describe what a 'lambda expression' is in C++ and how it relates to inline functions.
What are the two parts of a lambda expression that must be present?
What are the two parts of a lambda expression that must be present?
Explain how the arguments of every function are checked in C++.
Explain how the arguments of every function are checked in C++.
In C++, describe the purpose of the call operator ()
when using functions.
In C++, describe the purpose of the call operator ()
when using functions.
In C++, what is the correct syntax for including a header file and how does it relate to making declarations available?
In C++, what is the correct syntax for including a header file and how does it relate to making declarations available?
Describe the difference between calling a function with a nonreference parameter versus a reference parameter regarding what parameter values can be changed.
Describe the difference between calling a function with a nonreference parameter versus a reference parameter regarding what parameter values can be changed.
Explain the concept of recursion in the context of C++ functions.
Explain the concept of recursion in the context of C++ functions.
In C++, explain with an example how a list can be initialized as a return value from a function.
In C++, explain with an example how a list can be initialized as a return value from a function.
Flashcards
Function Prototype
Function Prototype
Synonym for function declaration. Includes name, return type, and parameter list.
Call Operator
Call Operator
The operator that triggers a function's execution.
Header
Header
Mechanism for making class definitions and other declarations available in multiple source files.
Header Guard
Header Guard
Signup and view all the flashcards
Nonreference Parameters
Nonreference Parameters
Signup and view all the flashcards
Reference Parameters
Reference Parameters
Signup and view all the flashcards
Overloaded Function
Overloaded Function
Signup and view all the flashcards
Function Matching
Function Matching
Signup and view all the flashcards
Candidate Functions
Candidate Functions
Signup and view all the flashcards
Viable Functions
Viable Functions
Signup and view all the flashcards
Ambiguous Call
Ambiguous Call
Signup and view all the flashcards
Best Match
Best Match
Signup and view all the flashcards
Inline Function
Inline Function
Signup and view all the flashcards
this Pointer
this Pointer
Signup and view all the flashcards
Const Member Function
Const Member Function
Signup and view all the flashcards
Automatic Objects
Automatic Objects
Signup and view all the flashcards
Temporary (Object)
Temporary (Object)
Signup and view all the flashcards
Local Static Objects
Local Static Objects
Signup and view all the flashcards
Recursion
Recursion
Signup and view all the flashcards
Function Prototypes
Function Prototypes
Signup and view all the flashcards
object lifetime
object lifetime
Signup and view all the flashcards
Exact match
Exact match
Signup and view all the flashcards
Promotion
Promotion
Signup and view all the flashcards
Standard conversions
Standard conversions
Signup and view all the flashcards
Simplify pointers
Simplify pointers
Signup and view all the flashcards
Lambda expressions
Lambda expressions
Signup and view all the flashcards
Study Notes
Functions: Basic Definitions
- Function prototype is a synonym for function declaration
- Function prototype includes the name, return type, and parameter list
- A function's prototype must be declared before the point of call
- Call operator is the operator that causes a function to be executed
- Call operator consists of a pair of parentheses and takes two operands
- The name of the function to call
- A comma-separated list of arguments
Header Files
- Header files are a mechanism for making class definitions and other declarations available in multiple source files
- Headers are specifically for declarations, not definitions
- Header guard is a preprocessor variable which prevents a header from being included more than once in a single source file
Functions
- Functions must specify a return type
- C++ is statically typed, so arguments of every call are checked during compilation
Argument Passing
- Each parameter is created anew on each call to the function
- Value used to initialize a parameter is the corresponding argument passed in the call
- If a parameter is a nonreference type, the argument is copied
- If a parameter is a reference, it is just another name for the argument
Nonreference Parameters
- Nonreference parameters represent local copies of the corresponding argument
- Changes to the parameter are local to the copy
- Once the function terminates, these local values are gone
- Example:
int fct(int i)
- Pointer parameter example:
int fct(int* i)
- Const parameter example:
int fct(int const i)
Reference Parameters
- Copying an argument is unsuitable for every situation
- The function needs to change the value of the argument
- A large object needs to be passed as an argument
- Reference parameters example:
int fct(int& i)
- Array parameter example:
int fct(int* )
- This is equivalent to:
int fct(int[])
orint fct(int [10])
- Array dimensions are ignored and size is not checked
- This is equivalent to:
- Passing by reference:
int fct(int (&arr)[10]);
the size checked
Argument Passing specifics
- Command line options example:
int main(int argc, char *argv[]) {}
- Functions with varying parameters (old style) example:
void fct(parm_list, ...);
- In C using printf
- Initializer lists parameter example:
void msg (initializer_lists<string> il)
Return Statement
- Functions with no return value example:
void fct() { return; }
- When a function returns a value
- The returned value initializes a temporary object at the point the call was made
- Never return a reference/pointer to a local object
- Reference returns are Ivalues
- List initialization of the return value is possible (C++11)
- Recursion is when a function calls itself again
Function Declarations
- Function prototypes provide the interface between programmer and user
- Source file that defines the function should include the header that declares the function
- Default arguments can be specified in the function definition or declaration, but not both!
- Example:
int fct(int i = 1);
- Example:
Object Lifetime
- Names have scope, objects have lifetime
- Every object has an associated lifetime
- Objects defined inside a block exist from when their definition is encountered until the end of the block
- Local static objects and global objects defined outside any function:
- Created during program startup
- Destroyed when the main function ends
- Dynamically created objects created through a new expression exist until the memory in which they were created is freed through delete
Local Objects
- Automatic objects: Objects local to a function. Automatic objects are:
- Created and initialized on each call
- Destroyed at the end of the block in which they are defined
- No longer exist once their function terminates
- Parameters are examples of automatic objects
- Temporary object: Unnamed object automatically created by the compiler when evaluating an expression
- Persists until the end of the largest expression that encloses the expression where it was created
- Example:
i+j
in expressionint res = i + j + k
- Local static objects
- Guaranteed to be initialized no later than the first time that program execution passes through the object's definition
- Not destroyed until program terminates
- Example:
size_t count() { static size_t ctr = 0; return ++ctr; }
Inline Functions
- Inline function: Expanded at the point of call, if possible
- Inline functions avoid normal function-calling overhead by replacing the call with the function's code
- Inline specification is only a request to the compiler
- Inline functions should be defined in header files to:
- Expand the code
- Ensure the compiler has access to the function declaration
Class Member Functions
- A member function may access private members of its class
this
pointer is an implicit parameter of a member function- It points to the object on which the function is invoked
- It is a pointer to the class type
- In a const member function, the pointer is a pointer to const
- const member function: Is a member of a class that may be called for const objects of that type
- Const member functions may not change the data members of the object on which they operate
- Example:
int MyClass::fct() const {}
Overloaded Functions
- Overloaded function: Function having the same name as at least one other function
- Overloaded functions must differ in the number or type of their parameters
- Main function may not be overloaded
- Keeping function names and operator behavior intuitive prevents the need of overloading
Overload Resolution
- Function matching (overload resolution): Compiler process by which a call to an overloaded function is resolved
- Arguments used in the call are compared to the parameter list of each overloaded function
- In C++, name lookup happens before type checking at compile-time
- Steps in overload resolution:
- Candidate functions
- Determine viable functions (default arguments are treated like any other arguments)
- Find best match, if any
- Candidate functions: Set of functions that are considered when resolving the function call
- All functions with the name used, declaration is in scope at time of call
- Viable functions: Subset of overloaded functions that could match a given call
- The same number of parameters as arguments to the call
- Each argument type can potentially be converted to a corresponding parameter type
- Ambiguous call: Compile-time error
- Occurs when there is not a single best match for a call to an overloaded function
- Best match: Single function from a set of overloaded functions that has the best match for arguments
Argument-type Conversions
- Argument-type conversions in descending order include:
- Exact match: argument and parameter type are the same
- Promotion: integral types like char, short are converted to int
- Standard conversions: like double to int
- Class type conversions
- Arguments should not need casts when calling overloaded functions
- Whether a parameter is const only matters when the parameter is a reference or pointer
Pointers to Functions
- Parentheses around function name are necessary
- Example: bool (*pf)(const string&, const string&)
- Use typedefs to simplify pointer definitions
- Example: typedef bool (*ptrfct)(const string&, const string&)
- Function pointer may be initialized and assigned only by a function (pointer) that has the same type or by a zero-valued constant expression
- Example: ptrfct pf1 = 0;
- A parameter can be defined by a function type
- Example:
void fct (const string&, bool (*) (const string&) );
- This is equivalent to:
void fct (const string&, bool (const string&) );
- Example:
- A function return type must be a pointer to a function, it cannot be a function directly
- Example:
int (*ff(int)) (int*, int);
- This is equivalent to:
typedef int (*PF) (int*, int); PF ff(int);
- Example:
Lambda Expressions
- Lambda expressions are a callable unit of code
- A lambda is somewhat like an unnamed, inline function
- Syntax:
[capture list] (parameter list) -> return type { function body }
- Return type, parameter list, and function body are the same as for ordinary functions
- Capture list is an (often empty) list of local variables defined in the enclosing function
- Capture list and function body are obligatory
- Example:
auto f = [] { return 42; }
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.