NC allows you to create arrays of integers, reals, elements, vectors and transforms. Arrays can also be passed to functions.
Just as in the C programming language, an NC array variable is defined by using a pair of square brackets after the variable, with the dimension of the array within the brackets.
- real theta ;
int count ;
element e ;
vector position ;
transform rotation ;
The above defines array variables theta, count, e, position and rotation. The variable theta contains 5 real elements: theta, theta, theta, theta and theta. Each element of theta can be used in any place of an NC program that accepts a real variable. This includes a real argument of a function, for example:
- sin( theta ) ;
Passing arrays to functions
The above example shows how a single element of an array is passed to a function.
An entire array can also be passed by reference to a function. To do this, the function has to accept an array. Just like in the C language, you denote this with a star ahead of the variable when you declare it in the argument list. You can then use that variable within the function as if it is an array, e.g.,
- void myFunction( real *a )
- real d ;
d = a ;
To pass an array to the function, you simply pass the name of the array, i.e., using the theta example described earlier, you can pass the array theta to myFunction with
- myFunction( theta ) ;
integers and real are passed "by value" to a function. If you change the value of the argument of the integer in a function, it would not affect the value of the original variable that was sent to the function. I.e., the function makes a copy of the variable and initializing it to the value that was passed in.
Arrays are different in that they are passed to a function "by reference." I.e. like the C programming language, you are passing the reference of the array to a function. If the function changes an element of an array that is passed to it, the value of the element also changes in the calling function. Indeed, this is a way for you to return multiple values back to a calling program, e.g.,
- void getNiceNumbers( real *result )
result = 2.718 ;
result = 3.14159 ;
result = 1.618 ;
model( "e pi psi" )
float magic ;
getNiceNumbers( magic ) ;
printf( "%f %f %f\n", magic, magic, magic ) ;