Introduction to Modular Programming Design
Built-in Math Functions
User-Defined Functions
Modular Programming Design You should strive for modularity in all phases of the problem-solving process, beginning with the initial design of the solution. A module is a self-contained unit of code. Modularity describes a program that is organized into loosely coupled (independent), cohesive modules. A top-down design will lead to a modular solution. Functions allow the programmer to modularize a program.
Math Library Functions We have seen some standard functions such as as sqrt that are available in the math library. To access these functions we include the header file math.h. A list of commonly used math library functions is given on page 147 of text.
User-Defined Functions
A user-defined function is a subprogram designed by the programmer that performs a task. A function is given a name and called or invoked, using its name each time the task is to be performed within the program. The program that calls, or invokes, a function is the calling program. There are major advantages in using functions;
(1) They facilitate modular design of programs. Make your program more readable. The main program and all functions should be relatively small.
(2) They eliminate the need for duplicate statements within a program. Given a task to be performed more than once, the statements are written just once for the function. Then the function is called each time the task must be performed,
(3) If changes need to made in the code for a task which is performed multiple times in the program, the changes need only be made in the function itself and not in the calling program.
From now on you will be expected to use functions in the design of all your programs.
User-Defined Function Format
//FUNCTION HEADER <return data class><function name>(<parameter list>){//BEGIN FUNCTION STATEMENT BLOCK <local constant and variable objects should go here> //FUNCTION STATEMENT or BODY function statement 1; function statement 2; . . . function statement n; return <value>: }//END FUNCTION STATEMENT BLOCK
The function header provides the data interface for the function. It forms a common boundary between the function and its calling program.
Example 1. Give code for a user-defined function that computes the square of an integer.
int Square(int Num)
{
return Num*Num;
}//END Square()
This function has one parameter of type int called Num. It returns a value of class int.
A function that does not have a return value is called a void function. Such functions are often called procedures in other programming languages.
Example 2. Give code for a user-defined function that displays the purpose of the program.
void ProgramPurpose()
{
cout << "\nThe purpose of this program is to read in an integer";
cout << "\nand compute and display its square." << endl;
}//END ProgramPurpose()
This function is a void function with no parameters. Note that it is still necessary to include the round brackets after the function name.
Example 3. Give code for a user-defined function that displays an input symbol a given number of times.
void RepeatSymbol(char Symbol,int Multiplicity)
{
for(int Counter = 1; Counter < Multiplicity; ++Counter)
cout << Symbol;
}//END RepeatSymbol()
This function is a void function with two parameters, one called Symbol of type char and the other called Multiplicity of type int.
Actual Arguments vs. Formal Parameters
Actual arguments are values/variables used within the function call.
Formal parameters are variables used with the function header.
The number and type of actual arguments must MATCH the number and type of formal parameters., e.g.
int AFunction(int P1, float P2, char P3, double P4);
In the calling program AnyFunction is invoked as follows:
AFunction(A1,A2,A3,A4)
where A1, A2, A3, A4 are of type int, float, char, double, respectively.
The identifier name used for an actual argument need not be the same as the identifier name used for its corresponding parameter.
This is important because it allows you to design the function independent of the calling program. When writing the function you do not have to be concerned about what identifier names will be used for the actual arguments.
We now give a complete program that invokes the functions described above. Note that a function prototype needs to be given for each function used in the program. It consists simply of the header statement for the functions and is included after the preprocessor directives. In the prototype, unlike the actual function header, the use of the parameter identifier name is optional.
//PREPROCESSOR DIRECTIVES
#include <iostream.h>
#include <math.h>
//FUNCTION PROTOTYPES
void ProgramPurpose();
int Square(int Num);
void RepeatSymbol(char Symbol,int Multiplicity);
//THIS PROGRAM READS IN AN INTEGER AND COMPUTES ITS SQUARE. THE
PROGRAM DESCRIPTION IS BOXED AT THE BEGINNING BY A LINE OF STARS FOLLOWED
BY A LINE OF DASHES, AND AT THE END BY A LINE OF DASHES FOLLOWED BY
A LINE OF STARS. IT ILLUSTRATES FUNCTIONS.
int main()
{
int Number;
RepeatSymbol('*',55);
RepeatSymbol('-',55);
ProgramPurpose();
RepeatSymbol('-',55);
RepeatSymbol('*',55);
cout << "\nEnter integer: ";
cin >> Number;
cout << "The square of " << Number << " is "
<< Square(Number) << "." << endl;
cout << "\n\nPress any key to continue.";
return 0;
}//END main()
//This function displays the program purpose. It is a void function with
//no parameters.
void ProgramPurpose()
{
cout << "\nThe purpose of this program is to read in an integer";
cout << "\nand compute and display its square." << endl;
}//END ProgramPurpose
//This function computes the square of an integer. Note that the
//identifier name Num is used for the formal parameter is different
//from the actual argument Number in the main program.
int Square(int Num)
{
return Num*Num;
}//END Square()
//This is a void function which displays a number of multiple times.
void RepeatSymbol(char Symbol,int Multiplicity)
{
for(int Counter = 1; Counter < Multiplicity; ++Counter)
cout << Symbol;
}//END RepeatSymbol()