next up previous contents index
Next: Function declarations Up: Declarations Previous: Declarations   Contents   Index

Variable declarations

NASL1 had only global variables. NASL2 uses global and local variables. Local variables are created in a function and stop existing as soon as the function returns. When the interpretor looks for a variable, it first searches in the current function context, then in the calling context (if any), etc., until it reaches the top level context that contains the global variables.

Normally, you do not need to declare a variable: either it exists, because you already used it in this context, or because a calling function used it, or it will be created in the current context. However, this may be dangerous in some cases:

  1. if you want to write into a global variable from within a function and cannot be sure that the variable was created first in the top level context, or created as a local variable in a calling function context.
  2. if you want to be sure that you are creating a brand new local variable and not overwriting a global variable with the same name.
So you can explicitly declare a variable:

If the variable already exists in the specified context, you will get an error message, but this will work!


next up previous contents index
Next: Function declarations Up: Declarations Previous: Declarations   Contents   Index
Michel Arboi 2005-04-29