1 Simple package

An example package that contains one function is made showing how to save it and load it into Mathematica and to update it again by adding a second function to it.

Each time a new function is added to the package, the package has to be reloaded in order to use the new function.

This note is not meant by any means to be comprehensive, but only to give someone who is new at Mathematica the minimal set of commands to make a basic package.

More details about packages are found in Mathematica help pages.

  1. Open a new text file called foo.m (using any text editor) and write in it the following

    BeginPackage["foo`"] 
    Unprotect @@ Names["foo`*"]; 
    ClearAll @@ Names["foo`*"]; 
     
    f::usage = "f[x]" 
    Begin["`Private`"] 
     
    f[x_] := Module[{}, x^2]; 
     
    End[]; 
    Protect @@ Names["foo`*"]; 
    EndPackage[];
     
    

    In the above foo[x] is a public function which can be accessed from outside.

  2. Now save the file foo.m. Assuming it is saved to C:/mydata/foo.m
  3. To use the function f[x] defined in the package, open Mathematica notebook and load the package as follows (this is one easy way to load the package, there are other ways).

    Get["c:/mydata/foo.m"]
     
    

    Now the package is loaded and you can call the function defined inside the package as follows

    f[2]
                                                                                             
                                                                                             
           
    

    The function can also be called by explicitly pre-appending the package name to the function (which I prefer, as it makes it clear where this function came from) as follows

    foo`f[2]
     
    
  4. To add a second function, say g[x] to the package, open foo.m again using text editor, and change it to look as follows:

    BeginPackage["foo`"] 
    Unprotect @@ Names["foo`*"]; 
    ClearAll @@ Names["foo`*"]; 
     
    f::usage = "f[x]" 
    g::usage = "g[x]" 
    Begin["`Private`"] 
     
    f[x_] := Module[{}, x^2]; 
    g[x_] := Module[{}, x^4]; 
     
    End[]; 
    Protect @@ Names["foo`*"]; 
    EndPackage[];
     
    

    Now save the file foo.m.

  5. To use the new function added to the package, the package needs to be reloaded again

    Get["c:/mydata/foo.m"]
     
    
  6. To see which functions in your package type

    ?foo`*
     
    

    That is all. To add more functions, repeat the above steps.

  7. The above functions \(f(x),g(x)\) are public functions. This is because each of them had a usage statement. We can add private functions that are used internally in the package, which the user can not call or see as follow. Any function that has no usage statement is a private function.

    BeginPackage["foo`"] 
    Unprotect @@ Names["foo`*"]; 
    ClearAll @@ Names["foo`*"]; 
     
    f::usage = "f[x]" 
    g::usage = "g[x]" 
    Begin["`Private`"] 
     
    (*these are public functions *) 
    f[x_] := Module[{}, h[x]]; 
    g[x_] := Module[{}, x^4]; 
     
    (*this is private function because it has no usage statement*) 
    h[x_]:= Module[{},x^2]; 
     
    End[]; 
    Protect @@ Names["foo`*"]; 
    EndPackage[];
     
    

I’d like to thank Bill Rowe and Istvan Zachar on the Math newsgroup for helpful suggestions while working on this note.