Functions
A function is a type of sub-routine that does return a value. It must be defined before it can be used.
When a function is defined it can have zero, one, or more parameters. These are known as formal parameters. The formal parameters will 'catch' values that are passed to the function when it is 'called'.
The returned value is normally assigned to a variable, or it can be used in another sub-routine.
Example - General
def functionName(formalParameter1: dataType, formalParameter2: dataType, ...) -> returnDataType:
"""docstring"""
<function code>
return <result>
A function can be called from the main program, or from another sub-routine. The function call can pass zero, one, or more actual parameters to the function.
functionResult = functionName(actualParameter1, actualParameter2, ...)
Example - No formal parameters
def pickRandom() -> int:
"""Picks a random number from 1 to 10."""
# Get extra code
import random
# Intialise local variable
randomNumber = 0
# Pick random number
randomNumber = random.randint(1, 10)
# Return random number
return(randomNumber)
This function can be called from the main program, or from another sub-routine.
myNumber = pickRandom()
print("Random number: " + str(myNumber))
Example - One formal parameter
def square(number: int) -> int:
"""Calculates the square of a number."""
# Intialise local variable
squared = 0
# Calculate result
squared = number ** 2
# Return the result
return squared
This function can be called from the main program, or from another sub-routine.
result = square(2)
print("Result: " + str(result))
Example - Multiple formal parameters
def powerOf(number: int, power: int) -> int:
"""Calculates a number raised to a power."""
# Intialise local variable
result = 0
# Calculate result
result = number ** power
# Return the result
return result
This function can be called from the main program, or from another sub-routine.
result = powerOf(5, 3)
print("Result: " + str(result))
Return multiple values
Functions can return a tuple [term not part of Higher] that contains multiple values, similar to an array. These values can be assigned to individual variables when returned.
def myData() -> tuple:
"""Return an integer and a string."""
# Return a tuple
return 21, "Tom"
# Assign to a tuple
myTuple = myData()
# Display the tuple
print(myTuple)
# Display individual values
print(myTuple[0])
print(myTuple[1])
# Assign directly to variables
age, name = myData()
# Display values
print(name + " is " + str(age) + " years old.")