Variable Scoping and Locals

< Previous | Contents | Manuals Home | Boris FX | Next >

Variable Scoping and Locals

Variables have global scoping within Sizzle by default, ie once you create it in any function, it can be examined by any other function at any later time.

Parameters of functions are inherently local, they do not affect the caller. When a function is called, the initial values of the argument variables are saved,

then assigned to the values from the caller. When the function returns, the arguments are restored to their prior values.

You can declare specific values to be locals as well with the statement local identifier, identifier, identifier ... The current value is saved and the variable becomes undefined. The value is restored when the statement list exits. Although named "local", the variable is still available globally from subroutines the routine calls.

Here's a confusingly contrived example:

function HopSkipJump(miny)

// ... do some stuff ... local meeny

eeny = miny miny = 3

meeny = 5

if (2 == 2)

local eeny eeny = 2

end

end


eeny = "hi" meeny = "bye" miny = "moe"

HopSkipJump(eeny)

...

When this runs, miny is "hi" within HopSkipJump, but back to "moe" afterwards. Meeny is 5 withing HopSkipJump, but is back at "bye" afterwards, because it is a local. Eeny is "hi" after HopSkipJump, because it is set to the parameter miny which is really eeny from outside. The superfluous if statement has no effect, because eeny is declared as a local within it.

©2024 Boris FX, Inc. — UNOFFICIAL — Converted from original PDF.