Structured Programming & Object-Oriented Programming
Structured Programming:
- Structured programming is a programming paradigm.
- It emphasizes the use of structured control flow and modular design to write clear, understandable, and maintainable code.
- It was developed in the late 1960s.
- Single Entry, Single Exit (SESE): A block of code within a structured program must have a single point of entry and a single point of exit. It helps maintain code readability and understandability.
- Modularity: It breaks down a program into smaller, self-contained modules or functions. Each module should have a well-defined purpose and interface, making it easier to understand, test, and maintain.
- Also termed as Modular Programming or Procedure-Oriented Programming
- Recall – A Large task is divided into number of smaller tasks. The Task becomes Manageable.
- Ex. An Engineering College has different Departments. Example Civil, Electrical, Computer, Mechanical Departments etc.
- A Large Program is divided into a number of small programs called Sub-programs or Modules or a Subroutines.
- A Sub-program is implemented in a structured language (C | COBOL | Pascal) as a Function or a Procedure.
The conclusion is to Divide and Rule. It simply means a large tasks should be divided into smaller numbers of tasks so as that it becomes manageable to finish the task. The same approach is applied to the development of software. Let's understand with the following problem:
Problem: Write a program to accept and display a number using structured approach.
Here the question in developer or programmer's mind is –
What needs to be done?
To get answer, read the question again and again.
The identified sub-tasks are -
- Accept a Number
- Display a Number
//Defines a Procedure
//void keyword of C indicates that no value is returned
void proc() {
//executable Statements
}
//Defines a function
//Returns a integer type value
int function() {
//executable Statements
return 0;
}
The identified sub-tasks will be implemented as follows:
void accept() {
std::cin>>n;
}
void display() {
std::cout<<"n = "<<n;
The variable n is being used in both the sub programs. A variable used in a program is required to be declared. The question is where to declare it.
Recall:
- A Global variable is one whose scope is from the point of declaration and its lifetime is throughout the program. It is declared outside the body of the function.
- A Local variable is one whose scope is only in the function in which it is declared and its lifetime only till the function or procedure in which the variable is declared is in execution.
Here, the variable n needs to be declared as a global variable as shown below:
//Global variable
int n;
//Procedure definitions
void accept() {
std::cin>>n;
}
void display() {
std::cout<<"n = "<
}
Finally to test the developed code, one needs to define the main() function or procedure to test the developed accept() and the display() procedures.
//Execution starts from here
int main() {
// calling accept() and display()
accept();
display();
return 0;
}
- As seen in code – Structured programming makes extensive use of global variables.
- Global variables, as the name implies, are variables that are accessible globally, or everywhere throughout the program.
- Once declared, they remain in memory throughout the runtime of the program.
- This means that they can be changed by any function at any point and may affect the program as a whole.
- For smaller applications, global variables are not a problem.
Solution to these issues and drawbacks- Object Oriented Paradigm
Structured Programming & Object-Oriented Programming
Reviewed by Syed Hafiz Choudhary
on
September 10, 2023
Rating:
No comments: