Function Design Pattern

No matter what the purpose of a function is, the "syntax" for defining it is always the same. A Design Pattern is presented below to show this syntax. Your job is to memorize this design pattern, but until you do, you can cut and paste it from here.

Function Design Pattern

All functions in C always use the same basic pattern (syntax). This syntax can be captured in a "Design Pattern". Your job is to memorize this pattern for future use (and tests). When learning, you can refer here for help.


The Basic DP

All C functions will be written following the below "pattern".

        
/*
 * COMMENT as to what function does
 */

RETURN_TYPE
name_of_function( TYPE parameter1, TYPE parameter2, etc )
{

  // CODE goes here

  return 0; //or other default.  

}
        
      

Above the word TYPE (or RETURN_TYPE) represent one of the basic data types in C, including numbers (int, float, or double), bools, chars, arrays, and structures. RETURN_TYPE can be any of these except arrays.


Parts of the Function Design Pattern

Again, a function declaration is always of the form:

          RETURN_TYPE name( TYPE parameters, ... )
      
  1. RETURN_TYPE - char, bool, int, float, void, or a structure type.

  2. name - a symbolic name telling the user of the function the purpose of the function. In the case above, the name is average.

  3. TYPE parameter, TYPE parameter, etc - symbolic names for the inputs to the function (the data the function will process to compute an answer). The TYPE must be pre-specified for all C parameters.


The function Prototype or Declaration.

The very first thing you see for every function (after the comments) is the line with the keyword function on it. This line is called the function "prototype" or "declaration".

        
  float average(float first_value, float second_value);  // DECLARATION at top of C file

  ...

  // Somewhere in C file (or in another file)
  // repeat prototype (without ;) and add code
  float average(float first_value, float second_value)
  {
     return  ( first_value + second_value ) / 2.0;
  }

  ...
        
      

Note: You can place a copy of this line at the top of your ".C" file with a semi-colon (;) after it to tell the program that such a function will be defined later on in your code. Additionally you can put the prototype (with ;) in a .h file to let other files know about the function.

It is important to know that this line Doesn't Do Anything! In other words, this line is a syntactic pattern that tells C important information about the function. The line is never executed in your program, and can be considered a "Dummy" Statement, meaning not really part of the function.

Think of it this way, the function prototype is like the Cover of a book, it shows you the title, the author, maybe tells you a comment about the book, but if removed the book still contains the important parts of the book (the text).

Warning: Your prototype at the top of the file must exactly match the function declaration associated with the actual code.

In the average function above, the first actual line of the function is:

        
          return = (first_value + second_value) / 2.0;
        
      

A Very Simple Function File

Here is an example of a function that computes the average of two numbers. It shows the use of a return value, two parameters, comments, and computation. This function would be saved with the name "average.m".

        
  /*
   * Author: H. James de St. Germain
   * etc....
   */
        
  float
  average(float first_value, float second_value)
  {
    return (first_value + second_value) / 2.0;
  }
        
      

Another Sample Function (approximating pi)

        

 /*
  * float approximate_pi( int steps_to_take )
  *
  * Author:  H. James de St. Germain
  * Date:    Oct 1, 2005
  * Partner: I worked alone on this program
  * Course:  Computer Science 1000
  *
  * Function Description:
  *
  *    This program approximates the value of pi by expanding a
  *    mathematical series.
  *
  * Input Parameters:
  *
  *         steps_to_take: (integer number) how close an 
  *                        approximation to make.
  *
  * Returned Value: 
  *
  *         approx_value_of_pi: (floating point number)  This
  *                            value will be some approximation of pi.  The
  *                            more steps, the closer the approximation.
  *
  * Algorithm:
  * 
  *    The series of interest is:
  *
  *           4/1 - 4/3 + 4/5 - 4/7 + 4/9 - 4/11 + ....
  *
  *    At each step, 4 is divided by the next odd number. Then this
  *    value is alternatively subtracted and then added.
  *
  *
  * Example of Use:
  *
  *    approximate_pi( 1 )
  *   
  *    returns the float value:       4.0
  */


  float
  approximate_pi( int steps_to_take )
  {

    float  approx_value_of_pi = 4.0;
    bool   time_to_subtract = true;

    for (int i = 2; i<steps_to_take; i++)
      {
        if (time_to_subtract)
          {
             approx_value_of_pi = approx_value_of_pi - (4 / ((2*i)-1));
          }
        else
          {
             approx_value_of_pi = approx_value_of_pi + (4 / ((2*i)-1));
          }

        time_to_subtract = ! time_to_subtract;
      }
   }
        
      

Back to Topics List