Next: Arrays, Previous: Implicit scaling, Up: Programming
Asymptote
functions are treated as variables with a signature
(non-function variables have null signatures). Variables with the
same name are allowed, so long as they have distinct signatures.
Functions arguments are passed by value. To pass an argument by reference, simply enclose it in a structure (see Structures).
Here are some examples of Asymptote
functions:
int x, x(); x=5; x=new int() {return 17;}; x=x(); // calls x() and puts the result, 17, in the scalar x
int sqr(int x) { return x*x; } sqr=null; // but the function is still just a variable.
int a, a(), b, b(); // Valid: creates four variables. a=b; // Invalid: assignment is ambiguous. a=(int) b; // Valid: resolves ambiguity. (int) (a=b); // Valid: resolves ambiguity. (int) a=b; // Invalid: cast expressions cannot be L-values. int c(); c=a; // Valid: only one possible assignment.
typedef int intop(int); intop adder(int m) { return new int(int n) {return m+n;}; } intop addby7=adder(7); write(addby7(1)); // Writes 8.
void f(bool b); void g(bool b) { if(b) f(b); else write(b); } f=new void(bool b) { write(b); g(false); }; g(true);
Asymptote
is the only language we know of that treats functions
as variables, but allows overloading by distinguishing variables
based on their signatures.
Functions are allowed to call themselves recursively. As in C++, infinite
nested recursion will generate a stack overflow (reported as a
segmentation fault, unless the GNU library libsigsegv
is
installed at configuration time).