What about global variables?

Global variables are variables that are declared outside of any function and can be accessed and modified by any function within the same program. They have a lifetime that lasts for the entire duration of the program, meaning they remain in memory until the program terminates.

Here’s an example in Python:

global_var = 20  # This is a global variable

def my_function():
    print(global_var)  # Accessing the global variable

my_function()  # This will print 20

def modify_global():
    global global_var  # Declare that we want to use the global variable
    global_var = 30  # Modify the global variable

modify_global()
print(global_var)  # This will print 30

In this example, global_var is a global variable that can be accessed and modified by the function modify_global.

0 Comments

no data
Be the first to share your comment!