Pop Stack Return Value Rule

After a function has finished executing, Python returns to the line where the function was called (call site). The "Return Value" stays the same, since it just finished evaluating. More formally,

if the following are true:
  • the line at time t = i
  • the tab at time t = j
  • statement at line i, tab j = return rex
  • stack at time t = [ program context with variables: varis and expression state: (expression state with parent stack: parent_stack arguments: arg_stack values: value_stack line: line tab: tab) and control map: control_map, rest ]
  • expression state at time t = "end_expr"

then Return Value at time (t + 1) = Return Value at time t

For example, if the following are true:

  • the line at time 26 = 3
  • the tab at time 26 = 1
  • statement at line 3, tab 1 = return z
  • stack at time 26 = [ program context with variables: [ entry a: 5, [ ] ] and expression state: (expression state with parent stack: [ ] arguments: [ ] values: [ ] line: 5 tab: 0) and control map: [ ], [ ] ]
  • expression state at time 26 = "end_expr"

then Return Value at time (26 + 1) = Return Value at time 26

Try stepping through the simulator to see the "Return Value" set to z (8).

Code Editor
LW Python State
Current Line1Current Tab0Time0
LW Python Simulator

Comments

Please log in to add comments