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.

the rules

  1. You always start in what is called “the global frame”

  2. 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.)

  3. 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)

  4. The parent frame will always be the frame that the function was defined in

  5. The first thing defined in any frame are the parameters of the frame’s function (if there are any)

  6. 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

  7. When defining a function, draw an arrow to the function name, parameters, and parent frame

  8. A frame is only closed (or finished) when something has been returned or when there are no more lines to be executed

  9. If the function ends without returning anything, the return value is None