Вы находитесь на странице: 1из 7

5.1.

Structured Programming
Introduction
Organizations and individuals are continually searching for more efficient ways to perform the software development process.
One way of lowering the time and cost of development is to standardize software programs and the process of programming. The
benefits of standardized programs are that they are easier to code, maintain, debug, and modify. In recent years, many different
techniques have appeared attempting to minimize differences in the way programmers' design and develop software
Programming Approaches: Structured Vs Non-Structured Approaches
Structured programming is a standardization technique used for software development. The structured approach works by having
all programmers use the same structured design techniques. Structured programming is used to address the shortcomings of nonstructured programming, which frequently used the GO TO branch points to transfer from one part of the program to another
part. Using GO TO codes, one could transfer backward, forward, or anywhere else within the program. The problem is that the
connections between parts of the program by using GO TO commands can become quite haphazard.
The haphazard and sometimes convoluted pattern of linkages between parts of the program is called spaghetti code. This way of
programming is difficult to understand and debug. Non-structured programming is now viewed as an ineffective programming
strategy. To develop good software, developers have to carefully think out and design the programs. In the earliest days of
computing, programmers developed code according to their own whims, with the result that the software was often confusing and
difficult to work with. Software today is expected to follow recognized design principles. The prevailing design standards are
structured programming and structured design.
Structured Programming
Structured programming makes use of the control structures (sequence, selection and repetition). Structured programming does
not use GO TO commands. The principle of sequences implies that program instructions should be executed in the order in which
they appear. The principle of selection implies that instructions may be executed selectively using IF-THEN and/or IF-THENELSE statements. These conditional statements work in the following way. IF a condition is true or is met, THEN a specific set of
instructions will be executed.
If the condition is false, then another set of instructions will be executed. For example, if an employee works for more than 40
hours a week, THEN calculate his gross pay based on an overtime rate of 50% more than his normal hourly pay. If the employee
work 40 hours or less a week, THEN calculate gross pay based on the normal hourly rate of pay. IF-THEN-ELSE works the same
way, but in this case the word ELSE is substituted with a false case, a condition not met. IF the employee works more than 40
hours a week, then calculate gross pay based on a time and a half the rate, ELSE calculate gross pay based on the normal rate.
Alternatively when there are many options, one can employ the CASE statement. The iteration principle indicates that one part of
the program can be repeated or iterated a limited number of times. In most computer languages, the repetition or iteration may be
activated by using REPEAT ---UNTIL or using the WHILE loop and the FOR loop.
Structured Design
According to structured design principles, a program should be designed from the top-down or bottom-up as a hierarchical series
of modules. A module is a logical way of partitioning or subdividing a program so that each module performs one or a small
number of related tasks.
5.2. Modular Programming

The Modular approach to programming involves breaking a program down into sub-components called modules. Every module
consists of an independent or self-contained set of instructions. Modules are also called as routines, subroutines, or procedures or
subprograms. Each of these modules is designed to perform a specific task in the overall program, like calculating the gross pay
of an employee in a payroll program. The advantage of modular approach is the ability to write and test each module
independently and in some cases reuse modules in another program. A program consists of multiple modules. Additionly, there is
a main module in the program that executes the other modules. You can use the following approaches in order to design a
program consisting of modules.
Top-Down Design or Top-Down Decomposition
One programming approach that is proven to be most productive is called top-down decomposition. This is the process of
breaking the overall procedure or task into component parts (modules) and then subdividing each component module until we
reach the lowest level of detail. It is called top-down design or top-down decomposition since we start "at the top" with a general
problem and design specific solutions to its sub problems. To obtain an effective solution for the main problem, it is desirable that
the sub-problems (subprograms) should be independent of each other. Then each sub-problem can be solved and tested by itself.
The top level of the decomposition is the level of the overall plan. Unfortunately there is no single formula that will decompose a
complex problem into individual tasks. The strategy involves top-down reduction of the processing until a level is reached where
each of the individual processes consists of one self-contained task, which is relatively easy to understand and can be
programmed in a few instructions. This stepwise process may create multiple layers or levels of modules beginning with a main
module at the top. The middle level might contain intermediate modules, and minor modules at the third level. From the top the
program is developed in a stepwise fashion. Using this method, a complex problem is separated into simpler parts, which can be
easily programmed. At every stage, the instructions or steps can be checked for errors in logic, corrected or modified without any
effect on the other subprograms. The result will be a truly modular program that satisfies the requirement, which says, 'a good
program can be easily modified'. This programming strategy focuses on developing a software program conceptually before
coding commences. A programming team will likely create a diagram that looks much like an organizational chart with the main
module at the top and subordinate modules down below connected by lines with each box representing a program module. The
chart shows how modules relate to each other but does not depict the details of the program instructions in every module. The
structure chart is often referred to as a Hierarchical Program Organization (HIPO).
Example top-down decomposition
The company's payroll system can contain the following modules or tasks
1)Master file 2) Earnings 3) Deductions 4)Taxing 5)Net earning 6)Print reports
The tasks given above can be shown by a hierarchical chart (Hierarchical Program Organization) as shown below.

