User Tools

Site Tools


Declarations

Variables

A variable, with regard to computer programming, can be thought of as a place to store a piece of information, whether it varies or not. Generally speaking, a variable binds an object to an identifier for access to that object at a later point in the code. That object can be many things, for instance: a number, a string (text), or an as yet undefined collection of information.

Variables are declared using the var keyword. If a variable has a type1) specified, only objects of the given type should be assigned to that variable. Emphasis being on the word should. As mentioned on the main page of this documentation, DAZ Script is dynamically typed2) and as such does not prevent a variable from being assigned a type inconsistent with that which may have been specified at declaration. In fact, DAZ Script variables are always of a type consistent with the type of value they contain.

Note: If you do not initialize your variable upon declaration, that is, assign it some value, it automatically assumes the type and value undefined.

The scope of a variable is always an important factor to consider. The scope of a variable refers to where in the code the variable has meaning. When a variable is said to have a local or lexical scope, it means that the variable only exists, or has meaning, within a subroutine (a smaller portion of the overall program). Conversely, when a variable is said to have a global scope, it means that the variable exists such that it can be accessed from anywhere, by anything. It is considered good programming practice to make the scope of a variable as narrow as possible, to prevent different parts of a program from modifying, using or otherwise conflicting with another part's variables.

In DAZ Script, variables declared with var are local to the enclosing function. If a variable is assigned to implicitly (without being declared using the var keyword), it is automatically declared to exist in the global namespace. Although potentially troublesome, and typically considered to be poor form, it is valid syntax. Using global variables in this manner is not recommended, as it can make your code difficult to debug and maintain.

Note: It is important that you always declare variables before attempting to use them. Failure to do so will result in an error from the interpreter.

Examples

Variable declared, but not initialized.

var sTmp;
// typeof sTmp == undefined
// sTmp == undefined

Variable declared and initialized.

var sTmp = "Temp";
// typeof sTmp == "string"
// sTmp == "Temp"

Variable declared and initialized with one type, later changing type.

var sTmp = "5";
// typeof sTmp == "string"
// sTmp == "5"
 
// Assigned to a Number type
sTmp = 5;
// typeof sTmp == "number"
// sTmp == 5

Functions (Methods)

A function can be thought of as a sequence of statements (a subroutine) that performs a specific task, the same task, each time its run. Functions can be “called”, which means the “current” instruction sequence is temporarily suspended and the sequence of statements in the function are executed, after which point control is passed back to the instruction sequence that made the call. This also means that same sequence of statements can be executed as many times as necessary without the need to be written in the code each time. A function may take an optional set of input parameters (arguments) to parameterize that sequence, and it may also return an optional output (return value).

Functions are declared using the function keyword. They can exist in the global namespace, or in the context of another object.

Examples

A simple function.

function myFunction(){
	// ...statements;
}

A simple function with an argument.

// Create the function
function showMessage( sMessage ){
	MessageBox.information( sMessage, "Message", "&OK" );
}
 
// Call the function
showMessage( "Hello World!" );

A simple function with a return value.

function getWorldGreeting(){
	return "Hello World!";
}
 
var sTmp = getWorldGreeting();
// sTmp == "Hello World!"
1)
A name or label for a set of values and operations that can be performed on that set of values
2)
As opposed to statically typed languages, like C++