Functions and Procedures: local variables
2.2.3 Additional Programming Techniques
Practise exam questionsAdditional Programming Techniques — Note 9 of 12
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 hereReady to test yourself?
Practise 2.2.3 questions