Contents
Scope
- region of code where a name (e.g., function, variable) is visible
- denotes a variable's lifetime (except those with static storage duration)
- name lookup
- searches from innermost to outermost scope
- ends when first name is found
- scope resolution operator (::)
- without scope qualifier refers to global namespace (e.g., ::variable_name)
- example
Global Variables
- In general, do NOT use
- difficult to track read/writes
- difficult to reason and enumerate effects of changes to globals
- a function may give different results with identical input
- value of a function may be context dependent
- often better if value is the same whenever it is evaluated (e.g., assignment 1, assignment 2, )
- rule of thumb: minimize variable scope
Function Overloading
- more than one function shares same name
- different parameter lists (number of parameters, parameter type)
- function invoked depends on type of arguments
- ambiguous calls result in an error
- example
Function Default Parameters
- caller has the option to specify parameter value
- defaults must be at the end of the parameter list
- example
Recursion
- programming solutions often developed by decomposing a problem into smaller subproblems
- combining solutions to subproblems result in problem solution
- recursion can be used if a solution is formulated in terms of solutions to smaller instances of the same problem