Environment Diagrams Quick Review
Environment Diagram = A cool way to diagram a function and understand what the function is actually doing
why environment diagrams?
At first environment diagrams can be kind of confusing and weird. I mean, why do I have to sit here and diagram these functions, when I already know how to write functions? How is this even supposed to help me?
Well, first off, no matter how good you are at programming, it always helps to be able to diagram what you are doing, and make sure that you are writing the correct code. This class ramps up very quickly, and having some way to check your work and make sure you really understand what’s going on is super helpful. Plus, the great thing about environment diagrams is, as opposed to writing code, which takes a lot of intuition, writing out an environment diagram is very formulaic and just requires you to be able to follow a set of steps. Remember this: the rules of environment diagrams will never change throughout the semester; we may add in some new rules, but the old ones will always stay the same.
You always start in what is called “the global frame”
Whenever you call a function, open a new frame, write in the name of the function and the parent frame, unless the function is built in (like add, max, etc.)
If the function is built in, do not open a new frame, just write in the function’s return value (ex: x = max(2, 3), just write x = 3)
The parent frame will always be the frame that the function was defined in
Whenever you define a new variable or function in your current function, add that variable/function and its value to the current frame in your diagram
When defining a function, draw an arrow to the function name, parameters, and parent frame
A frame is only closed (or finished) when something has been returned or when there are no more lines to be executed
If the function ends without returning anything, the return value is None