# Scope of dummy variables used in defining a function

I define a function and then define another function, using the same name for the dummy variables as I used for the first function. Will Mathematica use the first definition in the second definition? I hope not. The code should make my question a little clearer:

s[t_] := Integrate[Sqrt[Sinh[t]^2 + Cosh[t]^2 + 1], t]
t[s_] := ArcSinh[s/Sqrt]


I don't want the s in the second line of code to be identified with the s defined in the first line of code. Obviously, I could just use a different name, but I want to understand the scope of what I naively assume are dummy variables.

• How do I take the tour? – Gene Naden Jul 18 '18 at 19:37
• OK I went through and did the upvoting. It was not clear whether I can vote on questions that were put on hold or closed. As for the link, the color is very subtle on my machine. I will keep an eye out for the color. – Gene Naden Jul 18 '18 at 19:48
• – JungHwan Min Jul 18 '18 at 19:56
• It is preferable to have minimal examples. Functions like Sinh in you code are distracting from the actual content of your question. – Hector Jul 18 '18 at 20:01

First try a smaller example.

s[t_] := Integrate[Sin[t], t];
t[s_] := s + 1;
{s[x], t}
(*{-Cos[x], 4}*)


It yields your expected result. On the other hand,

w = 1; Integrate[Sin[w], w]
(*Integrate::ilim: Invalid integration variable or limit(s) in 1.*)


does produce an error. So we have two seemingly contradictory pieces of information. The second example does show that Mathematica does not create dummy variables for integration as it does for for sums:

i = 1; Sum[i, {i, 4}]
(*10*)


Why did the small example work? Because t[s_] := … does not assign values to t but values to t[s_]:

OwnValues[t]
DownValues[t]
(*
{}
{HoldPattern[t[s_]] :> s + 1}
*)


Compare to

OwnValues[w]
DownValues[w]
(*
{HoldPattern[w] :> 1}
{}
*)


Reading the documentation of Integrate, the only hint at Integrate not creating dummy variables is the sentence

The integration variable can be a construct such as x[i], or any expression whose head is not a mathematical function.

Compare that to the documentation of Sum where it is explicitly stated that

The iteration variable i is treated as local, effectively using Block.

Finally, notice that you can always "test" whether dummies were created by assigning a value to the would-be dummy (the way w and i were assigned above). In that sense, the u in the pattern u_ below is a dummy:

u = 1;
f[u_] := u;
f
(*3*)

• I get that function definition uses dummy variables and Integrate does not, but I do not understand the example of OwnValues and DownValues. I guess every symbol has rules attached to it, right? – Gene Naden Jul 18 '18 at 20:06
• The comment by JungHwan Min has a link to an extensive discussion: What is the distinction between DownValues, UpValues, SubValues, and OwnValues?. A very short summary would be: t[s_]:= … assigns the right hand side to the expression t[s_] but the bookkeeping is done under t. On the other hand, t= … assigns the right hand side to the expression t and the bookkeeping is done under tas well. – Hector Jul 18 '18 at 20:22
• A "function definition" does not use "dummy variables". s_ is a named pattern. SetDelayed (:=) makes a definition that replaces instances of names on the right hand sides with named patterns on the left hand side. This is profoundly different from the "dummy variables" of other programming languages, although it bears a superficial resemblance. – John Doty Jul 19 '18 at 0:05
• @JohnDoty I did not write that u_ was a variable. I wrote " the u in the pattern u_ below is a dummy". I agree with you that u_ is a named pattern. However, I quote the documentation: "there is nothing special about the name x that appears in the x_ pattern. It is just a symbol, indistinguishable from an x that appears in any other expression." – Hector Jul 19 '18 at 6:00