AN EXAMPLE OF WRITING A C PROGRAM USING MULTIPLE FUNCTIONS


If you are not familiar with functions, you may find it helpful to review the web page entitled Functions and Parameter Passing. Note that this example illustrates only the top-down approach to analysis and design, but does not involve the parameter passing.

The Situation:

You want to display a vertical message consisting of graphic block letters (made from stars) on the screen, such as:

* *
***
* *
---
***
* *
***

* *
***
* *
---
***
* *
***

Notice the redundant (repetitious) use of some of the lines of text above. For example, the block letter "H" is used twice. So is the letter "O". The single line containing the hyphens ("---") is also used twice. If we deal with each block letter and the line of hyphens as separate problems, we can minimize the need for highly repetitive coding later in the programming process.

TOP-DOWN ANALYSIS

Almost any task can be sub-divided into smaller, simpler tasks called "modules". Such an approach is called "top down design". Each module is treated as a separate problem. In the C programming language, modules are written in separate blocks of code called "functions", with a master function named "main" that "calls" (loads and executes) the other functions in whatever order is necessary to produce the overall objectives of the program.

Most problems can be divided (or "structured") in a variety of different ways. For example:

When making a decision about how to structure a problem, you should consider how its separate functions might be used in future programs. In this example, you would probably find that a group of functions that display entire block letters would be more useful than a group of functions that display lines of stars in various patterns. Thus, the first bulleted approach above would be the more useful.

An analyst's approach to top-down design can be illustrated in a form of documentation known as a "structure diagram". The structure diagram below illustrates the first bulleted top-down design approach above. The diagram starts with a general task which is labeled and described in a box at the top, followed by more detailed descriptions on the levels below.

STRUCTURE DIAGRAM

Notice that each function appears in a box with a unique "function identifier" (label) at the top and a brief description of its purpose below. The description of purpose need not be as detailed as a "problem statement", but must provide enough detail to identify the purpose of the function. Although each of the sub-tasks may be "called" more than once by the main function (HOHO), each separate function is shown only once in the structure diagram. Then, each function will be analyzed as a separate (simpler) program and documented accordingly.

When separate functions are used, you should provide separate documentation for each function. This includes a Problem Statement, Data Definition (Sample Outputs, Symbolic Constant Lists, Variable Lists, etc.) and an Algorithm or Flowchart for each function. The Desk Check is performed independently for each function and then overall for the main function.

The remainder of the documentation for this overall problem and its functions appears below.


DOCUMENTATION FOR FUNCTION "main"

PROBLEM STATEMENT

Display the message "HO HO" as a totem pole of block letters (as shown in the Sample Softcopy below) constructed from stars (asterisks) and spaces, 3 lines high by 3 characters wide. Separate each letter in the words of the output with a line of 3 hyphens. Separate each word in the output with a blank line.

SAMPLE OUTPUT (Softcopy):

In the sample output below, the numbers to the left of the sample screen are there only for your reference in other documentation. They are not intended to be part of the output.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

* *
***
* *
---
***
* *
***

* *
***
* *
---
***
* *
***

SYMBOLIC CONSTANT LIST: (None)   VARIABLE LIST: (None)

ALGORITHM FOR THE FUNCTION "main":

  1. Start.
  2. Call function BlockH.
  3. Call function Hyphens.
  4. Call function BlockO.
  5. Skip a line on the screen.
  6. Call function BlockH.
  7. Call function Hyphens.
  8. Call function BlockO.
  9. End.

DESK CHECK:

The desk check cannot be performed until all subordinate functions have been completed. Each function should be checked independently and then an overall check can be performed.


DOCUMENTATION FOR FUNCTION "BlockH"

PROBLEM STATEMENT

Display the block letter "H" constructed from stars and spaces, 3 lines high by 3 characters wide.

SAMPLE OUTPUT (Softcopy):

1
2
3

* *
***
* *

SYMBOLIC CONSTANT LIST: (None)   VARIABLE LIST: (None)

