Dynamic scoping rules

R uses Lexical scoping rules, unlike R many other languages use Dynamic scoping for assigning values to their free variables.

Its good to know what is Dynamic scoping and how it is different from lexical scoping. So followed by my previous post on lexical scoping I took privilege to write a short post on dynamic scoping too.


In simple terms when applying dynamic scoping rules, search for free variable values behinds from calling function instead of parent environment.

For example in R the following code will print apple as red and values of apple is assigned by applying lexical scoping rules.


In other languages which follows dynamic scoping will print apple as green This is because as soon as goo function starts executing, value for apple is searched from the current environment followed by the environment in which it is called (calling environment). Note that in dynamic scoping search process extents from current environment to calling environment instead of current environment to parent environment. So for the above example we get apple as green.

This difference in scoping rules gives completely different picture in the execution of programing languages. Even though this types scoping make certain tasks easier it brings the same amount of difficulty in introduce bugs and debugging headaches to programmer community. 

Post a Comment

Your email is never published nor shared. Required fields are marked *