# Scope and Passing Arguments

> Where a name can be seen in a program, and why names inside a function stay local. What changes outside when a function changes its argument.

**Course:** [Programming Basics](https://learn.benibix.com/learn/development-concepts)  
**Time:** 13 min

**After this lesson you can:**

- Explain local and global scope
- Tell pass by value from pass by reference
- Predict whether a call changes a list outside

## Where a name can be seen, and what a function can change

Scope is the part of a program where a name can be seen. A name made inside a function is local. It exists only while that function runs, and code outside cannot see it. A name made at the top of a file is global. Every function in that file can read it.

Local names keep functions apart. Two functions can each have a variable called total, and they never clash. When the function ends, its local names are thrown away. Call it again and they start fresh. That is what makes a function safe to reuse.

Python looks a name up from the inside out. First it checks the function's own names, then the file's global names, then the built-in ones like print. So a local variable called len hides the built-in len inside that function. Pick names that do not shadow ones you need.

The second question is what a function gets when you pass it a value. With pass by value, it gets a copy. Changing the copy leaves the caller's value alone. With pass by reference, it gets a link to the real value, so its changes show up outside too.

Python does a bit of both. The function gets the same object the caller has, not a copy. For numbers, strings and tuples, which cannot change, that feels like pass by value: n = n + 1 inside makes a new number and the caller's stays the same.

Lists can change in place, and so can key and value pairs. So a function that adds an item to a list changes the caller's list as well. That is just like pass by reference. To leave the caller's list alone, pass in a copy, or build and return a new list.

The mistake that bites is a function that quietly changes a list it was only meant to read. The caller's data is now different, and the bug shows up far away. So it is safer to return a new value than to change the one you were given.

```python
def add_one(n):
    n = n + 1          # a new number, local to the function

def add_item(items):
    items.append(4)    # changes the caller's list

score = 5
add_one(score)
print(score)           # 5

nums = [1, 2, 3]
add_item(nums)
print(nums)            # [1, 2, 3, 4]
```
The number outside is untouched. The list outside changes, because the function worked on the same list.

![Two halves. Pass by value: score holds 5, a copy goes into the function's n, which becomes 6, and afterwards score is still 5. Pass by reference: the function's n points back at score, the value becomes 6, and afterwards score reads 6.](https://learn.benibix.com/diagrams/dev7-pass-by.svg)

### Example: Jack's report loses its first row

Jack's function prints a list of 20 orders. It removes the header row first, with orders.pop(0).

After the call, his main list has 19 orders, and the next report is one short. He changes the function to work on orders.copy(). The main list keeps all 20.

**Words in this lesson:** [Scope](https://learn.benibix.com/term/development/scope), [Pass by Value](https://learn.benibix.com/term/development/pass-by-value), [Pass by Reference](https://learn.benibix.com/term/development/pass-by-reference)

**Question:** A function runs items.append(4) on a list it was given. What happens to the caller's list?

**Answer:** It also gains the 4, because both names share one list. A Python function gets the same list object, not a copy. append changes that list in place, so the caller sees the 4 too.

---

Source: https://learn.benibix.com/learn/development-concepts/scope-and-passing-arguments
Author: Behnia Zarei (a developer in Vienna, Austria, who builds and teaches data, AI and business intelligence at BENIBIX)
Publisher: BENIBIX Learn (https://learn.benibix.com/)
Last updated: 2026-11-19
Licence: CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Quote freely with attribution.
