How can we pass a global variable to a function in Python? #1
Loading…
Reference in New Issue
No description provided.
Delete Branch "%!s(<nil>)"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
In Python, you can pass a global variable to a function by explicitly including it as an argument in the function's parameter list. While global variables are accessible within functions, modifying them directly inside a function requires the use of the global keyword.
Here's an example to illustrate how to pass a global variable to a function:
Define a global variable
global_variable = 10
Function that takes a global variable as an argument
def modify_global_variable(value):
global global_variable
global_variable = value
print("Inside the function:", global_variable)
Display the global variable before calling the function
print("Before function call:", global_variable)
Call the function and pass the global variable
modify_global_variable(20)
Display the global variable after calling the function
print("After function call:", global_variable)
Do visit: Python course in Pune for more details