Identifying and diagramming the relationship between modules in this fashion allows programmers to focus on the overall
organization and logic of the program without getting bogged down in the details. After the overall structure of the program is
finalized, detailed coding of individual modules can proceed. It is the set of principles that enable a problem to be solved by
breaking it down into manageable smaller parts, step by step from the overall problem specification, in stages through to the
actual code.
Bottom-up Design
Already existing facilities/designs are used/taken into consideration as a model for a new (better) design. With the use of Bottomup design strategy, we take an already existing computer program as a model for our new program. We try to utilize the existing
facilities or design in a way, which gives out program a better performance.
Stepwise Refinement
Stepwise refinement is a top-down design strategy. The program is developed by successively refining levels of procedural detail.
In every refinement step, one or several instructions of the given program are decomposed into more detailed ones. This
successive decomposition or refinement of specifications terminates when all instructions are expressed in terms of any
underlying computer or programming language. Each step of refinement implies some design decisions. It is important that the
programmer is aware of the underlying criteria.
Sub Programs
In top down design, the problem is broken down into sub-problems. The main problem is solved by the corresponding main
program. The sub-problems solutions are provided by subprograms, known as subroutine, functions, or procedures depending on
the programming language. A subprogram executes or performs the computer instructions required to solve a given subproblem.
User defined Sub Programs and Functions
A Sub Program is a part of a program that performs one or more related tasks, has its own name, is written as a separate part of
the program, and is accessed via a call statement. A Function returns a value to the program and shares many of the same
characteristics as a Sub Program (e.g., has its own name, is written as a separate part of the program).

Pros and Cons of Using Procedures

Disadvantage -- Procedure calls consume execution time and memory.

Advantage -- Procedures make the program easier to write, test, debug and maintain.

Within a program we can normally identify sub-tasks that are performing a specific function. Where program features clearly
identify sub-tasks it is good practice to implement each sub-task on its own, stand-alone, sub-program.
There are considerable advantages of a separate sub-program such as:

A stand-alone sub-program can be developed and tested independently.

Once programmed, the sub-program can be called a number of times within a program.

The sub-program is called by a meaningful name chosen by the programmer, which helps greatly in understanding the
code.

The sub-program can be re-used in subsequent programs.

The ability to reuse a sub-program is obviously useful. We can include an existing subprogram in another program whenever you
need it.
Procedures and Functions
A function or procedure is a sub-program that exists to perform a specific, single task. It has its own inputs and its own outputs
(often called parameters), it can also define its own variables. It may return the values to the referencing or invoking program or
procedure A function is a special sort of procedure that has a single output.
Procedures
A procedure has a header, followed by declarations of variables used within the procedure known as local variables (identifiers)
and finally statements that are to be performed when the procedure is invoked. The procedure header is the mechanism used to
give a name to the procedure. The procedure header also may contain the values that can be communicated between the
procedure and its invocation known as parameters.
The common structure for defining a procedure (in C++) is:
void Procedure_Name(parameter)

{
. //List of Statements.
}
Example:
void WelcomeNote ()
{
Cout<<Say Hello to Programming Concepts;
}
Procedures in c++ start with the keyword void.

Functions
The general structure for defining a function (in C++) is:
Data_type Function_Name(parameter)
{
.
.
Return statement;
}
Example:
float Cube (float x)
{
float product;
product = x*x*x;
return product;
}

The most important thing to note is that the function must have a result type which dictates what type of value the function
represents.

A function is a special sort of sub-program - it is used when a sub-program is needed which takes a number of inputs and returns
a single output value (as with cube above). This is typical of many algebraic functions (sine, cosine, exp, log, cube) but is
otherwise fairly unusual since the sub-programs that we want to define quite often have either no output at all or very many
outputs - in these cases we use a procedure.
Parameters
As we've seen, procedures and functions can take a list of quantities called parameters. Initially you might like to regard the
parameters as the inputs to the module (but this is not always true).
For example:
void sumAndDifference( float a, float b)
{
a = a+b;
b = a-b;
cout<<"The sum is "<<a;
cout<<"The difference is"<<b;
}
Both total and difference are local variables and only exist within the procedure, as soon as the procedure finishes they and their
values disappear. The following program is an example of the main program:
void main()
{
float val1, val2;
val1=2;
val2=4;
sum_and_difference(val1,val2);
cout<<val1;
}
The values of val1 and val2 are simply copied into the variables a and b of the procedure sumAndDifference. At the end of the
procedure the values are not copied back! The value of val1 that the final output statement displays is therefore 2 (not 0).
Sometimes however, we would like the values to be copied back at the end of the procedure. This is particularly true if there is
more than one output of the procedure, as in the case of sumAndDifference. The sum and difference cannot be passed back
together as the return value of a function, as a function can return only a single value.
void sumAndDifference( float a, float b, float &s, float &d)

{
s =a+b;
d =a-b;
}
void main()
{
float val1, val2;
float total, difference;
val1=2;
val2=4;
sum_and_difference(val1,val2, total, difference);
cout<<The sum is <<total;
cout<<The difference is<<difference;
}
The symbol & in the procedure parameter list instructs the computer to copy back any changes to the values of a and b once the
procedure has completed. In summary, if you want to write a sub-program that has a number of inputs and a single output, write a
function. If you want many outputs from the sub-programs, use a number of var parameters in a procedure.
Built in functions
While most programming languages enable you to write your own functions (as above), most also provide a library of common
functions ready for you to use (e.g. sine, cosine).
A common example of built-in function is the Printf function in C language. (Or any function used for printing out values in
console in any language; System.Out.Println function in case of Java). This function is implemented and is available to the
programmer to use any time he wants. Normally, the programmer is not bothered how the function is implemented in the
framework or how the compiler of the language work with this function.
For further reading you may refer the websites below.
http://www.maths.udsm.ac.tz/mujuni/mt512/Programming%20Approaches.pdf

Вам также может понравиться