ALGORITHM FOR FUNCTION "BlockH":

  1. Start.
  2. Display a star, a space and another star, followed by a carriage return.
  3. Display three stars followed by a carriage return.
  4. Display a star, a space and another star, followed by a carriage return.
  5. End.

DESK CHECK:

Since this function involves no variables, there is no need for a Tracing Chart.

TEST OUTPUT:

1
2
3

* *
***
* *


DOCUMENTATION FOR FUNCTION "BlockO"

PROBLEM STATEMENT

Display the block letter "O" constructed from stars and spaces, 3 lines high by 3 characters wide.

SAMPLE OUTPUT (Softcopy):

1
2
3

***
* *
***

SYMBOLIC CONSTANT LIST: (None)   VARIABLE LIST: (None)

ALGORITHM FOR FUNCTION "BlockO":

  1. Start.
  2. Display three stars followed by a carriage return.
  3. Display a star, a space and another star, followed by a carriage return.
  4. Display three stars followed by a carriage return.
  5. End.

DESK CHECK FOR FUNCTION "BlockO":

Since this procedure involves no variables, there is no need for a Tracing Chart.

TEST OUTPUT:

1
2
3

***
* *
***


DOCUMENTATION FOR FUNCTION "HYPHENS"

PROBLEM STATEMENT

Display a line of 3 hyphens.

SAMPLE OUTPUT (Softcopy):

1 ---

SYMBOLIC CONSTANT LIST: (None)   VARIABLE LIST: (None)

ALGORITHM FOR FUNCTION "Hyphens":

  1. Start.
  2. Display three hyphens followed by a carriage return.
  3. End.

DESK CHECK:

Since this procedure involves no variables, there is no need for a Tracing Chart.

TEST OUTPUT:

1 ---

DESK CHECK FOR FUNCTION "MAIN":

CHART OF VARIABLE VALUES: (None required - no data)

TEST OUTPUT (Softcopy):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

* *
***
* *
---
***
* *
***

* *
***
* *
---
***
* *
***


PROGRAM CODE (C):

/* hoho.c - A program to display a totem message "HO HO" */
/* Written by: R. Gibson   -   Date: 9/14/2000 */

/* ----- PREPROCESSING DIRECTIVES ------------------------- */

#include <stdio.h> /* load pre-defined code for console I/O */

/* ----- FUNCTION PROTOTYPES ------------------------------ */

/* Function prototypes are statements that declare the labels
   used to name functions prior to using them in the code below */

void BlockH (void);  /* Display the block letter H */
void BlockO (void);  /* Display the block letter O */
void Hyphens (void);  /* Display a line of 3 hyphens */

/* ----- PROGRAM SETUP -----  CONSTANTS & VARIABLES ------- */

/* Note: this program has neither constants nor variables.  */

/* ===== MAINLINE CONTROL ================================ */

int main (void)
{
     BlockH();   /* Call the function without arguments */
     Hyphens();  /* Call the function without arguments */
     BlockO();   /* Call the function without arguments */
     printf("\n");
     BlockH();   /* Call the function without arguments */
     Hyphens();  /* Call the function without arguments */
     BlockO();   /* Call the function without arguments */
     return (0); /* Return zero error code to parent process */
}

/* ===== CHILD FUNCTIONS  ================================ */

/* ----- DISPLAY BLOCK LETTER H ----------------------------*/

void BlockH (void)
{
     printf ("* *\n");
     printf ("***\n");
     printf ("* *\n");
}

/* ----- DISPLAY BLOCK LETTER O ----------------------------*/

void BlockO (void)
{
     printf ("***\n");
     printf ("* *\n");
     printf ("***\n");
}

/* ----- DISPLAY LINE OF 3 HYPHENS -------------------------*/

void Hyphens (void)
{
     printf ("---\n");
}

Go to Home Page     Return to the COP 2000 Home Page

Last Revised: 11 October 2004 © 2004 Randolph Gibson
www.gibson.vero-beach.fl.us/classes/cop2000/example_proc.html E-mail: rgibson@ircc.edu