The scope affects whether a given variable can be accessed from a given location in the code and, if there are multiple variables with the same name, which one is meant by a given use of the name.
Using static scope you can use variables that are defined globally or inside a code block that you're nested in. This means that, when using nested and/or anonymous functions (in languages that support them), such a function can access variables of the enclosing function even after that function is no longer running. If multiple variables with the same name could be accessed by that logic, the inner-most one is chosen.
Using dynamic scope you can use variables that are defined globally or which exist in any activation record currently on the stack. This means that you can access local variables of another function if (and only if) that function called you. If multiple variables with the same name could be accessed by that logic, the one that has been most recently pushed onto the stack is chosen.
Suppose i have 2 functions (main) and (fact).The activation record for main knows the address of fact so that it can call it.How? Do main and fact functions link before compiling?
(Assuming the functions are defined in different compilation units - otherwise linking is irrelevant) No, linking happens after compilation. The address of fact is simply not known until linking. Before then, the object file simply contains a symbol as a placeholder instead of the address. The linker then replaces that placeholder with the actual address.
PS: If I may ask, where do these questions come from? It sounds like you're working through a compilers book (or following a compilers course), but such a book/course should already contain the information you're looking for.
Yes you happen to be right.I have been reading a book on compiler construction but still the picture was blur in my mind.
So,in dynamic scoping if the varibles are declared in main and then assigned values there and then main calls fact() then the fact() would be able to access the variables(local) of main() because the activation record of main()
is still on the stack?