CIS 250 - functions
Objectives
- create functions to solve problems and modularize programs
- send data to functions for processing
- use data returned from functions
- create function prototypes
- discuss variable scope and lifetime
- discuss differences between passing by-value vs. by-reference
- discuss reference variables
- define the term recursion
Functions
Additional function topic outline
- variable scope (local vs. global)
- duplicate variable names with different scope
- variable lifetime
- static local variables: retain value between function executions
- default arguments: must be rightmost parameters/arguments
- overloading functions: same function name, but different formal parameter list
- function header, function body, function signature
- passing by-value vs. by-reference
- reference variables
- using a comma-separated list as a return value
- signifying global scope using the scope resolution operator
Recursion
Recursion is when a function calls itself either directly or indirectly. We will look
at some examples in class.
- recursion exists to make the programmer's life easier
- any recursive algorithm can be rewritten using repetition structures
- recursion usually uses many computer resources such as processing cycles
and memory, so iterative alternatives may be preferable if time critical
or memory intensive situations
- testfunc11.cpp: recursive and non-recursive factorial functions
- testfunc17.cpp: recursive and non-recursive GCD functions
- testfunc16.cpp: recursive Towers of Hanoi solver
Demonstration programs