Return values
Procedures and Functions
In the previous benchmarks, all subroutines were procedures - blocks of code that carry out a task and then finish. A function does the same thing, but it also returns a value back to the part of the program that called it. That returned value can then be stored in a variable, used in a calculation, or passed directly to an output statement - wherever a value of that type is needed.
| Procedure | Function | |
|---|---|---|
| Carries out a task? | Yes | Yes |
| Returns a value? | No | Yes |
| Typical use in AQA pseudocode | SUBROUTINE...ENDSUBROUTINE |
FUNCTION...ENDFUNCTION with RETURN |
| How called | As a statement: showBanner() |
In an expression: result ← add(3, 4) |
The RETURN Statement
Inside a function, the RETURN statement specifies what value is sent back to the calling code. As soon as RETURN is reached, the function stops executing and the value is passed back. In the calling code, the function call is replaced by the returned value - so area ← calculateArea(5, 3) stores 15 in area after the function runs.
Functions with Zero, One, and Two Parameters
A function can have any number of parameters. The three examples below progress from no parameters through to two, using a consistent theme. Select a tab, then choose a language.
Even with no parameters, a function is more powerful than a procedure because its result can be used directly in an expression. Here, getMaxScore() returns the integer 100. The calling code stores it and uses it without hardcoding the number 100 in multiple places.
# Python -- function with no parameters; returns a fixed integer
def getMaxScore():
return 100 # sends the value 100 back to the caller
maxScore = getMaxScore() # maxScore is now 100
print("The maximum score is " + str(maxScore))
' VB.NET -- function with no parameters; returns a fixed integer
Function getMaxScore() As Integer
Return 100 ' sends the value 100 back to the caller
End Function
Dim maxScore As Integer = getMaxScore() ' maxScore is now 100
Console.WriteLine("The maximum score is " & maxScore)
// C# -- function with no parameters; returns a fixed integer
static int GetMaxScore()
{
return 100; // sends the value 100 back to the caller
}
int maxScore = GetMaxScore(); // maxScore is now 100
Console.WriteLine("The maximum score is " + maxScore);
addTax(price) receives one argument - a price - performs a calculation, and returns the result. The calling code can store the returned value, print it, or pass it straight into another expression. The function does not print anything itself; it only returns a value.
# Python -- function with one parameter; returns a calculated real number
def addTax(price):
return price * 1.2 # returns the price with 20% tax added
total = addTax(50) # total receives 60.0
print("Price with tax: " + str(total))
print("Price with tax: " + str(addTax(80))) # return value used directly
' VB.NET -- function with one parameter; returns a calculated real number
Function addTax(ByVal price As Double) As Double
Return price * 1.2 ' returns the price with 20% tax added
End Function
Dim total As Double = addTax(50) ' total receives 60.0
Console.WriteLine("Price with tax: " & total)
Console.WriteLine("Price with tax: " & addTax(80)) ' return value used directly
// C# -- function with one parameter; returns a calculated real number
static double AddTax(double price)
{
return price * 1.2; // returns the price with 20% tax added
}
double total = AddTax(50); // total receives 60.0
Console.WriteLine("Price with tax: " + total);
Console.WriteLine("Price with tax: " + AddTax(80)); // return value used directly
calculateArea(length, width) accepts two integer parameters and returns their product. Notice that the return value can be stored in a variable, used directly in an output, or even passed as an argument to another function call - the returned value behaves exactly like any other value of that type.
# Python -- function with two parameters; returns an integer
def calculateArea(length, width):
return length * width # returns the product of the two arguments
area1 = calculateArea(5, 3) # area1 receives 15
area2 = calculateArea(10, 4) # area2 receives 40
print("Room 1: " + str(area1) + " m2")
print("Room 2: " + str(area2) + " m2")
print("Total: " + str(area1 + area2) + " m2")
' VB.NET -- function with two parameters; returns an integer
Function calculateArea(ByVal length As Integer, ByVal width As Integer) As Integer
Return length * width ' returns the product of the two arguments
End Function
Dim area1 As Integer = calculateArea(5, 3) ' area1 receives 15
Dim area2 As Integer = calculateArea(10, 4) ' area2 receives 40
Console.WriteLine("Room 1: " & area1 & " m2")
Console.WriteLine("Room 2: " & area2 & " m2")
Console.WriteLine("Total: " & (area1 + area2) & " m2")
// C# -- function with two parameters; returns an integer
static int CalculateArea(int length, int width)
{
return length * width; // returns the product of the two arguments
}
int area1 = CalculateArea(5, 3); // area1 receives 15
int area2 = CalculateArea(10, 4); // area2 receives 40
Console.WriteLine("Room 1: " + area1 + " m2");
Console.WriteLine("Room 2: " + area2 + " m2");
Console.WriteLine("Total: " + (area1 + area2) + " m2");
Key Takeaways
- A function is a subroutine that returns a value to the calling code using a RETURN statement; a procedure carries out a task but does not return a value.
- The returned value replaces the function call in the expression - e.g.
area ← calculateArea(5, 3)stores 15 inarea. - A function can have zero, one, or more parameters; parameters supply input data, while the return value sends output data back.
- The return value can be stored in a variable, printed directly, or passed as an argument to another function - it behaves like any other value of that type.
- In VB.NET, the return type is declared with
Asafter the parameter list; in C#, the return type appears before the function name; in Python, no type declaration is needed.