Knowledge organisersAdditional Programming Techniques
The use of local variables within functions and procedures:
A local variable is defined inside a sub-program (function or procedure) and can only be used within that sub-program. Once the sub-program finishes executing, the local variable is removed from memory. This prevents naming conflicts between different parts of a program.
def calculate_tax(price):
tax = price * 0.2 # 'tax' is local
return tax
result = calculate_tax(100)
print(result) # Output: 20.0
# print(tax) # ERROR: 'tax' is not defined here