up previous next
1.1.9 Tutorial: defining new functions (advanced)
Please read Tutorial: defining new functions before reading this.

Inside a function it is possible to access variables which were defined outside it. The command TopLevel will make a global variable accessible from inside the function, and parameters may be passed "by reference".

  /**/ define negate(ref X) // pass by reference
  /**/   X := -X;           // changes value of variable OUTSIDE the fn
  /**/ enddefine;           // does not "return" a value, so is a procedure!
  /**/ A := 1;
  /**/ negate(ref A);  // use "ref" also to call the fn
  /**/ println A;      // now see that value has changed

  /**/ define QQx()
  /**/   TopLevel QQ;    // make global variable QQ accessible
  /**/   return NewPolyRing(QQ, "x");
  /**/ enddefine;
  /**/ QQx();
  RingWithID(3, "QQ[x]")