How to use a global variable in a function?
Published: 2024-03-31 10:48:41

Using [global variables within functions in Python](https://pythonkb.com/how-to-use-a-global-variable-in-a-function/) can be handy for sharing common data across various parts of a program. However, it's crucial to use them judiciously to maintain code clarity and avoid unintended side effects. Here are five examples to illustrate different scenarios involving global variables.

### Example 1: Accessing a Global Variable

```python
x = "awesome"

def myfunc():
    print("Python is " + x)

myfunc()
```

**Explanation:**
- `x` is defined as a global variable with the value `"awesome"`.
- `myfunc()` accesses the global variable `x` and prints `"Python is awesome"`.
  
**Output:**
```
Python is awesome
```

### Example 2: Local Variable Shadowing Global Variable

```python
x = "awesome"

def myfunc():
    x = "fantastic"
    print("Python is " + x)

myfunc()
print("Python is " + x)
```

**Explanation:**
- The global variable `x` has the value `"awesome"`.
- Inside `myfunc()`, a new local variable `x` is defined with the value `"fantastic"`. This shadows the global variable `x` within the function's scope.
- After calling `myfunc()`, the global `x` remains unchanged.

**Output:**
```
Python is fantastic
Python is awesome
```

### Example 3: Modifying Global Variable Inside a Function

```python
x = "awesome"

def myfunc():
    global x
    x = "fantastic"

myfunc()
print("Python is " + x)
```

**Explanation:**
- Initially, `x` is a global variable with the value `"awesome"`.
- `myfunc()` modifies `x` by declaring it as global within the function and then changing its value to `"fantastic"`.
- The change is reflected globally.

**Output:**
```
Python is fantastic
```

### Example 4: Using Global Variables Across Multiple Functions

```python
counter = 0

def increment():
    global counter
    counter += 1

def print_counter():
    print("Counter:", counter)

increment()
increment()
print_counter()
```

**Explanation:**
- `counter` is a global variable.
- `increment()` increments `counter` by 1 each time it is called.
- `print_counter()` prints the current value of `counter`.
- Calling `increment()` twice and then `print_counter()` reflects the global `counter`'s value.

**Output:**
```
Counter: 2
```

### Example 5: Global Variable and Local Scope

```python
message = "original"

def outer():
    message = "outer"
    
    def inner():
        global message
        message = "inner"
    
    print("Before inner():", message)
    inner()
    print("After inner():", message)

outer()
print("In global scope:", message)
```

**Explanation:**
- The global variable `message` is initially set to `"original"`.
- `outer()` defines a local variable `message` with the value `"outer"`.
- `inner()` is defined within `outer()` and modifies the global `message` to `"inner"`.
- The prints demonstrate the scope of changes at each step.

**Output:**
```
Before inner(): outer
After inner(): outer
In global scope: inner
```

These examples demonstrate how global variables interact with local scopes and how the `global` keyword is used to modify global variables within functions. They show the importance of understanding scope to manage variables effectively in Python.