<< Chapter < Page Chapter >> Page >
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.

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){ // Inputcout<<"\nEnter the age of the first person --->: "; cin>>age1; cout<<"\nEnter the age of the second person -->: "; cin>>age2; // Processanswer = (age1 + age2) / 2.0; // Outputcout<<"\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 //******************************************************

Got questions? Get instant answers now!

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Programming fundamentals - a modular structured approach using c++. OpenStax CNX. Jan 10, 2013 Download for free at http://cnx.org/content/col10621/1.22
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Programming fundamentals - a modular structured approach using c++' conversation and receive update notifications?

Ask