Variables are declared using the var
keyword. If a variable has a type (datatype - a name or label for a set of values and operations that can be performed on that set of values) specified, only objects of the given type should be assigned to the variable. Emphasis being on the word should. As mentioned on the main page of this documentation, DAZ Script is dynamically typed (as opposed to statically typed languages, like C++) 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.
In DAZ Script, variables declared with var
are local to their enclosing block. 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.
var sTmp; // typeof sTmp == undefined // sTmp == undefined
var sTmp : String; // typeof sTmp == "string" // sTmp == undefined
var sTmp = "Temp"; // typeof sTmp == "string" // sTmp == "Temp"
var sTmp : String = "Temp"; // typeof sTmp == "string" // sTmp == "Temp"
var sTmp : String = "Temp"; // typeof sTmp == "string" // sTmp == "Temp" // Assigned to a Number type sTmp = 5; // typeof sTmp == "number" // sTmp == 5
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.
function
function myFunction(){
// ...statements;
}
function
with an argument// Create the function function showMessage( sMessage ){ MessageBox.information( sMessage, "Message", "&OK" ); } // Call the function showMessage( "Hello World!" );
function
with a return
valuefunction getWorldGreeting(){ return "Hello World!"; } var sTmp = getWorldGreeting(); // sTmp == "Hello World!"