Welcome to Program Structure!

Ever tried assembling a massive piece of flat-pack furniture without the instruction manual? It would be overwhelming, messy, and prone to mistakes! Writing computer programs is exactly the same. When developers build complex software, they don't just write thousands of lines of code in one giant block. Instead, they organize, break down, and structure their programs neatly.

In this chapter of AS 1: Approaches to Systems Development, you will learn how programmers structure software using top-down design, modular programming, subroutines, and variable scoping. Mastering these concepts will help you write cleaner code and score top marks in your CCEA exams!


1. Top-Down Design and Stepwise Refinement

When faced with a large, complicated problem, the best approach is to break it down into smaller, bite-sized tasks. This technique is known as top-down design (or stepwise refinement).

How It Works

Step 1 (The Big Picture): Start with the main goal or overall problem at the very top level.
Step 2 (Decomposition): Break this main task down into major sub-tasks.
Step 3 (Refinement): Break each sub-task down further into smaller steps until each piece is simple enough to be written as an individual subroutine or function.

Real-World Analogy: Think of planning a school formal. You don't try to do everything at once! You split the project into main categories (Venue, Catering, Music, Tickets). Then, you break "Catering" down further (Choose menu, collect dietary requirements, confirm guest count).

Structure Charts

A structure chart is a graphical diagram used to represent a top-down design. It shows:
• The hierarchical relationship between different modules.
• Which modules call other sub-modules.
• How data and control signals flow between modules (often shown using small arrows with open or solid circles).

Quick Takeaway: Top-down design turns an intimidating project into small, manageable pieces through stepwise refinement.


2. Modular Programming & Subroutines

Modular programming is a software design technique where a program is divided into separate, independent sub-programs called modules or subroutines.

Why Use Modular Programming?

Teamwork: Different developers can work on different modules at the same time without interfering with each other.
Reusability: Once a module is written and tested (e.g., a tax calculation module), it can be reused multiple times across different programs.
Easier Testing and Debugging: It is far easier to isolate and fix a bug in a 20-line module than in a 2,000-line continuous program.
Readability and Maintenance: Programs are shorter, cleaner, and much easier for other programmers to understand and update later.

Procedures vs. Functions

Subroutines come in two main flavors: procedures and functions. Don't worry if this sounds confusing—the difference is very straightforward!

Procedure: A block of code that performs a specific set of actions or operations, but does not return a single value directly to the calling statement. Example: A procedure that displays a welcome message on screen or clears a form.
Function: A block of code that performs a calculation or task and returns a single calculated value back to the part of the program that called it. Example: A function that takes two numbers and returns their sum: \(Area = Length \times Width\).

Memory Trick: Functions return a Fixed value. Procedures just Perform an action.

Quick Takeaway: Subroutines make code reusable and manageable. Procedures do work; functions do work and give you back a single value.


3. Parameters and Parameter Passing

When a main program calls a subroutine, it often needs to pass information into it. The items of data passed into a subroutine are called parameters (or arguments).

There are two fundamental ways to pass parameters into a subroutine:

A. Passing by Value (ByVal)

• A duplicate copy of the actual data is sent to the subroutine.
• The subroutine works on this separate copy in memory.
Key Rule: Any changes made to the parameter inside the subroutine do not alter the original variable in the main program.
Analogy: You photocopy your homework and hand the copy to a friend. If they scribble on it, your original homework remains completely untouched!

B. Passing by Reference (ByRef)

• The subroutine receives the memory address (pointer) of the original variable, rather than a copy.
• The subroutine accesses and operates directly on the original data stored at that address.
Key Rule: Any changes made inside the subroutine will permanently change the original variable in the calling program.
Analogy: You share a live Google Doc with a friend. If they delete a paragraph, it is deleted from your view too because you are both looking at the exact same file!

Common Exam Mistake to Avoid

Exam Trap: Students often say "ByVal doesn't change anything." Be precise! Say: "Passing by value ensures the original variable in the calling program remains unchanged, because only a copy of the data is manipulated."

Quick Takeaway: ByVal passes a safe copy; ByRef passes direct access to the original memory address.


4. Scope of Variables: Local vs. Global

The scope of a variable defines where in the program that variable can be seen, accessed, and modified.

Local Variables

• Declared inside a specific subroutine or block of code.
• Accessible only within that subroutine.
• Created when the subroutine starts executing, and destroyed (releasing memory) as soon as the subroutine ends.
Advantage: Different subroutines can use the same variable name (like \(counter\)) without accidentally overwriting each other's data.

Global Variables

• Declared at the very start of the main program, outside of all subroutines.
• Accessible and modifiable from anywhere within the program.
• Remain in memory for the entire duration of the program's runtime.
Disadvantage: Overusing global variables makes debugging very difficult because any subroutine can change their values unexpectedly (known as "side effects"), and they consume memory unnecessarily.

Did You Know? In professional software engineering, relying too heavily on global variables is considered "bad code smell" because it leads to unpredictable errors that are tough to track down!

Quick Takeaway: Keep variables local whenever possible to protect data integrity and save memory.


5. Fundamental Control Structures

Every structured program, no matter how complex, is built using just three basic control structures:

1. Sequence: Statements are executed one after another in the exact order they appear, from top to bottom.
2. Selection: The program tests a condition and chooses which path of execution to follow. Examples include IF...THEN...ELSE and SELECT CASE (or switch statements).
3. Iteration (Looping): The program repeats a block of code multiple times. This can be:
    - Count-controlled: Repeats a set number of times (e.g., FOR...NEXT).
    - Condition-controlled: Repeats while or until a condition is met (e.g., WHILE...WEND or DO...LOOP UNTIL).

Quick Takeaway: Structured programming uses Sequence, Selection, and Iteration to create logical, readable, and predictable code.


Chapter Summary & Quick Review

Top-Down Design: Breaking a large problem into smaller sub-tasks (stepwise refinement).
Structure Charts: Visual diagrams showing module hierarchy and data flow.
Procedures vs. Functions: Functions return a single calculated value; procedures do not.
ByVal vs. ByRef: ByVal sends a copy (protects original); ByRef sends the memory location (changes the original).
Local vs. Global: Local variables are restricted to their own subroutine; global variables can be accessed anywhere.
Structured Constructs: Sequence, Selection, and Iteration.