Subroutines and Scope
Aligned to the OCR J277 specification
- Level
- Intermediate
- Reading time
- 7 min
- Published
- 11 June 2026
- Updated
- 1 July 2026
On this page
Key takeaways
- A function returns a value and its result is used in an expression or assignment; a procedure performs an action and returns nothing.
- Parameters are the variable names in a subroutine's definition; arguments are the actual values passed in when the subroutine is called.
- Local variables exist only inside the subroutine where they are declared and are destroyed when that subroutine ends; they cannot be accessed from outside it.
- Global variables are declared outside all subroutines and can be accessed from anywhere; they should be used sparingly as they make programs harder to debug.
- random.randint(a, b) returns a random integer where both a and b are inclusive, so randint(1, 6) can return 1, 2, 3, 4, 5, or 6.
How much of this have you taken in?
Quiz yourself on this section, free, no card needed.
Key terms
- Subroutine
- A named, reusable block of code that performs a specific task; called by name whenever needed, avoiding code duplication.
- Function
- A subroutine that returns a value using a return statement; the result is used in an expression or assigned to a variable.
- Procedure
- A subroutine that performs an action but does not return a value; called as a standalone statement.
- Parameter
- A variable name listed in a subroutine's definition that receives a value when the subroutine is called.
- Argument
- The actual value passed to a subroutine at the point of calling, corresponding to a parameter in the definition.
- Local variable
- A variable declared inside a subroutine; only accessible within that subroutine and destroyed when it ends.
- Global variable
- A variable declared outside all subroutines; accessible from anywhere in the program.
- Scope
- The region of a program in which a variable can be accessed; local variables have subroutine scope, global variables have program-wide scope.
- Return statement
- The statement inside a function that sends a computed value back to the code that called the function.
- random.randint(a, b)
- A Python function that returns a random integer between a and b inclusive; both bounds can be returned.
Frequently asked questions
A function returns a value that can be used in an expression or stored in a variable. A procedure performs an action (such as printing output) and returns nothing. Printing to the screen does not make a subroutine a function - returning a value does.
Parameters are the variable names listed in a subroutine's definition, for example def add(a, b). Arguments are the actual values passed when the subroutine is called, for example add(3, 7). In this case a and b are parameters; 3 and 7 are arguments.
Global variables can be accessed and changed from anywhere in the program, which makes it harder to trace bugs and understand what a subroutine does. Local variables are preferred because they cannot accidentally interfere with the rest of the program.
Generate revision on any topic you study
Type any topic you're studying and Aicademy generates a complete lesson, quiz, and flashcard set, personalised to your level.
Lessons on anything
Structured, level-matched lessons on any topic you study
Practice quizzes
Find out what you actually know before the exam does
Flashcard sets
Lock in key concepts with instant revision cards
Ask Aica
Stuck on something? Get a clear explanation, any time
SQL Basics
Defensive Design
Related lessons
6 min
6 min
7 min
8 min