Where a variable lives
After this: Say which boxes a function can see, and which are gone when it returns.
On this page · 7 sections · 3 checks
A meeting room has a whiteboard. During a meeting, people write on it. When the meeting ends, the board is wiped for the next group. Notes that have to last are kept on the notice board in the hallway, where every meeting can read them.
A function's frame is a whiteboard. This lesson is about which boxes live on it, which live in the hallway, and what happens to each when the function returns.
A box made inside a function
In total, the line cost = price * countint cost = price * count; makes a box named cost. The trace shows where it goes: into the frame for total, next to price and count. When total returns, the whole frame is gone, and cost goes with it. The program never had a box named cost. It only received the 12 that total returned.
A box made inside a function is a . It belongs to one call. The second function in the trace, other, makes a box that is also named cost. That is a different box in a different frame. The two never meet, and neither function can see the other's.
- Run the programRun it. Watch cost appear inside the frame for total, and watch what the Variables panel shows after total returns.
def total(price, count): cost = price * count return cost
def other(): cost = 100 print(cost)
print(total(3, 4))other()run the program to see its boxes
nothing printed yetPress Run it, then step through the program one line at a time.
- lines
- 0
- prints
- 0
- calls
- 0
Work through it:
total(3, 4)opens a frame withpriceandcount. After its first line, the frame also holdscost, with 12 in it.totalreturns 12 and the frame closes. The Variables panel is back to the program alone, with no boxes at all.other()opens a frame, makes its owncostholding 100, and prints it.
A box made outside
A box made by the program itself, outside every function, is a . A function can read it. Here tax is made on the first line, and total uses it.
tax = 2
def total(price): return price + tax
print(total(10))run the program to see its boxes
nothing printed yetPress Run it, then step through the program one line at a time.
- lines
- 0
- prints
- 0
- calls
- 0
tax sits in the program's frame the whole time, and total looks it up when it needs it: 10 + 2 is 12.
The same name inside and outside
What if a function gives a value to a name that already exists outside? The answer surprises people. The line count = 0int count = 0; inside reset does not touch the outside count. It makes a new local box with the same name, inside the frame. The function prints its own 0, and after it returns, the program's count still holds 5.
count = 5
def reset(): count = 0 print(count)
reset()print(count)run the program to see its boxes
nothing printed yetPress Run it, then step through the program one line at a time.
- lines
- 0
- prints
- 0
- calls
- 0
The rule: reading a name inside a function finds the local box if there is one, and the global box otherwise. Giving a value to a name inside a function makes or changes a local box, never the global one.Making a box inside a function, with its type in front, makes a local box, even when a static box has the same name. To get a value out of a function, return it.
The code
def total(price, count): cost = price * count return cost
def other(): cost = 100 print(cost)
print(total(3, 4))other()12 100
public class Main { static int total(int price, int count) { int cost = price * count; return cost; }
static void other() { int cost = 100; System.out.println(cost); }
public static void main(String[] args) { System.out.println(total(3, 4)); other(); }}12 100
Run it
Change the 100 to another number and run it again: the first line printed does not change.
Check yourself
total makes a box cost on its first line and returns it. After total(3, 4) has returned, does the program have a box named cost?
What does this print?
level = 3
def bump(): level = 99 print(level)
bump()print(level)public class Main { static int level = 3;
static void bump() { int level = 99; System.out.println(level); }
public static void main(String[] args) { bump(); System.out.println(level); }}Two functions each have a line that makes a box named cost. The program calls one, then the other. Which is true?
Honesty notes
- Say that a box made inside a function is local: it lives in that call's frame and is gone when the function returns.
- Say that a function can read a box made outside every function (a global box).
- Say that making a box inside a function (in Java, with its type in front) makes a new local box; the outside box with that name is untouched.
- Say that two functions can each have a box with the same name without clashing.