Skip to content Skip to navigation

Connexions

You are here: Home » Content » Modularization and C++ Program Layout

Navigation

Lenses

What is a lens?

Definition of a lens

Lenses

A lens is a custom view of the content in the repository. You can think of it as a fancy kind of list that will let you see content through the eyes of organizations and people you trust.

What is in a lens?

Lens makers point to materials (modules and collections), creating a guide that includes their own comments and descriptive tags about the content.

Who can create a lens?

Any individual member, a community, or a respected organization.

What are tags? tag icon

Tags are descriptors added by lens makers to help label content, attaching a vocabulary that is meaningful in the context of the lens.

This content is ...

Endorsed by Endorsed (What does "Endorsed by" mean?)

This content has been endorsed by the organizations listed. Click each link for a list of all content endorsed by the organization.
  • CCQ display tagshide tags

    This module is included in aLens by: Community College of QatarAs a part of collection: "Programming Fundamentals - A Modular Structured Approach using C++"

    Comments:

    "Used in the Computer Programming Fundamentals I course."

    Click the "CCQ" link to see all content they endorse.

    Click the tag icon tag icon to display tags associated with this content.

Affiliated with (What does "Affiliated with" mean?)

This content is either by members of the organizations listed or about topics related to the organizations listed. Click each link to see a list of all content affiliated with the organization.
  • OrangeGrove display tagshide tags

    This module is included inLens: Florida Orange Grove Textbooks
    By: Florida Orange GroveAs a part of collection: "Programming Fundamentals - A Modular Structured Approach using C++"

    Click the "OrangeGrove" link to see all content affiliated with them.

    Click the tag icon tag icon to display tags associated with this content.

  • Houston Community College display tagshide tags

    This module is included in aLens by: Houston Community CollegeAs a part of collection: "Programming Fundamentals - A Modular Structured Approach using C++"

    Comments:

    "COSC1436 Programming Funaamentals I"

    Click the "Houston Community College" link to see all content affiliated with them.

    Click the tag icon tag icon to display tags associated with this content.

  • Featured Content display tagshide tags

    This module is included inLens: Connexions Featured Content
    By: ConnexionsAs a part of collection: "Programming Fundamentals - A Modular Structured Approach using C++"

    Comments:

    "Programming Fundamentals - A Modular Structured Approach Using C++ is a new course written by Kenneth Leroy Busbee, a faculty member at Houston Community College. This text introduces students to […]"

    Click the "Featured Content" link to see all content affiliated with them.

    Click the tag icon tag icon to display tags associated with this content.

Also in these lenses

  • Busbee's Compter Science display tagshide tags

    This module is included inLens: Busbee's Computer Science Lens
    By: Kenneth Leroy BusbeeAs a part of collection: "Programming Fundamentals - A Modular Structured Approach using C++"

    Comments:

    "Texas Common Course Numbering: COSC1336 or COSC1436"

    Click the "Busbee's Compter Science" link to see all content selected in this lens.

    Click the tag icon tag icon to display tags associated with this content.

  • Lens for Engineering

    This module is included inLens: Lens for Engineering
    By: Sidney Burrus

    Click the "Lens for Engineering" link to see all content selected in this lens.

  • eScience, eResearch and Computational Problem Solving

    This module is included inLens: eScience, eResearch and Computational Problem Solving
    By: Jan E. OdegardAs a part of collection: "Programming Fundamentals - A Modular Structured Approach using C++"

    Click the "eScience, eResearch and Computational Problem Solving" link to see all content selected in this lens.

Recently Viewed

This feature requires Javascript to be enabled.

Tags

(What is a tag?)

These tags come from the endorsement, affiliation, and other lenses that include this content.
 

Modularization and C++ Program Layout

Module by: Kenneth Leroy Busbee. E-mail the author

Summary: An introduction of the concepts of modular programming with a simple example of program control and specific task functions using a C++ source code listing. The general layout of a C++ program is described.

Concept of Modularization

