Variables and Functions
In CindyScript, variables and functions are not declared explicitly. They are created on demand and are not explicitly typed. This is in sharp contrast to many other programming languages. In this section you will learn under what circumstances one can create functions and variables. You will also learn how to destroy or clear variables and about their scope.
Defining a function in CindyScript is very easy. One simply has to specify the name of a function, provide a parameter list, and write down the body of the function. No explicit typing of arguments or function values is required. In what follows, we provide some examples of simple functions. For example, function
calculates the sum of the first
Functions with more than one argument can be defined similarly. The following function assumes that
sq(a,b):=( n=(b-a); n2=(-n_2,n_1); draw(a,b); draw(a,a-n2); draw(b,b-n2); draw(a-n2,b-n2); )
In this code a few interesting things happen. First of all, the code is in principle procedural. The body of the function has the form
The return value of a function is the value of the last evaluated statement in the function. Thus the following function calculates the arithmetic mean of three entries.
mean(a,b,c):=( sum=a+b+c; sum/3; )
Since functions are not explicitly typed, it is also possible to pass more complex objects as a function's arguments. The function is automatically as polymorphic as possible, restricted only by the generality of the operations used in the function. For instance,
Functions may also be defined recursively. Then a new instance of every function parameter is created for each level of recursion. The following code calculates the factorial of a number:
The following more complicated code calculates the greatest common divisor of two positive numbers:
gcd(a,b):=if(b==0, //End of recursion reached a, //Then return the number a if(b>a, //Perhaps switch parameters gcd(b,a), //switched version gcd(b,mod(a,b)) //Recursion ) );
Variables in CindyScript are defined on their first occurence in code. Once defined, variables remain accessible throughout the rest of the program. A variable may contain any type of object (numbers, strings, booleans, lists, geometric points, or even programs, …). The program
x=3; b=[x^2,x^3]; c=2*b;
f(x):= ( x=x+x; println(x); y="User" ); x="Hello "; y="World"; println(x+y); f(x); println(x+y);
It produces the output
Hello World Hello Hello Hello User
Local variables in a function may be defined explicitly using the
They are automatically removed when the function terminates.
In the following code snippet, as a slight variation of the above program,
f(x):= ( regional(y); x=x+x; println(x); y="User"; ); x="Hello "; y="World"; println(x+y); f(x); println(x+y);
The program produces the output
Hello World Hello Hello Hello World
Run variables in loops are also treated as local variables.
Binding Variables to Functions
Variables in a function (unless defined as local variables) remain visible
after the execution of the function. Besides, variables used in functions
may have initial values that influence the evaluation of the function.
For instance, the following piece of code
a=3; timesa(x):= x*a; println(timesa(2)); a=5; println(timesa(2));
produces the output
The return value of
a=3; timesa(x)::= x*a; println(timesa(2)); a=5; println(timesa(2));
produces the output
Every time the function is called, the original value of
There is one way to intentionally circumvent this binding: The value of
a=3; timesa(x)::= x*a; println(timesa(2)); println(timesa(2,a->10));
This program fragment produces the following output
In mathematics it is often necessary to use mathematical constants like
println(i); repeat(4,i,println(i)); println(i);
It produces the following output:
0 + i*1 1 2 3 4 0 + i*1
If, for instance, the complex unit is needed but the variable
There is another important type of predefined variable. Any geometric element in a construction may be referred to as a predefined variable of the corresponding name. Thus, for instance, a point A can be accessed using variable
User Defined Data
There is also a possibility to associate user defined data to geometric elements. This can be done by the
The usage of this operator is best explained by a examples. Assume that A ad B are geometric objects. The following code associates some data to them:
A:"age"=17; B:"age"=34; A:"haircolor"="brown"; B:"haircolor"="blonde";
The data may be accessed by the same key. So the following code
forall(allpoints(),p, println(p:"age"); println(p:"haircolor"); )
will produce the output
17 brown 34 blonde
A list of all keys of a geometric object may be accessed via the
will produce the following output:
It is also possible to attach key information to lists. By this one can also create custom data that is passed by variables. The following code exemplifies this behavior.
a=; a:"data"=18 print(a:"data")
Caution: The functionality of attaching key data is still subject to change. It is planned to support object like data structures. So the currently implemented feature may not be compatible with future releases.
The content on this page is licensed under the terms of the License.