The define statement
This statement allows to define new types the parser can recognize along with primitives and classes.
Using a function as a variable
You can use a function as argument of another function by defining this function as a type. This work only at the global level and can't be used inside a class. Once a type is defined, you can't redefine it.
1) define a function:
Ex:
int add(int a, int b) int x = a + b return x
2) define a type, using this function as model:
define ADDING = "add"
3) define another generic function that uses this function as argument:
void myfunc(ADDING a, int x, int y) print a(x,y) return
4) use the generic function:
myfunc("add", 10, 8)
You can now define another function, "mul" for example, with the same model, thus having same return type and same parameters that the "add" function, and use it instead.
myfunc("mul", x, y)
Using an external type
The syntax:
define NEWTYPE
This create a new type you can use in arguments of external functions.
Look at the GTK examples for how to use the new type.
Exercises |
1) Write two integer function "compareInt" and "addInt"
that respectively compare and add two integers. |