SESSION 01
SESSION 01
SESSION 01
INTRODUCTION
STRUCTURED PROGRAMMING ELEMENTS
STRUCTURED DESIGN PRINCIPLES
Introduction
Structured programming (sometimes known as modular programming) is an approach to writing
programs that are easier to test, debug, modify and maintain by enforcing a modular approach
which breaks a large complex problem into sub-problems. Let’s have a recap of what
programming, programming language, and unstructured programming are, before continue with
the preamble of structured programming.
Programming means to convert problem solutions into instructions for the computer. It also refers
to the process of developing and implementing various sets of instructions to enable a computer to
do a certain task. A programming language is a vocabulary and set of grammatical rules designed
for instructing a computer to perform specific tasks. In unstructured programs, the statements
are executed in sequence (one after the other) as written. This type of programming uses the GoTo
statement which allows control to be passed to any other section in the program. When a GoTo
statement is executed, the sequence continues from the target of the GoTo. Thus, to understand
how a program works, you have to execute it. This often makes it difficult to understand the logic
of such a program. Due to the difficulty and challenges of unstructured programming, structured
programming was introduced. It was invented to overcome the aforementioned challenges of the
unstructured programming.
Structured programming regularly employs a top-down design model, in which developers
break the overall program structure into separate subsections. A defined function or set of
similar functions is coded in a separate module or sub-module, which means that code can be
loaded into memory more efficiently and that modules can be reused in other programs. After
a module has been tested individually, it is then integrated with other modules into the overall
program structure. Program flow follows a simple hierarchical model that employs looping
constructs such as "for," "repeat," and "while." Use of the "GoTo" statement is discouraged. Most
programs will require thousands or millions of lines of code. (Windows 2000 – over 35 millions
Page 1 of 6
lines of code). The importance of splitting a problem into a series of self-contained modules then
becomes obvious. A module should not exceed 100 lines, and preferably short enough to fit on a
single page or screen. Examples of structured programming languages include C, C+, JAVA, C#,
PYTHON ADA, PASCAL, FORTRAN e t c.
Advantages of Structured programming
i. It is user friendly and easy to understand.
ii. Similar to English vocabulary of words and symbols.
iii. It is easier to learn.
iv. They require less time to write.
v. They are easier to maintain.
vi. These are mainly problem oriented rather than machine based.
vii. Program written in a higher-level language can be translated into many machine
languages and therefore can run on any computer for which there exists an
appropriate translator.
viii. It is independent of machine on which it is used, i.e., programs developed in high
level languages can be run on any computer.
ii. The object code generated by a translator might be inefficient compared to an equivalent
assembly language program.
iii. Data type are proceeds in many functions in a structured program. When changes occur in
those data types, the corresponding change must be made to every location that acts on
those data types within the program. This is really a very time consuming task if the
program is very large.
iv. In a structured program, each programmer is assigned to build a specific set of functions
and data types. Since different programmers handle separate functions that have mutually
shared data type, other programmers in the team must reflect the changes in data types done
by the programmer in data type handled. Otherwise, it requires rewriting several functions.
Page 2 of 6
Elements of Structured Programming
In structured programming design, programs are broken into different functions these functions
are also known as modules, subprogram, subroutines or procedures. Each function is design to do
a specific task with its own data and logic. Information can be passed from one function to another
function through parameters. A function can have local data that cannot be accessed outside the
function’s scope. The result of this process is that all the other different functions are synthesized
in another function. This function is known as main function. Many of the high-level languages
support structured programming. Structured programming minimizes the chances of the function
affecting another. It allows for clearer programs code. It made global variables to disappear and
replaced by the local variables. Due to this change one can save the memory allocation space
occupied by the global variable. There are three basic elements of structured programming which
are briefly discussed as follow.
Modules
In programming, module is a section of code that is added in as a whole or is designed for easy
reusability. It can also be referred to a discrete piece of code which can be independently created
and maintained to be used in different systems. For example, a developer may create a module
containing the code required to use a “sound card” or perform “I/O” on a certain type of file system.
The module can then be distributed for and used by any system that needs that functionality, and
development of the module can proceed independently. This approach is known as modular design.
Subprogram
A Subprogram is a program inside any larger or main program that can be reused any number of
times. The Main Program is suspended during the execution of any subprogram. Moreover, after
the completion of the subprogram, the main program executes from the next sequential address
present in the Program Counter. The Main advantage of Subprogram is that it avoids repetition of
Code and allows us to reuse the same code again and again, at multiple places in a program when
convenient. This reuse results in multiple types of savings, from memory space to coding time.
Such reuse is also an abstraction, for the analysis of subprograms computations are restored in a
program by a statement that calls the subprogram. There are main types of subprogram in
structured programming namely procedures and functions. Procedures − A procedure is defined
Page 3 of 6
as a subprogram that defines parameterized computations. These computations are executed by an
individual call statement. Procedures represent new statements. For example, because Pascal does
not have a sort statement, a user can develop a procedure to sort arrays of records and use a call to
that procedure in place of the unavailable sort statement. The general syntax of a procedure in
Pascal is given as
PROCEDURE Name of Procedure (formal parameter list); {local declaration section}
BEGIN
{instruction sequence}
END;
{end of procedure}
The declaration implies that a procedure has two parts as the specification and the body. The
procedure specification begins with the keyword PROCEDURE and end with the procedure name
or a parameter list. Parameter declarations are optional. Procedures that take no parameters are
written without parenthesis. The procedure body begins with the keyword BEGIN and end with
the keyword END followed by an optional procedure name. The procedure body has three
elements such as a declarative part, an executable part, and an optional exceptional handling part.
Functions − A function is a subprogram that evaluates a value. Functions and procedures are
structured identical, except that Functions are semantically modeled on mathematical functions,
Functions have a RETURN clause, Functions produce no side effects i.e., it changes neither its
parameters nor any variables defined outside the function.
A function has two elements as the specification and the body. The function specification begins
with the Return type followed by name of the function and parameter list. The function body begins
with {and ends with}. The function body has three parts such as a declaration part, an executable
part, and an optional exceptional-handling part.
Page 4 of 6
Subroutine
Subroutines are programs that are used by other routines to accomplish a particular task. A
subroutine can be called from any point within the main body of the program. Frequently, many
programs contain identical sections of code. Instructions can be saved by employing subroutines
that use common sections of code. For example, the sequence of operations needed to generate the
effective address of the operand for instruction is common to all memory reference instructions.
This sequence could be a subroutine that is called from within many other routines to execute the
effective address computation. The instruction that transfers program control to a subroutine is
known by different names. The most common names used are called subroutine, jump to the
subroutine, branch to the subroutine, or branch and save the address. A recursive subroutine is a
subroutine that calls itself. If only one register or memory location can save the return address, and
the recursive subroutine calls itself, it ends the previous return address.
i. Standardization
ii. Abstraction
iii. Expressive Power
iv. Orthogonality
v. Minimality
Standardization: It is always desirable to build software from components that all exhibit the
same external interface. The simple interface comprises one entry point at the start and one exit
point at the end. This has the strength of beings consistent with the essence of sequential
programming. It also conforms to the important idea of calling a procedure. We consider the idea
of calling a procedure as a sequential step and returning from it to the next instruction in sequence.
Other constructs that possess the same single entry at the start single exit at the end property are
if then else and while do.
Page 5 of 6
Abstraction: Abstraction is probably the most important idea in structured programming. The
human mind cannot devise or understand the whole of a complex system. It can only understand
some part of the system. But it is required and essential to understand the whole system. So to
resolve such problem abstraction is used. In abstraction the system must be described in a notation
that allows subsystems to be seen as black boxes whose task is readily understood but whose detail
is invisible. In programming the procedure has long been a mechanism that fulfils this role.
Expressive Power: The go to statement has very less expressive power because one statement of
go to may have 3-4 types of meanings. That’s why we require some mechanism for repetition and
either a while statement or recursion are sufficient to provide this facility. Many languages
provide both a while statement and a do while statement. Most languages also support recursion.
Orthogonality : When designing a set facilities a good design principle is to create features that
different from each other as possible. If this is so we can more easily satisfy the goal of a
minimum number of functions while at the same time ensuring that the facilities are sufficiently
powerful for all our needs.
Minimality: When principle of minimality restricts our tendency to include too many facilities.
So to follow the concept of minimality, three constructs given by Boehm Jacopini are the sufficient
constructs to develop a structured program. There are other control structures that involve only
three or less boxes but from amongst them all this three constructs set is the minimal set.
Page 6 of 6