CS 102
[Schedule] [Examples] [Programs] [Notes & Reference] [Syllabus] [Lab & TA] [Tests] [Grades]

Functions

Examples, Why, Syntax, Scope, Parameters, Recursion, Storage Classes, Macros

Examples

  1. 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...)
  2. Computing the square of a number: square.c

Why use Functions?

  1. Avoid redundancy: write it only once. Makes maintenance easier, more efficient.
    1. E.g. use system's predefined functions: math.h, random number generator: random.c
    2. E.g. Help organize large programs: #include of your own stuff.
  2. 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

  1. A basic function:
         returnType FunctionName( parameterList...)
         {
              ... 
         }
  2. Declaration: stating existance of function name, return type & parameter types (Just the "header")
  3. Definition: The declaration plus the actual "guts" of the code (Includes the "body")
  4. Position of declarations relative to main()

Enclosing scope

  1. E.g. scope.cpp
  2. 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

  1. Passing in values using parameters
    E.g. Pass two numbers to a function to be added: parameters1.c
  2. Call by value vs. call by reference
    1. Program to swap 2 numbers
      1. 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.
      2. 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:
        1. Call with an ampersand '&'
        2. Catch with an asterisk
        3. Use with an asterisk
    2. Confusing program dealing with parameters:
      1. version in C++: confuse1.cpp
      2. version in C: confuse2.c
  3. 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

  1. Non-recursive version of factorial: factorial1.c
  2. Recursive version of factorial: factorial2.c
    In an attempt to understand how recursion works, try both of the following:
    1. Write as multiple functions with different names, calling each other
    2. Write in outline form (compress the function body to make this easier to write)
  3. Other examples
    1. Printing on the way in to the recursion ("head" recursion): reverseDigits.c
    2. Printing on the way out of the recursion ("tail" recursion): reverseText.c
    3. 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]