Map and Table calculation

User-defined functions (advanced)

Besides the normal MapCalc and TabCalc expressions, the ILWIS calculator has the basic functionality of a Pascal like program. With this Pascal like functionality you can build functions that contain IF, THEN, ELSE loops, FOR loops, WHILE loops and REPEAT/UNTIL loops.

Using loops is an advanced subject and the user should fully understand the basics of user-defined functions. If you are more interested in the basic functionality go to Map and Table calculation : User-defined functions.

Syntax

The syntax for a 'calculator' program/function with parameters is:

 

Function FunctionName (ParamDomain Param [,ParamDomain Param]) : OutputDomain
Begin
[Declarations;]
[Statements;]
  Return  Expression;
End;

where:

FunctionName is the name of the function as specified in the Create Function dialog box.
ParamDomain is the domain of an input parameter. You can use e.g. Value | String | Coord | Color | Bool. By default, the domain of input parameters is set to Value.
Param is the name of an input parameter.
Parameter names:
  • must start with a character from A to Z,
  • may contain characters from A to Z, digits 0 to 9, and underscores.
OutputDomain is the name of the domain that should be used for the output object (a column or a map). You can choose any domain available in your data set as well as a coordinate system. By default the output domain is set to the system Value domain. When you choose a value output domain, the value range is determined by the selected domain.
Declarations is the section where the variables have to be declared before they can be used in expressions. The full set of variable types that are functioning are: Var | Int | Real | Value | String | Coord | Color (Real and Value are synonyms, Var and Int are also synonyms).
Statements are instructions to ILWIS to do something. Statements can be single or multiple. Multiple statements are always grouped inside the keywords Begin and End and each statement must be separated by a semicolon. A group of statements is often referred to as a Compound statement
Expression is the expression that the function should perform, as defined before in the Create Function dialog box.

The words Function, Begin, Return and End are keywords in ILWIS and have a fixed meaning (see below).

Note:

Declarations:

A variable can be thought of as a box that contains data of a specific form or type. Before being used in expressions, variables have to be declared in the following way:

int i, j; string s, t;

real x; value y;

Statements:

In ILWIS, a statement is an instruction to ILWIS to do something. Multiple statements are always grouped inside the keywords Begin and End and each statement must be separated by a semicolon. A group of statements is often referred to as a Compound statement.

Assignment statement:

One of the most basic kinds of statements is the assignment statement. It is used to store a value into a variable. The general form for an assignment statement is:

Designator := Expression;

The Designator is just a name of a variable that is to have its value set, and the result of the expression is what the variable will be set to. Expressions can be quite complicated but they can also be as simple as just being a number.

Examples

Example 1 : If, then, else statements in user-defined functions
Example 2 : For loops in user-defined functions
Example 3 : While loops in user-defined functions
Example 4 : Repeat/until loops in user-defined functions

Example 1 : If, then, else statements in user-defined functions

The IF statement is used to do different things depending on a certain condition. A condition is the result of a logical expression (i.e. a Boolean expression). The general syntax for an IF, THEN, ELSE statement is:

if a then b [else c]

where:

a = a boolean expression
b = either a statement or a compound statement
c = either a statement or a compound statement

 

Function ifthenelse(Value x) : Value

Begin

var n;

if (x<10) then

   begin

   n := sq(x);

   end

else 

   begin

   n :=sqrt(x);

   end;

Return n;

End;

For each input value: if the input value is smaller than 10, return the square of the input value, else return the square root of the input value.

Example 2 : For loops in user-defined functions

The FOR loop used to execute a group of statements multiple times. A FOR loop works from an initial loop counter value until an end loop counter value. The general syntax for a FOR statement is:

for a to b [step c] do d

where:

a = loop counter assignment
b = end value loop counter
c = expression
d = either a statement or a compound statement

 

Function forloop(Value a) : Value a

Begin

var i,b;

   i :=1;

   b :=a;

for i :=1 to 100 step 4 do 

   b :=b+1;

Return b;

End;

For each input value: count from 1 to 100 with steps of 4 (thus 25 times) and, each time, add 1 to the input value.

Example 3 : While loops in user-defined functions

The WHILE loop is used to execute a group of statements multiple times. A WHILE loop works as long as a logical expression for the loop counter is true. The general syntax a WHILE statement is:

while a do begin b end

where:

a = logical expression 
b = either a statement or a compound statement

 

Function whileloop (Value a) : Value

Begin

var i,b;

   i:=1;

   b:=a;

while i<a do

begin

   b:=b+1;

   i:=i+1;

end;

Return b;

End;

For each input value: set the counter to 1, then if the counter is smaller than the input value, add 1 to the input value and add 1 to the counter. When the counter equals the input value, the output value is returned.

Example 4 : Repeat/until loops in user-defined functions

The REPEAT/UNTIL statement is used to execute a group of statements multiple times. A REPEAT/UNTIL loop works until a logical expression for the loop counter becomes true. The general form a REPEAT/UNTIL statement is:

repeat a until

where:

a = either a statement or a compound statement 
b = boolean expression

 

Function repeatloop (Value a) : Value

Begin

var i,b;

   i:=1;

   b:=a;

repeat

   b:=b+1;

   i:=i+1;

until i>=a;

Return b;

End;

For each input value: set the counter to 1, then add 1 to the input value, add 1 to the counter, check whether the counter is not yet larger than the input value, and repeat adding. When the counter equals the input value, the output value is returned.

The significant difference between a REPEAT/UNTIL loop and a WHILE loop is that every REPEAT/UNTIL loop is entered at least once because the test of the Boolean condition occurs at the bottom of the loop; a WHILE loop is not entered at all if the Boolean condition is initially false. Also the WHILE ends when the Boolean condition becomes false, whereas the REPEAT/UNTIL ends when the condition becomes true.

 

Out1=ifthenelse(Value)

Out2=forloop(Value)

Out3=whileloop(Value)

Out4=repeatloop(Value)

 

Value

Out1

Out2

Out3

Out4

0

0

25

0

1

1

1

26

1

2

2

4

27

3

3

3

9

28

5

5

4

16

29

7

7

5

25

30

9

9

6

36

31

11

11

7

47

32

13

13

16

4

41

31

31

25

5

50

49

49

See also: