<< Chapter < Page | Chapter >> Page > |
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:
The main program must establish the existence of functions used in that program. Depending on the programming language, there is a formal way to:
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:
We are going to consider a simple program that might be used for testing a compiler to make sure that it is installed correctly.
//******************************************************
// 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
//******************************************************
Notification Switch
Would you like to follow the 'Programming fundamentals - a modular structured approach using c++' conversation and receive update notifications?