Stack Diagram -- Python

14,069

As per the definition of the Stack Diagram:

Each function is represented by a frame. A frame is a box with the name of a function beside it and the parameters and variables of the function inside it.

This means that you also need to account for variables defined within a function. It is better to maintain the value of y as y=3 instead of y=x+2 - since we track the variable values and are not typically concerned with how those values were obtained

To answer your second query - it appears from the description that a stack diagram is used to mainly track variable to function name mapping. However, please remember that when language processors and runtimes are developed, they do make necessary additions to the proposed stack diagram principle that makes it easy or efficient to process variables, or identify errors.

Hopefully, this post clarifies your doubts

Share:
14,069
Quester
Author by

Quester

Updated on June 04, 2022

Comments

  • Quester
    Quester almost 2 years

    I am trying to completely understand the "Stack Diagram" principle. Could someone check if I am correct please?

    So far my understanding is that the stack diagram is used to keep track of the variables used in the functions within the code.

    However I am not sure if a new variable is created within a certain function, should it be included in the stack diagram.

    For instance, should we include the variable "p" in the stack diagram? Let's say:

    def g(y):
        p = A(y, y)  
        print z, p
        return p
    
    def A(x, y):
        x = x + 1
        return x * y
    
    x = 1
    y = x + 2
    

    I guess my stack should look something like:

    <module>  x --> 1
              y --> 3   (Should I put 3 or x + 2 here)
    
    fct g     y --> 3 (should I stop here or should I include a line for the variable p)
    
    
    fct A     x --> 4
              y --> 3 
    

    The final question is: Should we mention anything about what other things the function does. Like in the case of function A, it returns x*y = 12. Should we include this in the stack diagram or is the diagram correct the way it is?

    Thanks