The Box Method for Tracing Code

advertisement
The Box Method for Tracing Code
Draw a box labeled globals
Inside that box, create one smaller box for each global constant or global variable
Label each little box with the name of the global constant or variable
Put the value of the global in the box, or ? to indicate an uninitialized variable
Draw a box labeled main
Inside that box, create one smaller box for each constant or variable declared inside
main
Label each little boxes with the name of the constant or variable
Put the value of the variable/constant in the box, or ? to indicate an uninitialized
variable
Trace the code, line by line, crossing out and writing to the side new values of the
variables as the code changes them
When a function is called
Create a new big box for the function
Label this new box with the name of the function
Draw a line from the calling box to the function box
Create smaller boxes inside the function box:
1 box for each parameter
1 box for each local variable (variable declared inside the function)
Parameter Passing
Initialize the parameter values by copying into the small box:
Value parameters: Copy the value of the argument into the parameter’s box
Reference parameters: Draw an arrow from the parameter’s box to the box for the
argument
Trace the function code line by line, changing the values in the local boxes.
If there is no local box for the variable name in the code, check the globals box next.
If there is a modification to a value and the box has an arrow in it,
Follow the arrow and make the change in the pointed to box
Returning from a function
When the function ends, draw an arrow back from the function box to the calling box
Write the value of any return value on that arrow
Store the return value in the box for the variable being assigned the return value
Draw a line through the function box to indicate that the function has ended
Continue executing the code from the next line after the function call
Download