Scope revisited

A variable that is defined at the top level is said to have global scope.

The for, while, and try blocks (but not the if blocks) all introduce a new scope. Variables defined in these blocks are only known to that scope. This is called the local scope, and nested blocks can introduce several levels of local scope. However, global variables are not accessible in for and while loops.

Variables with the same name in different scopes can safely be used simultaneously. If a variable exists both in global and local scope, you can decide which one you want to use by prefixing them with the global or local keyword:

  • global: This indicates that you want to use the variable from the outer, global scope. This applies to the whole of the ...

Get Julia 1.0 Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.