Hello! This is a guide on making environment diagrams for 61A. If you have this playbook open, you should be able to navigate most 61A diagramming questions.

Steps to Diagramming — The Big Picture

  1. Draw the current frame. We start in the very beginning by drawing the global frame. If current frame is not the global frame, make sure to add the function's arguments to the frame.
  2. Start assigning values to variables.
  3. Re-assign variables if they already exist in the current frame and they are getting overwritten.
  4. When looking up values of a variable, look in the current frame. If they aren't in the current frame, look in the parent frame. Keep going until variable is found. If not found, Error.
  5. If there is a function call, find the function being called and evaluate the arguments. Then open a new frame, and start back at step 1 in this new frame.
  6. If you reach a return statement, return the value from the frame and close the frame. Go back to where you stopped in the frame that called this function, and continue from step 2.
  7. If you reach the end without a return statement, simply close the frame and return to where you stopped in the frame that called this function. If you are in the global frame, you're done — flip to the next question.

The Environment Diagram Commandments

<aside> 💡 Things to keep in mind!

</aside>

  1. The parent of a function is the frame in which it was created.
  2. We cannot have two variables with the same name in the same frame.
  3. You can read the values in your parent frame, as well as your grandparent frame, and so on...
  4. You cannot change any values in your parent frame.