up previous next
Tutorial-08: defining new functions (advanced)
|
Tutorial-08: defining new functions (advanced)
Tutorial-08 for CoCoA-5; please read Tutorial-07 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
-1
/**/ define QQx()
/**/ TopLevel QQ; // make global variable QQ accessible
/**/ return NewPolyRing(QQ, "x");
/**/ enddefine;
/**/ QQx();
RingWithID(3, "QQ[x]")
|