up previous next

passing function parameters by reference

ref X
  where X is the identifier of a CoCoA variable.

The keyword ref is used to pass a parameter "by reference" to a function which may modify its value (e.g. append ). The keyword ref alerts the programmer to the possibility that the value may be changed during the call.

To write a new function which can modify some parameters use the same keyword ref to identify which formal parameters are to be passed by reference. The following example illustrates the difference between passing by reference and passing by value.

/**/ Define CallByRef(ref L)  -- "call by reference": The variable referred
/**/   L := "new value";       -- to by L is changed.
/**/ EndDefine;
/**/ M := "old value";
/**/ CallByRef(ref M);  -- here "ref" recalls that M might change
/**/ PrintLn M;
new value

/**/ Define CallByVal(L)  -- "call by value": The value of L is passed to
/**/   L := "new value";  -- the function.
/**/   Return L;
/**/ EndDefine;
/**/ L := "old value";
/**/ CallByVal(L);
new value

/**/  PrintLn L;
old value

See Also