5

I am trying to understand two of the answers to the question 114769 Package functions and symbolic calculations

    ClearAll["MyP`*"];

BeginPackage["MyP`"]; Global`b; MyLagrange;

Begin["Private"];

MyLagrange[f_, var_] := Module[{a}, a = D[f, var] + b; a];

End[]; EndPackage[];

b = 17; MyLagrange[Sin[x1], x1]

returns

MyP`Private`b + Cos[x1]

so the Global statement has been ignored

florin
  • 1,798
  • 7
  • 12

1 Answers1

7

You could explicitly use Global`b in the definition of MyLagrange:

ClearAll["MyP`*"];

BeginPackage["MyP`"]; MyLagrange;

Begin["Private"];

MyLagrange[f_, var_] := Module[{a}, a = D[f, var] + Global`b; a];

End[]; EndPackage[];

b = 17; MyLagrange[Sin[x1], x1] (* 17 + Cos[x1] *)

Chris K
  • 20,207
  • 3
  • 39
  • 74