One of the most important concepts of programming is the ability to group some lines of code into a unit that can be included in our program. The original wording for this was a sub-program. Other names include: macro, sub-routine, procedure, module and function. We are going to use the term function for that is what they are called in the two predominant programming languages of today: C++ and Java. Functions are important because they allow us to take large complicated programs and to divide them into smaller manageable pieces. Because the function is a smaller piece of the overall program, we can concentrate on what we want it to do and test it to make sure it works properly. Generally functions fall into two categories:

  1. Program Control - Functions used to simply sub divide and control the program. These functions are unique to the program being written. Other programs may use similar functions maybe even functions with the same name, but the content of the functions are almost always very different.
  2. Specific Task - Functions designed to be used with several programs. These functions perform a specific task and thus are useable in many different programs because the other programs also need to do the specific task. Specific task functions are sometimes referred to as building blocks. Because they are already coded and tested, we can use them with confidence to more efficiently write a large program.

The main program must establish the existence of functions used in that program. Depending on the programming language, there is a formal way to:

  1. define a function (it's definition or the code it will execute)
  2. call a function
  3. declare a function (a prototype is a declaration to a complier)

Program Control functions normally do not communicate information to each other but use a common area for variable storage. Specific Task functions are constructed so that data can be communicated between the calling program piece (which is usually another function) and the function being called. This ability to communicate data is what allows us to build a specific task function that may be used in many programs. The rules for how the data is communicated in and out of a function vary greatly by programming language, but the concept is the same. The data items passed (or communicated) are called parameters. Thus the wording: parameter passing. The four data communication options include:

  1. no communication in with no communication out
  2. some communication in with no communication out
  3. some communication in with some communication out
  4. no communication in with some communication out

Introduction of Functions within C++

We are going to consider a simple program that might be used for testing a compiler to make sure that it is installed correctly.

Example 1: Compiler_Test.cpp source code


//******************************************************
// Filename: Compiler_Test.cpp
// Purpose:  Average the ages of two people
// Author:   Ken Busbee; © Kenneth Leroy Busbee
// Date:     Jan 5, 2009
// Comment:  Main idea is to be able to 
//           debug and run a program on your compiler.
//******************************************************

// Headers and Other Technical Items

#include <iostream>  
using namespace std;

// Function Prototypes

void pause(void);

// Variables

int     age1;
int     age2;
double  answer;

//******************************************************
// main
//******************************************************

int main(void)
  {
  // Input	
  cout << "\nEnter the age of the first person --->: ";
  cin >> age1;
  cout << "\nEnter the age of the second person -->: ";
  cin >> age2;

  // Process
  answer = (age1 + age2) / 2.0;

  // Output
  cout << "\nThe average of their ages is -------->: ";
  cout << answer;

  pause();
  return 0;
  }

//******************************************************
// pause
//******************************************************

void pause(void)
  {
  cout << "\n\n";
  system("PAUSE");
  cout << "\n\n";
  return;
  }

//******************************************************
// End of Program
//******************************************************

This program has two functions, one from each of our categories. The technical layout of functions are the same, it is our distinction that creates the two categories based on how a function is being implemented.

Program Control Function

The main program piece in C++ program is a special function with the identifier name of main. The special or uniqueness of main as a function is that this is where the program starts executing code and this is where it usually stops executing code. It is usually the first function defined in a program and appears after the area used for includes, other technical items, declaration of prototypes, the listing of global constants and variables and any other items generally needed by the program. The code to define the function main is provided; however, it is not prototyped or usually called like other functions within a program. In this simple example, there are no other program control functions.

Specific Task Function

We often have the need to perform a specific task that might be used in many programs. In the Compile_Test.cpp source code above we have such a task that is used to stop the execution of the code until the user hits the enter key. The functions name is: pause. This function is not communicating any information between the calling function and itself, thus the use of the data type void.

Example 2: general layout of a function


<return value data type> function identifier name(<data type> <identifier name for input value>)
  {
  lines of code;

  return <value>;
  }

There is no semi-colon after the first line. Semi-colons are used at the end of a statement in C++, but not on the first line when defining a function. Functions have a set of braces {} used for identifying a group or block of statements or lines of code. There are normally several lines of code within a function. Lines of code containing the instructions end in a semi-colon. Can you identify the definition of the pause function in the above program example? The pause function definition is after the function main. Though not technically required, most programs list all functions (program control or specific task) after the function main.

Let's identify the location where the function pause is called. The calling function is the function main and it towards the end of the function. The line looks like:

pause();

When you call a function you use its identifier name and a set of parentheses. You place any data items you are passing inside the parentheses, and in our example there are none. A semi-colon ends the statement or line of code. After our program is compiled and running, the lines of code in the function main are executed and when it gets to the calling of the pause function, the control of the program moves to the pause function and starts executing the lines of code in the pause function. When it’s done with the lines of code, it will return to the place in the program that called it (in our example the function main) and continue with the code in that function.

Once we know how to define a function and how to call a function, we usually will need to know how to declare a function to the compiler (called a prototype). Because of normal computer programming industry standards, programmers usually list the function main first with other functions defined after it. Then somewhere in the function main, we will call a function. When we convert our source code program to an executable version for running on our computer, the first step of the process is compiling. The compiler program demands to know what the communication will be between two functions when a function is called. It will know the communication (what going in and out as parameters) if the function being called has been defined. But, we have not defined that function yet; it is defined after the function main. To solve this problem, we show the compiler a prototype of what the function will look like (at least the communication features of the function) when we define it.

void pause(void);

This line of code looks exactly like the first line in our function definition with one important addition of a semi-colon. Prototypes (or declarations to the compiler of the communications of a function not yet defined) are placed near the top of the program before the function main. Summary concept: If you call a function before it is defined you must prototype it before it is called. Looking at our list of the three things you do in conjunction with a function in the order that they normally appear in a program, there is a formal way to:

  1. declare a function (a prototype is a communications declaration to a complier)
  2. call a function
  3. define a function

C++ Program Layout

From the above example, you can see that 2/3 of the program is the two functions. Most C++ programs have several items before the function main. As in the example, they often are:

  1. Documentation – Most programs have a comment area at the start of the program with a variety of comments pertinent to the program. Any line starting with two slashes // is a comment and the compiler software disregards everything from the // to the end of the line.
  2. #include<iostream> – This line of code inserts a file into the source code. The file contains necessary code to be able to do simple input and output.
  3. using namespace std – The C++ compiler has an area where it keeps the identifier names used in a program organized and it is called a namespace. There is a namespace created in conjunction with the iostream file called: std. This line informs the compiler to use the namespace std where the identifier names in the iostream are established.
  4. Function prototypes have already been explained.
  5. We need some variables (storage areas) for this program to work. They are defined next.

Definitions

Definition 1: modularization
The ability to group some lines of code into a unit that can be included in our program.
Definition 2: function
What modules are called in the two predominant programming languages of today: C++ and Java.
Definition 3: program control
Functions used to simply sub divide and control the program.
Definition 4: specific task
Functions designed to be used with several programs.
Definition 5: parameter passing
How the data is communicated in to and out of a function.
Definition 6: identifier name
The name given by the programmer to identify a function or other program items such as variables.
Definition 7: function prototype
A function's communications declaration to a complier.
Definition 8: function call
A function's using or invoking of another function.
Definition 9: function definition
The code that defines what a function does.
Definition 10: braces
Used to identify a block of code in C++.

Content actions

Download module as:

PDF | EPUB (?)

What is an EPUB file?

EPUB is an electronic book format that can be read on a variety of mobile devices.

Downloading to a reading device

For detailed instructions on how to download this content's EPUB to your specific device, click the "(?)" link.

| More downloads ...

Add module to:

My Favorites (?)

'My Favorites' is a special kind of lens which you can use to bookmark modules and collections. 'My Favorites' can only be seen by you, and collections saved in 'My Favorites' can remember the last module you were on. You need an account to use 'My Favorites'.

| A lens I own (?)

Definition of a lens

Lenses

A lens is a custom view of the content in the repository. You can think of it as a fancy kind of list that will let you see content through the eyes of organizations and people you trust.

What is in a lens?

Lens makers point to materials (modules and collections), creating a guide that includes their own comments and descriptive tags about the content.

Who can create a lens?

Any individual member, a community, or a respected organization.

What are tags? tag icon

Tags are descriptors added by lens makers to help label content, attaching a vocabulary that is meaningful in the context of the lens.

| External bookmarks