W4: FUNCTIONS Flashcards
W4-Q1: What does well-designed function exhibit?
high COHESION and low COUPLING
W4-Q2: What are CLOSURES?
_ Nested types can access variables within the scope of their host function.
_ Such types couple their logic to their nesting environment => CLOSURES
W4-Q3: List 2 SYNTACTIC IMPROVEMENTS.
_ type-inference declaration
_ trailing return type declaration
W4-Q4: What is a FUNCTION POINTER?
_ A function POINTER holds the ADDRESS of a function type
_ The address identifies the LOCATION IN MEMORY where control is transferred to start executing the function’s code.
W4-Q5: What is a FUNCTION OBJECT?
_ is an OBJECT for which the function CALL operator has been OVERLOADED.
_ Since a function object has a TYPE, we can pass it to any other function in the SAME way that we pass an object.
_ Unlike a function POINTER, a function object can store a state.
_ We use function objects to perform the SAME operation in several different parts of an application.
W4-Q6: What is a LAMBDA EXPRESSION?
_ A function OBJECT that is called only ONCE can be replaced by a LAMBDA EXPRESSION
_ A lambda expression is an ANONYMOUS function nested within the BODY of another function.
_ It represents an UNNAMED function object that can capture variables within the scope of its caller.
_ It consists of its own FUNCTION BODY and a CAPTURE-LIST that references the NON-LOCAL variables accessed by that body.
_ We call a lambda expression with its referencing environment a closure.
_ A CLOSURE, unlike a function pointer, has DIRECT access to non-local variables.
W4-Q7: Describe characteristic of a function type with external/ internal LINKAGE
_ A function type can have either external or internal linkage.
_ A function type with EXTERNAL linkage is VISIBLE outside its translation unit, while a type with INTERNAL linkage is INVISIBLE outside its translation unit.
_ The DEFAULT linkage for a function type is EXTERNAL.
=> We could specify this redundantly using the keyword extern.
_ The main() function of every application must have external linkage.
_ To specify INTERNAL linkage, we preface the function declaration with the keyword static.
W4-Q8: What is RECURSIVE FUNCTION?
_ is a function that calls itself from within its own body.
_ Recursive functions require an EXIT CONDITION that determines when the recursion terminates.
_ Once recursion terminates control begins stepping back through the function call stack to the initial caller.
_ The exit condition prevents stack overflow caused by an ever increasing set of recursive calls.