Functions and Procedures: global variables
2.2.3 Additional Programming Techniques
Practise exam questionsAdditional Programming Techniques — Note 10 of 12
The use of global variables within functions and procedures:
A global variable is defined in the main program (outside any sub-program) and can be accessed from anywhere in the program, including inside functions and procedures. While useful for sharing data, overusing global variables can make programs harder to debug and maintain.
global keyword: global userID = "Cust001".global keyword inside a function to modify a global variable.score = 0 # Global variable
def add_points(points):
global score
score = score + points # Modifies the global
add_points(10)
add_points(5)
print(score) # Output: 15Ready to test yourself?
Practise 2.2.3 questions