Knowledge organisersAdditional Programming Techniques
The difference and use of functions and procedures and where to use them effectively
Sub-programs (also called sub-routines) are reusable blocks of code that perform a specific task. There are two types: functions return a value, while procedures perform a task without returning a value. Using sub-programs makes code modular, reusable, and easier to test and maintain.
return keyword.direction and position are given as parameters, USE them — don't do direction = input() inside the function.totalPay = calculatePay(experience, miles).return total, not print(total), inside the function.freeseats("Red"), not freeseats(Red). Store the result: redseats = freeseats("Red").result = newPrice(5, "premium") then print(result).totalPay = calculatePay(experience, miles) then print(totalPay).# FUNCTION - returns a value
def newPrice(nights, room):
if room == "basic":
price = 60 * nights
else:
price = 80 * nights
return price # RETURN, not print
# CALLING the function:
result = newPrice(5, "premium")
print(result) # Output: 400
# PROCEDURE - does not return a value
def greet(name):
print("Hello, " + name + "!")
greet("Alice") # Output: Hello, Alice!