# Returning Sin[t] from package turns into Sin[scopet] [duplicate]

I have a function in scope that returns an expression with a variable t

BeginPackage["Pkg"]
F1::usage = "";
Begin["scope"]
F1[x_] := Sin[t + x];
End[]
EndPackage[]


When I use it from outside

In[159]:= F1[2]
Out[159]= Sin[2 + scopet]


I can Replace scopettot outside

In[169]:= F1[2] /. scopet -> t
Out[169]= Sin[2 + t]


But is there any way to replace it from the inside of Package ?

I have tried replacing t with "t"

BeginPackage["Pkg"]
F1::usage = "";
Begin["scope"]
F1[x_] := Sin[t + x] /. t -> "t";
End[]
EndPackage[]


This looks like it works

In[223]:= F1[2]
Out[223]= Sin[2 + t]


However it is actually Sin[2 + "t"] So I cannot plot it with variable t

Plot[F1[2], {t, 0, 200}]


doesn't plot anything

• You need to define t as a public variable as well as F1, with a usage message in the same way. – SPPearce Nov 11 '16 at 13:02
• @KraZug You should write your comment up as an answer, it seems like the most elegant solution & helped me out with a similar problem. – Chris K Nov 11 '16 at 19:45
• @Chris The reason I didn't was because I assumed that the question would get closed. – SPPearce Nov 11 '16 at 19:51

t needs to be an argument of your function F1 like so:

BeginPackage["Pkg"]
F1::usage = "";
Begin["scope"]
F1[x_, t_] := Sin[t + x];
End[]
EndPackage[]


The calling it:

F1[x, t]
(*Sin[t + x]*)

• Is there any way to force t to be a variable not a value so that it doesn't conflict with a global variable t declared in user's notebook ? – Neel Basu Nov 12 '16 at 18:57
• @NeelBasu, I don't understand what you are looking for. Can you show (code) what is conflicting in this case? – user21 Nov 12 '16 at 19:37
• If I have t=42 in my Notebook prior invoking F1[x, t] it will use that t and return Sin[x + 42]. So is there any way such that user is forced to pass a variable only. or the symbol is used instead of its value even if a predefined variable is passed. – Neel Basu Nov 13 '16 at 13:41
• Use something like 't_Symbol' in the definition of F1. Have look in the documentation under pattern matching. – user21 Nov 13 '16 at 20:51

Simply add a usage definition the variable t as well as F1 (and in general, anything you want to return from your package)

BeginPackage["Pkg"]
F1::usage = "";
t::usage = "time variable";
Begin["scope"]
F1[x_] := Sin[t + x];
End[]
EndPackage[]


Then the t that is returned works fine.

• If the user code uses a variable t it use that instead – Neel Basu Nov 12 '16 at 18:50
• This code does not work when there is a global definition for t. E.g. try t=1 and then run your code. – user21 Nov 12 '16 at 19:35