Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Its worth noting that if, inside a function, you don't have access to any variables from the caller function, it lowers the number of levels you need to look at. For example:</p> <pre><code>variable a; function one() { variable b; // in this function, we can see the global a, local b two(); } function two() { // in this function, we can see the global a, local c // we cannot see the local b of our caller variable c; while (true) { variable d; // here we can see local d, local c, global a } } </code></pre> <p>The idea being that function boundaries limit the visibility of variables, with the global scope being "special".</p> <p>That being said, you can consider removing the specialness of global variables, but allowing the code to specify that they want access to non-local variables</p> <pre><code>variable a; function one() { global a; // or upvar #0 a; variable b; // in this function, we can see the global a, local b two(); } function two() { // in this function, we can see the local c // and the local b of our caller // (since we specifically say we want access to "b" one level up) upvar 1 b; variable c; } </code></pre> <p>It looks complicated at first, but it's really easy to understand once you get used to it (upvar is a construct from the Tcl programming language). What it allows you is access to variables in your caller's scope, but it avoids some of the costly lookup involved by requiring that you specify exactly where that variable comes from (with 1 being one level up the call stack, 2 being two levels up, and #0 being "special" in saying "the uppermost call stack, the global)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload