3
$\begingroup$

I created a Mathematica packagr and then I used the Needs function to add the library to my .nb file, but when use a function in my .nb file and the function is built in my library it comes up with the functions output multiplied by "mylibrarynamePrivate".

My Package:

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

Output:

Afak10`Private`s + x^2 

What if I define f[x] as:

f[x_] := Module[{asdfs},asdfs]; 

The Output is:

Afak10`Private`asdfs$4980 
$\endgroup$
9
  • 2
    $\begingroup$ You need to give a more concrete example of your functions and how you are using them. I can think of a few different possible causes for this but I don't feel like explaining problems that don't exist. $\endgroup$ Commented Jan 25, 2014 at 9:34
  • $\begingroup$ Well its a big package with many functions, some of them are fine and others aren't $\endgroup$ Commented Jan 25, 2014 at 16:02
  • 1
    $\begingroup$ If you want help, then find a simple case: make up a small package that has the same problem. Then we can try and help. $\endgroup$ Commented Jan 25, 2014 at 16:21
  • $\begingroup$ Take this library for example: $\endgroup$ Commented Jan 25, 2014 at 22:00
  • $\begingroup$ BeginPackage["Afak10"] Unprotect @@ Names["Afak10*"]; ClearAll @@ Names["Afak10*"]; f::usage = "f[x]" Begin["Private"] f[x_] := Module[{}, x^2 + s]; End[] Protect @@ Names["Afak10*"]; EndPackage[] $\endgroup$ Commented Jan 25, 2014 at 22:01

1 Answer 1

1
$\begingroup$

In your code you have a Symbol s that is not declared before the line Begin line. Because of this it is created in that specified Afak10`Private` context. (Also, your code presently has an error; it needs to be Begin["Private`"].) You can include the Global` context if that is in fact the Symbol you wish to return:

f[x_] := Module[{}, x^2 + Global`s]; (* as part of your package *) 

Now using the function:

f[z] 
s + z^2 

Nevertheless I caution you about this method as you generally should not use (plain) global Symbols like this; instead use Formal Symbols which are Protected to prevent accidental assignment.

f[x_] := Module[{}, x^2 + \[FormalS]]; 

It looks like this:

enter image description here

$\endgroup$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.