Mathematica 9 is now available
THIS IS DOCUMENTATION FOR AN OBSOLETE PRODUCT.
SEE THE DOCUMENTATION CENTER FOR THE LATEST INFORMATION.
Mathematica > Core Language > Package Development > Namespace Management >

BeginPackage

BeginPackage["context`"]
makes context` and System` the only active contexts.
BeginPackage["context`", {"need1`", "need2`", ...}]
calls Needs on the needi.
  • BeginPackage is typically used at the beginning of a Mathematica package.
  • The interpretation of symbol names depends on context. BeginPackage thus affects the parsing of input expressions.
Commands to define a simple package:
The context for symbol creation has been changed to square`:
The context path for symbol lookup includes square`:
Here are the commands defining the function in the package:
Restore the context, but leave square` on the context path for symbol lookup:
Since EndPackage leaves square`square on the context path, the function defined is visible:
Get the square of 12:
Commands to define a simple package:
In[1]:=
Click for copyable input
The context for symbol creation has been changed to square`:
In[2]:=
Click for copyable input
Out[2]=
The context path for symbol lookup includes square`:
In[3]:=
Click for copyable input
Out[3]=
Here are the commands defining the function in the package:
In[4]:=
Click for copyable input
In[5]:=
Click for copyable input
Restore the context, but leave square` on the context path for symbol lookup:
In[6]:=
Click for copyable input
Since EndPackage leaves square`square on the context path, the function defined is visible:
Get the square of 12:
In[8]:=
Click for copyable input
Out[8]=
Use BeginPackage and EndPackage to start and end a package in a file:
The package can be loaded with Get:
Once loaded the definitions work:
New in 1
Ask a question about this page  |  Suggest an improvement  |  Leave a message for the team