Saturday, July 4, 2020

Functions & Scopes


The scope of a name (e.g., a variable name) is the part of a code where the name is properly recognizable.

For example, the scope of a function's parameter is the function itself. The parameter is inaccessible outside the function.

Let’s consider the below snippet.








Here, we get the following error message.





So let’s consider the below snippet.










Output is







A variable existing outside a function has a scope inside the functions’ bodies, excluding those of them which define a variable of the same name.


It also means that the scope of a variable existing outside a function is supported only when getting its value (reading). Assigning a value forces the creation of the function's own variable.

No comments:

Post a Comment