f(n):=sum(1..n,i,i^2)
n
squares. For instance, after this definition, f(4)
evaluates to 30
.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); )
(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.mean(a,b,c):=( sum=a+b+c; sum/3; )
mean([3,4],[2,7],[4,7])
evaluates to [3,6]
.fac(n):=if(n==0,1,n*fac(n-1));
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 ) );
x=3; b=[x^2,x^3]; c=2*b;
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);
Hello Hello Hello User
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);
Hello Hello Hello World
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);
0 + i*1 1 2 3 4 0 + i*1
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
.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