up previous next
1.8.2 First Example of a Package
The following is an example of a package. It could be typed into a window as-is during a CoCoA session, but we will assume that it is stored in a file in the CoCoA directory under the name one.cpkg5 .

Example
package $contrib/toypackage

export ToyTest;

define IsNumberOne(n)
  if n = 1 then return true; else return false; endif;
enddefine;

define ToyTest(n)
  if IsNumberOne(n) then
    print "The number 1";
  else
    print "Not the number 1";
  endif;
enddefine;

endpackage; -- of toypackage
Below is output from a CoCoA session in which this package was used:

Example
-- read in the package:
Source "one.cpkg";
/* */ ToyTest(4);  -- was exported
Not the number 1
/* */ IsNumberOne(4);  -- !!! ERROR !!! as expected: wasn't exported
ERROR: Cannot find a variable named "IsNumberOne" in scope
IsNumberOne(4);
^^^^^^^^^^^

/* */ $contrib/toypackage.IsNumberOne(4);
false