Getting Started: Building Programs Like Lego

Imagine you are building a massive Lego castle. Would you try to build the whole thing out of one giant, solid piece of plastic? Of course not! You use smaller bricks, windows, and doors to create the final masterpiece.

In computer science, structured programming works the same way. Instead of writing one long, confusing list of instructions, we break our programs down into smaller, manageable "bricks" called subroutines. This makes our code easier to read, easier to fix, and much more organized.

Don't worry if this seems a bit abstract at first – by the end of these notes, you'll see how these "code bricks" make a programmer's life much easier!


1. What is a Subroutine?

A subroutine is a named "out of line" block of code. This simply means it is a group of instructions that sits to the side of your main program. It only runs when you specifically ask for it.

To use a subroutine, you "call" it by writing its name in a program statement.

Real-World Analogy: The Recipe

Imagine a recipe for a Sunday Roast. One of the steps might say: "Make the gravy."

The "Make the gravy" step is like a subroutine call. The actual instructions for making gravy aren't written in the middle of the roast beef instructions—they are usually on a different page. You go to that page, follow the steps, and then come back to where you left off.

Key Takeaway:

Subroutines are named blocks of code that perform a specific task and can be called whenever needed.


2. The Advantages of Using Subroutines

Why bother breaking a program up? Why not just write it all in one go?

  • Reusability: You only have to write the code once. If you need to calculate a discount ten times in a program, you just call the calculateDiscount subroutine ten times instead of writing the math ten times.
  • Easier to Test: You can test one subroutine at a time to make sure it works perfectly before adding it to the rest of the program.
  • Teamwork: In the real world, big programs (like Minecraft or Instagram) are built by teams. One person can write the "Login" subroutine while another writes the "Photo Filter" subroutine.
  • Readability: It is much easier to read a program that says checkPassword() than one that has 50 lines of complex security code in the middle of it.

3. Parameters: Passing Data IN

Sometimes a subroutine needs information to do its job. We pass this information using parameters.

Did you know? In exams, you might hear people use the word "arguments." For AQA GCSE, we use the term parameter to refer to both the data being sent and the placeholder inside the subroutine.

Example: The Vending Machine

Think of a vending machine as a subroutine. To get it to work, you need to give it two things: Money and a Selection Code. These are the parameters. Without them, the machine doesn't know what to do!

Step-by-Step: How it works
1. The main program "calls" the subroutine.
2. The main program provides parameters (the data).
3. The subroutine uses those parameters to do a calculation or task.


4. Return Values: Passing Data OUT

Once a subroutine has finished its task, it might have an answer for you. It sends this answer back to the main program using a return value.

Procedures vs. Functions:
• A function is a type of subroutine that always returns a value (like a calculator giving you the result of \( 5 + 5 \)).
• A procedure is a subroutine that performs a task but doesn't necessarily have to send a value back.

Quick Review:

Parameters = Data going into the subroutine.
Return Values = Data coming out of the subroutine.


5. Local Variables: "What Happens in Vegas..."

Subroutines often need their own variables to store temporary data while they are working. These are called local variables.

Important Rules for Local Variables:
1. They only exist while the subroutine is actually running.
2. They are only accessible within that specific subroutine.

Why is this good practice?

  • Memory Efficiency: Once the subroutine is finished, the computer "deletes" the local variables, freeing up RAM.
  • Safety: Using local variables prevents you from accidentally changing a variable in another part of the program. It keeps the "mess" contained!

Memory Aid: Think of a local variable like a piece of scrap paper you use to do a quick sum. Once you have the answer, you throw the scrap paper away.


6. The Structured Approach

The structured approach is a professional way of writing code. It involves modularized programming (breaking things into subroutines) and using clear, well-documented interfaces.

A "well-documented interface" just means it is very clear what parameters the subroutine needs and what return values it will provide.

Advantages of the Structured Approach:

  • Programs are easier to maintain because you can update one module without breaking the others.
  • The code is easier to debug (find errors).
  • The logic of the program is clearer to other programmers.

Common Mistakes to Avoid

The "Invisible" Variable: Students often try to use a local variable from a subroutine in their main program. This will cause an error! Remember: if a variable is created inside a subroutine, the main program can't "see" it.

Confusing Parameters and Returns: Remember that parameters are the ingredients you put into the blender, and the return value is the smoothie that comes out!


Final Quick Check

Can you explain...
1. What a subroutine is? (A named, out-of-line block of code).
2. Two benefits of subroutines? (Reusability and easier debugging).
3. The difference between a parameter and a return value? (Parameters go in; returns come out).
4. Why local variables are used? (To save memory and prevent accidental changes to other code).

You're doing great! Structured programming is the foundation of becoming a professional coder. Keep practicing by breaking your own small programs into subroutines!