Functions in CAESES Feature Definitions
Sometimes it is helpful to define helper functions within feature definitions to keep the code clean and efficient (i.e. do not use the same code for the same function again and again). Defining Functions allows to structure the code of a feature definition in sub execution units. This may be beneficial to encapsulated recurring calculations on different data and to improve maintainability. Also, it helps readability when implementing complex algorithms and allows to implement recursive algorithms.
How to Write a Function
Functions have a name, an argument list as well as an optional return type:
function <functionName>(<argumentList>): <returnType>
// ... do something
endfunction
Once you have defined such a function, it can be used anywhere within the feature code, similar to an in-built command, by writing its name followed by the argument list.
Example
The following snippet creates a function at the top of the feature definition. The function simply takes the input points and creates a line where the z-coordinates are set to zero:
function my2DCreator(F3DPoint p1, F3DPoint p2) : FLine
line l([p1.getX(),p1.getY(),0], [p2.getX(),p2.getY(),0])
return(l)
endfunction
Now you can use this function in your feature definition:
point start(1,2,3)
point end(2,3,4)
// create a 2D line with z==0
line conn( my2DCreator(start,end) )
Public and Static Functions🚧
Besides helper functions you use inside one feature definition, you might want to reuse those functions in other feature definitions or in parameters as well. Therefore two keywords can be used:
public function <functionName>(<argumentList>): <returnType>static function <functionName>(<argumentList>): <returnType>
While public functions are bound to an instance of a feature definition, static functions are available from the feature definition itself.
Public Functions
Assuming you would like to have a feature creating some object and you would like to have a function available doing something with these objects or variables and returning a result value. The following feature code shows a basic implementation, calculating +1 to the attribute a.
// function definition:
public function calcSome():double
return(a+1)
endfunction
// main:
double a(5)
By creating a feature instance named myInstance, the result of the defined function would be available by using myInstance.calcSome() anywhere a double value is usable in your project like for a parameter.
If you would like to bind a function to a button or any other object editor action use the keyword this to access the feature object.
Static Functions
Assuming you would like to define a library containing independent functions, static functions are making that possible. One rather trivial example would be to calculate the square of an argument value:
// function definition:
static function calcSquare(double a):double
return(a*a)
endfunction
As no feature instance is needed you can access the function by using the name of the feature definition. For example, if the name of the feature definition is mylib you will receive 25 by typing mylib.calcSquare(5).