StudyVault
Home Unit Overview Exam Technique Revision Techniques ← Previous Lesson Next Lesson →

← Back to Dashboard

Overlay:
0:00 / 0:00 Narration generated using AI text-to-speech.

Advanced Programming Techniques

Most real programs are far too large to write as one long list of instructions. If every line sat in a single block, the code would be hard to read, tricky to test and almost impossible to fix. The professional solution is to break a big problem into smaller, self-contained blocks of code that each do one job. Writing a program in this way is known as producing structured code.

A sub program is a named block of code that you can run whenever you need it, simply by writing its name. Because a sub program is written once but can be called many times, it saves you repeating the same instructions and keeps each part of the program short and focused. There are two kinds of sub program: procedures and functions.

Procedures and functions

A procedure is a sub program that carries out a task — such as printing a message or updating a score — but does not send any value back. You define it between the keywords procedure and endprocedure, then run it by writing its name followed by brackets.

procedure agePass()
    print("You are old enough to ride")
endprocedure

agePass()

A function also carries out a task, but it sends a single value back to the part of the program that called it. The value is passed back using the keyword return. Because a function produces a result, you normally store what it returns in a variable so you can use it later.

function squared(number)
    squared = number^2
    return squared
endfunction

newValue = squared(4)
Key Fact

A procedure performs a task but returns no value. A function performs a task and returns one value, which should be stored in a variable if you want to use it later.

Use a function whenever the sub program needs to work out or look up a value that the rest of the program will use — for example calculating a total, converting units or checking a password. The answer comes back through return.

Use a procedure when the sub program simply needs to do something and does not need to hand a value back — for example printing a menu, saving a file or clearing the screen. Choosing the right one makes your intentions clear to anyone reading the code.

Parameters and arguments

Sub programs become far more useful when you can feed data into them. A parameter is a variable listed in the sub program definition that receives an incoming value. An argument is the actual value you supply when you call the sub program. In the code below, width and height are parameters; when the sub program is called, the numbers 5 and 3 are the arguments.

procedure printArea(width, height)
    print(width * height)
endprocedure

printArea(5, 3)

Passing data through parameters means the same sub program can work on different values each time it is called, instead of being tied to one fixed set of numbers. This is exactly what makes sub programs reusable.

Variable scope: local and global

The scope of a variable describes which parts of a program are allowed to use it. A local variable is created inside a sub program and only exists while that sub program is running, so the rest of the program cannot see it. A global variable is declared in the main program and can be read or changed anywhere, including inside sub programs.

Key Fact

A local variable only exists inside the sub program where it is created and disappears when that sub program ends. A global variable can be accessed from anywhere in the program.

Because a local variable is sealed inside its own sub program, it cannot be changed by accident from somewhere else. This keeps sub programs independent, so you can change one without breaking another.

Global variables can be handy for data the whole program needs, but if many sub programs can all change the same global value, tracking down a bug becomes much harder. As a rule, keep variables local unless there is a clear reason to make them global.

Reading and writing files

A running program normally loses everything held in memory when it closes. To keep data permanently, a program can save it to a text file and read it back later — this is called file handling. There are four basic operations: open a file, read from it, write to it, and close it. When you open a file it must be stored in a variable so the program can work with it.

myFile = open("sample.txt")
print(myFile.readLine())
myFile.close()

The command readLine() returns the next line of the file each time it is used. To read a whole file you loop until you reach the end, which the endOfFile() command checks for you:

myFile = open("sample.txt")
while NOT myFile.endOfFile()
    print(myFile.readLine())
endwhile
myFile.close()

To store new data, writeLine() adds a line of text to the end of the file, and newFile() creates a brand new file ready to be opened. Always close a file with close() when you have finished, so that your changes are saved and the file is freed for other programs to use.

Practice Questions

Test yourself on this lesson. Pick a question, write your answer, then get it marked.

AI feedback
AI feedback is guidance, not a final exam mark. If you disagree, your teacher's view is always the tie-breaker.
← Back to unit
Video

© 2026 StudyVault · About · FAQ · Copyright & IP · Privacy · Disclaimers

StudyVault Education Ltd · registered in England and Wales, company no. 17490559 · 71–75 Shelton Street, London WC2H 9JQ