Examples
- Illustrating transfer of control:
Examples: transferControl.c,
bottles.c, morse.c
(morse.c one passes values to functions, which we don't know about yet...)
- Computing the square of a number: square.c
Why use Functions?
- Avoid redundancy: write it only once. Makes maintenance easier, more efficient.
- E.g. use system's predefined functions: math.h, random number generator:
random.c
- E.g. Help organize large programs: #include of your own stuff.
- Miller's Magic number 7 +/- 2: Problem decomposition
If this piques your interest, take a look at the original
article:
Miller, George A. 1956.
The magic number seven, plus or minus two: Some limits on our capacity for
processing information. Psycholgical Review 63: 81-97.
Syntax for function declaration and definition
- A basic function:
returnType FunctionName( parameterList...)
{
...
}
- Declaration: stating existance of function name, return type & parameter
types (Just the "header")
- Definition: The declaration plus the actual "guts" of the code (Includes
the "body")
- Position of declarations relative to main()
Enclosing scope
- E.g. scope.cpp
- Process: Draw enclosing boxes around each function, including main() and
the entire program. When looking for a variable, look in the current box or
outside the current box. You cannot look into another box.
Parameters
- Passing in values using parameters
E.g. Pass two numbers to a function to be added: parameters1.c
- Call by value vs. call by reference
- Program to swap 2 numbers
- swap1.cpp:
version in C++
In C++ to have a parameter's changed value returned to the caller
(i.e. it is a reference parameter), all that is needed is to put an
ampersand '&' between the parameter type and the parameter name.
- swap2.c:
version in C
In C if you want a parameter's changed value to be reflected back
to the calling program you must do the following three things:
- Call with an ampersand '&'
- Catch with an asterisk
- Use with an asterisk
- Confusing program dealing with parameters:
- version in C++: confuse1.cpp
- version in C: confuse2.c
- Again consider the program parameters1.c
that we looked at above. How would you change this to return the answer as
a parameter rather than using the function's return value? Answer: parameters2.c
Recursion: a function can call itself
- Non-recursive version of factorial: factorial1.c
- Recursive version of factorial: factorial2.c
In an attempt to understand how recursion works, try both of the following:
- Write as multiple functions with different names, calling each other
- Write in outline form (compress the function body to make this easier
to write)
- Other examples
- Printing on the way in to the recursion ("head" recursion):
reverseDigits.c
- Printing on the way out of the recursion ("tail" recursion):
reverseText.c
- Misc examples, including exponentiation: recursionExamples.c
You may want to retrieve the Word
document used as a handout for the topic of recursion.
Storage classes: register, static, extern
Macro Substitution: #define for constants
[CS Dept] [UIC]
[Prof. Reed]