Subroutines that Return Values (AQA GCSE Computer Science): Revision Notes
Subroutines that return values
What are subroutines that return values?
When you write programmes, you'll often need to create subroutines that don't just perform an action, but also give you back a result. These special subroutines are called functions because they calculate and return a value to wherever they were called from in your main programme.
This is different from procedures, which are subroutines that simply carry out tasks without returning anything back to the main programme. Think of it this way:
Key Distinction:
- Functions = Do something AND give you back a result
- Procedures = Just do something
How to create functions using pseudocode
When writing functions in AQA pseudocode, you use the RETURN keyword to send a value back to the main programme. Here's how it works:
Pseudocode Example: Circle Area Function
SUBROUTINE circle_area(radius)
const pi ← 3.14159
area ← pi * radius^2
RETURN area
ENDSUBROUTINE
This function calculates the area of a circle and returns that calculated value.
When you call this function from your main programme, you can store the returned value in a variable:
new ← circle_area(10)
OUTPUT new
The function circle_area(10) runs with radius = 10, calculates the area, and returns that value which gets stored in the variable new.
Syntax in different programming languages
Python
def circle_area(radius):
PI = 3.14159
area = PI * radius**2
return area
VB.Net
Function circle_area(ByVal radius As Single) As Single
Dim area As Single
Const pi As Single = 3.14159
area = pi * radius^2
Return area
End Function
C#
static single circle_area(single radius)
{
const single pi = 3.14159;
single area = pi * radius * radius
return area;
}
While the syntax looks different in each language, the basic principle remains the same - you calculate a value and use a return statement to send it back.
Understanding local variables
Here's something really important to understand: variables that you create inside a function are called local variables. These variables have local scope, which means they only exist inside that specific function and cannot be accessed from your main programme.
Critical Concept: Variable Scope
Looking at our circle_area function, the variables area and pi are local variables. They exist only while the function is running and disappear when the function finishes.
What happens if you try to access local variables from outside?
Let's see what happens when you try to use a local variable from the main programme:
Example: Local Variable Access Error
def circle_area(radius):
PI = 3.14159
area = PI * (radius**2)
return area
print(circle_area(10))
print(area) # This will cause an error!
The second print statement will give you an error: NameError: name 'area' is not defined. This happens because area is a local variable that only exists inside the function.
This is actually a good thing! It prevents different parts of your programme from accidentally interfering with each other.
Why use functions that return values?
Using functions that return values is part of good structured programming practice. Here are the main benefits:
Modularisation: You can break your large programme into smaller, manageable chunks. Each function handles one specific task, making your code easier to understand and debug.
Reusability: Once you've written a function like circle_area, you can use it multiple times throughout your programme without having to write the calculation code again.
Easier maintenance: If you need to change how area calculation works, you only need to update it in one place - inside the function.
Clear interfaces: Functions have clear inputs (parameters) and outputs (return values), making it obvious what data flows in and out.
Exam tips
Essential Exam Knowledge:
- Remember the difference: Functions return values, procedures don't
- Use the RETURN keyword: In pseudocode, always use RETURN to send a value back
- Watch out for local variables: Don't try to access variables from inside a function in your main programme
- Practice the syntax: Make sure you know how to write functions in at least one high-level language like Python
Remember!
Key Points to Remember:
- Functions are subroutines that return values back to the main programme, while procedures just perform tasks
- Use the RETURN keyword in pseudocode to send a value back from a function
- Local variables only exist inside their function and cannot be accessed from the main programme
- Structured programming with functions makes your code more organised, reusable, and easier to maintain
- Different programming languages have different syntax but the concept of returning values works the same way