Structured Programming  «Prev  Next»
Lesson 9

Structured Programming Conclusion

This module discussed the fundamental concepts of structured programming. You now have the skills and knowledge necessary to:
  1. Describe structured programming:
    Structured programming is a programming paradigm that emphasizes clear, logical organization of code to enhance readability, maintainability, and reliability. It advocates for the use of well-defined control structures—sequence, selection, and iteration—while avoiding chaotic constructs like unrestricted jumps (e.g., GOTO statements). By breaking programs into modular, hierarchical components, structured programming ensures that code is easier to understand, debug, and modify. This approach, pioneered in the 1960s by figures like Edsger Dijkstra, forms the foundation of modern software development, promoting disciplined coding practices that reduce complexity and improve software quality.
  2. Identify disadvantages of unstructured programs:
    Unstructured programs, often characterized by heavy reliance on GOTO statements and haphazard control flow, suffer from several significant disadvantages. They are difficult to read and understand due to their lack of clear organization, making debugging and maintenance a nightmare. Modifications to such programs frequently introduce errors, as the tangled "spaghetti code" obscures the program’s logic. Additionally, unstructured programs are harder to test systematically, increasing the likelihood of undetected bugs. Their lack of modularity also hinders collaboration among developers and scalability, rendering them inefficient for large or complex projects.
  3. Describe and use pseudocode: Pseudocode is a high-level, informal description of a program’s logic, written in plain language to outline the steps of an algorithm without adhering to the syntax of a specific programming language. It serves as a planning tool, allowing developers to design and refine algorithms before coding them. For example, to calculate the average of a list of numbers, pseudocode might look like: Initialize sum to 0; For each number in list, add number to sum; Divide sum by count of numbers; Output result. This clarity helps bridge the gap between human understanding and machine implementation, making it easier to translate ideas into actual code.
  4. Describe the three control flow constructs of structured programming:
    The three fundamental control flow constructs of structured programming are sequence, selection, and iteration. Sequence refers to the linear execution of statements in the order they appear, ensuring a straightforward flow of operations. Selection, implemented through constructs like if-then-else statements, allows the program to execute different blocks of code based on conditions, enabling decision-making. Iteration, achieved through loops such as for or while, permits repeated execution of a code block until a condition is met, facilitating tasks like processing lists or repeating actions. Together, these constructs provide a disciplined framework for controlling program execution.
  5. Identify advantages of using subprograms:
    Subprograms, such as functions or procedures, offer numerous advantages in structured programming. They promote modularity by breaking complex programs into smaller, reusable units, making code easier to understand and maintain. Subprograms reduce redundancy, as common tasks can be written once and called multiple times, minimizing code duplication. They enhance testability, allowing individual components to be tested in isolation. Additionally, subprograms improve collaboration, as different developers can work on separate modules simultaneously. By encapsulating specific functionality, subprograms also make it easier to update or replace parts of a program without affecting the whole system.

How Fortran 90 uses subprograms to help achieve Structured Programming

In a procedural language like Fortran 90, subprograms (which include subroutines and functions) are **fundamental building blocks** that directly enable and enforce the principles of structured programming. Here's how they contribute:
  1. Modularity and Decomposition:
    • Breaking Down Complexity: Subprograms allow you to divide a large, complex problem into smaller, more manageable, and logically independent units. Each subprogram performs a specific, well-defined task.
    • Increased Readability: By encapsulating specific functionalities within subprograms, the main program becomes shorter, cleaner, and easier to understand. You can grasp the overall flow of the program without being bogged down in the details of every operation.
    • Improved Maintainability: Changes or bug fixes related to a specific task can be isolated within the corresponding subprogram, reducing the risk of unintended side effects on other parts of the program. This makes maintenance and debugging significantly easier.
  2. Abstraction:
    • Hiding Implementation Details: Subprograms allow you to abstract away the internal workings of a particular operation. The main program only needs to know what the subprogram does (its interface: name, arguments), not how it does it.
    • Focus on Functionality: This abstraction allows programmers to focus on the higher-level logic of the program without being concerned with the low-level implementation details of each subtask.
  3. Reusability:
    • Avoiding Code Duplication: Once a subprogram is written to perform a specific task, it can be called multiple times from different parts of the program or even from other programs. This eliminates the need to write the same code repeatedly, saving time and effort, and reducing the potential for errors.
    • Building Libraries: Collections of related subprograms can form libraries or modules, providing reusable components for various programming tasks.
  4. Enhanced Testability:
    • Independent Testing: Each subprogram can be tested independently with specific inputs to verify its correctness before being integrated into the larger program. This modular testing approach makes it easier to identify and fix bugs early in the development process.
  5. Top-Down Design:
    • Implementing the Design: Subprograms naturally support a top-down design approach. You can start by outlining the main steps of the program and then progressively refine each step by creating subprograms to handle the specific subtasks. This hierarchical structure makes the design process more organized and logical.

In essence, subprograms in Fortran 90 (and other procedural languages) are the primary mechanism for implementing the core principles of structured programming:**
  • Modular Design: Breaking the program into independent modules (subprograms).
  • Top-Down Approach: Designing the program from a high-level overview down to specific implementations in subprograms.
  • Single Entry, Single Exit: Well-designed subprograms typically have a single point of entry and a single point of exit, contributing to clearer control flow.

By effectively utilizing subprograms, Fortran 90 programmers can write programs that are more organized, readable, maintainable, reusable, and less prone to errors, adhering to the principles of structured programming.

Question: What do subprograms do and how do subprograms help the main program?
Answer: They do anything that the main program can do. They help you organize your total program (main program and subprograms) by grouping specific tasks in a well defined location. They can save you repeating similar program structures at several places in your code. Also, for many tasks someone has already written a subprogram to do the job and you save a lot of work by picking up their subprogram and plugging it into your work. You will see an example of this when we start solving systems of linear equations (say 10 equations and 10 unknowns). You will define the equations, then let somebody else's subprogram solve them. In the next module you will begin putting theory into practice by writing and running your first program.

SEMrush Software Target 9SEMrush Software Banner 9