Variables and Functions


In CindyScript, variables and functions do not have to be declared explicitly. They are created on demand and do not possess an explicit typing. 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 Functions


Defining a function in CindyScript is very easy. One has simply to define 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. We provide some examples for simple functions. For example,

f(n):=sum(1..n,i,i^2)


calculates the sum of the first n squares. For instance, after this definition, f(4) evaluates to 30.

Functions with more than one argument can be defined similarly. The following function assumes that a and b are two-dimensional vectors and draws a square whose edge is defined by these two vectors:

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 (statement_1;…;statement_k). Furthermore, the function uses the variables n and n1. These variables are created when the function is first called. However, they are not local. Their value will be visible also after the function is called.

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 feed more complex objects to the arguments. The function will automatically be as polymorphic as possible, restricted only by the generality of the operations used in the function. For instance, mean([3,4],[2,7],[4,7]) evaluates to [3,6].


Recursive Functions


Functions can also be defined recursively. For the parameters there will be always a new instance for each level of recursion. The following code calculates the factorial of a number:

fac(n):=if(n==0,1,n*fac(n-1));


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
                 )
             );



Defining Variables


Variables in CindyScript are defined when they first occur in the code. A variable will be present 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;


assigns to x the value 4, to b the value [9,27], and to c the value [18,54]. A variable that is defined in a function will also be visible outside the scope of the function. Exceptions to this rule are the parameters of the function and explicitly defined local variables. The following program exemplifies the scope of variables:

f(x):= (
  x=x+x;
  println(x);
  y="User"  
);
x="Hello ";
y="World";
f(x);
println(x+y);


It produces the printout

Hello Hello  
Hello User 


Local variables can be defined explicitly by the createvar(…) operator and removed by the removevar(…) operator. In the following small variation of the above program, y is a local variable within the function:

f(x):= (
  createvar(y);
  x=x+x;
  println(x);
  y="User";
  removevar(y);
);
x="Hello ";
y="World";
f(x);
println(x+y);


The program produces the output

Hello Hello  
Hello World 


Run variables in loops are also treated as local variables.


Predefined Constants


In mathematics it is often necessary to use mathematical constants like pi or the imaginary unit i. These variables are predefined in CindyScript. It is then possible to write a complex number for instance as 3+i*5. However, it is always possible to redefine the value of those variables. Thus it is always possible to use these variables as run variables in loops. The following program illustrates this feature:

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 i is overwritten, then it is still possible to access this value by the operator complex(0,1). Other predefined variables are true and false for the logical constants, and the empty list nil.

There is another important type of predefined variable. Any geometric element that is present in a construction will be the predefined value of the variable with the corresponding name. Thus, for instance, a point A can be accessed by the variable A. More detailed information on this topic can be found in the section Accessing Geometric Elements.





Page last modified on Friday 09 of December, 2005 [20:32:34 UTC].
The original document is available at http://doc.cinderella.de/tiki-index.php?page=Variables%20and%20Functions