About the Constant Function

A key difference between DScript and JavaScript is DScript includes the concept of a Constant. A constant is simply a function declared with no input arguments. Constants have a peculiar property in that they are executed only once and thereafter remember their value in all subsequent evaluations.

For example, the following code creates two constants called Income and Age and then uses them to assign a value to Qualifies:

Qualifies:=Income>100000||Age>18&&Income>30000||Age>35
Income:=asknumber("What is your annual income?") 
Age:=asknumber("How old are you?")

If Age were defined as the variable:

var Age=asknumber("How old are you?");

then this question would be asked, even if its result was never needed (if Income is greater than $100,000 for example).

If Age were defined as the function:

Age(none):=asknumber("How old are you?")

then this question might be asked twice when evaluating the Boolean expression for Qualifies because Age is used twice.

A constant has properties of both a function and a variable. It acts like a function the first time its value is requested. From then on, it acts like a variable by remembering its first result.

Because JavaScript does not have a syntax for creating constants, DScript has added the Define operator (:=). This operator can be used to create standard functions as well as constants.

See Define.

For example, the JavaScript code:

function Square(x) {
    return x*x;
}

is identical to the DScript code:

Square(x):={
    return x*x;
}

and is equivalent to the more streamlined DScript code:

Square(x):=x*x

The JavaScript function statement requires an argument list (which constants do not have) and requires all statements to be enclosed in braces (which DScript does not require if there is only one statement). In DScript programming, there is never a need to use the function statement because the Define operator performs the same task and is generally easier to use.