Next: , Previous: Implicit scaling, Up: Programming


5.10 Functions

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:

  1. Two distinct variables:
         
         int x, x();           
         x=5;
         x=new int() {return 17;};
         x=x();              // calls x() and puts the result, 17, in the scalar x
    
  2. Traditional function definitions are allowed:
         
         int sqr(int x)  
         {
           return x*x;
         }
         sqr=null;           // but the function is still just a variable.
    
  3. Casting can be used to resolve ambiguities:
         
         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.
    
  4. Anonymous (so-called "high-order") functions are also allowed:
         
         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.
    

  5. Anonymous functions can be used to redefine a function variable that has been declared (and implicitly initialized to the null function) but not yet explicitly defined:
         
         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 a fully working version of the GNU library libsigsegv (e.g. 2.4 or later) is installed at configuration time).