Welcome to Structured Programming!

Ever tried to bake a massive, multi-layered cake all at once without a plan? It would be a mess! Programming is the same. When programs get big, we need to break them down into smaller, manageable chunks. In this chapter, we will learn how to use subroutines to make our code organized, reusable, and much easier to fix. Don't worry if it sounds a bit technical at first—by the end of these notes, you'll be building programs like a pro architect!

1. What are Subroutines?

A subroutine is a named block of code that performs a specific task. Think of it as a "mini-program" inside your main program. Instead of writing the same code over and over again, you write it once inside a subroutine and "call" it whenever you need it.

There are two main types of subroutines you need to know:

1. Procedures: These perform a task but don't necessarily send a value back to the main program.
2. Functions: These perform a task and return a value back to the part of the program that called them.

How do we use them?

In your exam, you should know that a subroutine is described as being 'out of line'. This just means the code sits outside the main flow of the program. You call (execute) a subroutine simply by writing its name in a program statement.

Real-world Analogy: Imagine a "MakeTea" button on a robot. You don't tell the robot every single step (boil water, get mug, add bag) every time you want a drink. You just press the "MakeTea" button. The button is the call, and the steps inside the robot's memory are the subroutine.
Quick Review: Advantages of Subroutines

Easier to read: The main program becomes shorter and cleaner.
Reusable: You can use the same subroutine many times in different parts of the program.
Easier to test: You can test one small subroutine at a time to make sure it works perfectly.
Collaboration: Different programmers can work on different subroutines at the same time.

Key Takeaway: Subroutines are the building blocks of organized code. They save time and prevent "spaghetti code" (messy, confusing instructions).

2. Parameters: Passing Data In

Sometimes a subroutine needs information to do its job. For example, if you have a subroutine called CalculateArea, it needs to know the length and width of the shape. We pass this information using parameters.

A parameter is a special variable used to pass data into a subroutine. You can have one parameter, or many!

Example: In the subroutine call CalculateArea(10, 5), the numbers 10 and 5 are the data being passed into the parameters.

Did you know? In the exam, you might hear the term "argument." Don't let it confuse you! Teachers and programmers sometimes use "argument" for the actual value passed and "parameter" for the variable name inside the subroutine, but AQA uses the term parameter to cover both.

Key Takeaway: Parameters act like "inputs" for your subroutine, allowing it to work with different data every time it is called.

3. Return Values: Passing Data Out

While parameters get data into a subroutine, return values get data out. This is what turns a procedure into a function.

When a subroutine finishes its task, it can "hand back" a result to the main program using the RETURN keyword. This result can then be stored in a variable or used in a calculation.

Coffee Machine Analogy: You put water and beans in (parameters). The machine runs its "Brew" subroutine. Finally, it gives you a cup of coffee (return value).
Step-by-Step: How a Function Works

1. The main program calls the function and provides parameters.
2. The function runs its code using those parameters.
3. The function reaches a RETURN statement.
4. The value is sent back, and the function ends.

Key Takeaway: Use a function when you need your subroutine to calculate something and give you an answer back.

4. Local Variables and Scope

When you create a variable inside a subroutine, it is called a local variable. This is a very important concept for the AQA syllabus.

What makes a variable "local"?

• They only exist while the subroutine is actually running.
• They are only accessible within that specific subroutine.

Why is using local variables "Good Practice"?

It might seem annoying that you can't see a local variable from the main program, but it's actually a safety feature!
1. Prevents interference: You don't have to worry about accidentally changing a variable in the main program with the same name.
2. Saves Memory: Since the variable is deleted as soon as the subroutine finishes, it frees up space in the computer's RAM.

Common Mistake: Trying to use a local variable outside of its subroutine. This will cause an error because the rest of the program doesn't know that variable exists!

Key Takeaway: Keep your variables local whenever possible. It keeps your code "encapsulated" (self-contained and safe).

5. The Structured Approach

The structured approach to programming is a method where you look at a big problem and use decomposition to break it into smaller, logical modules (subroutines).

Features of Structured Programming:

Modularization: Breaking the program into subroutines.
Local Variables: Using variables that only exist where they are needed.
Parameters and Return Values: Using clear "interfaces" to pass data in and out of modules.
Documented Interfaces: Clearly stating what each subroutine needs (parameters) and what it provides (return values).

Advantages of the Structured Approach

Easier to Debug: If there is an error, you can pinpoint exactly which subroutine is failing.
Maintenance: It is much easier to update or change one part of the program without breaking everything else.
Teamwork: In the real world, 100 people might work on one program. Structured programming allows them to work on separate modules that fit together perfectly later.

Memory Trick: Think of LEGO. Each brick is a subroutine. They are self-contained, they have a specific shape (interface) to connect to others, and you can combine them to build something massive and complex!

Key Takeaway: Structured programming is about being a tidy and logical coder. It makes your programs robust and professional.

Quick Review Quiz (Mental Check!)

• Can you explain the difference between a parameter and a return value?
• Why is it better to use a local variable instead of a global one?
• What is the main benefit of "breaking down" a program into subroutines?

(Answers: Parameters go in, returns go out; Local variables are safer and save memory; Subroutines make code reusable and easier to test!)