I'm trying to learn to reuse functions. I have created a .wl file with the following content. As I understand the last line without semicolon is the function output. Should each function have it's own block of Begin[] and End[]?


Begin["`Private`"] (* Begin Private Context *) 

(* The function works fine if declared in the notebook. *)
vectorDeriv[v_,bc_Integer] := 
vFFirstDer = D[v,{t}];
vFSecondDer = D[v, {t,2}];
vFFirstDerNorm = N[Norm[vFFirstDer] /. t -> bc];
vFSecondDerNorm = N[Norm[vFSecondDer] /. t -> bc];
{vFFirstDer, vFSecondDer, vFFirstDerNorm, vFSecondDerNorm}
End[] (* End Private Context *)

When I try to call the function from the notebook I get no output.

 (* the path is correct *)
In[39]:= Get["C:\\Program Files \

Out[39]= "vectorFunctions`Private`"

In[40]:= vF = {Sin[2 t], E^(3 t), t^3 - 4 t};

In[41]:= vectorDeriv[{Sin[2 t], E^(3 t), -4 t + t^3}, 2]

Out[41]= vectorDeriv[{Sin[2 t], E^(3 t), -4 t + t^3}, 2]

Am I missing something with the Private vs Public scope? Should modules be kept in only Mathematica's running directory?

  • $\begingroup$ You do not need to create a package with BeginPackage etc. to simply re-use function definitions. It is sufficient to put the definitions into an .m or .wl file and load them with Get. Get just evaluates whatever is in your file—it does nothing more and nothing less. $\endgroup$
    – Szabolcs
    Sep 7, 2017 at 21:08
  • $\begingroup$ Can you format the question for readability? You have already asked many questions on StackExchage, so I am sure you are familiar with how to do this. $\endgroup$
    – Szabolcs
    Sep 7, 2017 at 21:09
  • $\begingroup$ If you are asking how to create proper packages (not just re-use a few definitions), then it is a duplicate of (29324). $\endgroup$
    – Szabolcs
    Sep 7, 2017 at 21:13
  • 2
    $\begingroup$ The problem is that vectorDeriv is not mentioned between BeginPackage and Begin. See the post about creating packages I linked above (also this answer). $\endgroup$
    – Szabolcs
    Sep 7, 2017 at 21:22
  • 3
    $\begingroup$ Another problem will be: mathematica.stackexchange.com/q/114769/5478 $\endgroup$
    – Kuba
    Sep 7, 2017 at 21:28


Browse other questions tagged or ask your own question.