It is possible to express the "same" function as a function of different variables in Mathematica using the following construct:

v[t_, "t"] := v0 - g t
v[y_, "y"] := Sqrt[v0^2 - 2 g (y - y0)]

Where the first is $v$ as a function of $t$ and the second is $v$ as a function of $y$.

Plugging in $t=y=0$ is done as follows:

v[0, "t"] 
v[0, "y"]

Which yields the following (expected) result:

Sqrt[v0^2 + 2 g y0]

My question is simple. Nowhere in the Mathematica documentation can I find this extra "t" or "y" string argument that goes into the v[] function. I would like to understand what this argument is, why it exists and what are its limitations. The only thing I found is this, which does address additional arguments but comes at them from the point of view of optional variables. What I am doing is different. I am using the same letter to functions of two different variables, where each variable is required (not optional).


closed as off-topic by MarcoB, corey979, Feyre, Simon Rochester, Sascha Jan 13 '17 at 14:16

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – MarcoB, corey979, Feyre, Simon Rochester, Sascha
If this question can be reworded to fit the rules in the help center, please edit the question.

  • $\begingroup$ tutorial/MakingDefinitionsForFunctions, take a look there, left hand side of := is a pattern, v[x_, "t"] matches v[...] if the second argument is exactly "t". $\endgroup$ – Kuba Jan 12 '17 at 10:59
  • $\begingroup$ You're focusing too much on the fact that there's a string in the function. Consider a discrete f defined as f[1]:=2; f[2]:=4. This means that when the argument of f is 1 it should return 2, and when it's 2, the output is 4. You could also write f[a]:=6 in the same manner. In your v, the second argument is such a 1/2/a: if it's "t" it is to take the corresponding definition, if it's "y" it is to take the other one. If you type, e.g., "z", you'll get nothing as this symbol is not a marker of any function. $\endgroup$ – corey979 Jan 12 '17 at 11:01

The 2nd argument of the function v is of a kind called a discriminator or a tag. Such arguments are used to discriminate between usages of an overloaded identifier, and are seen fairly often in programming languages that permit identifier overloading.

You should not call such arguments "optional". They are not optional, but required. Mathematica does support optional arguments, but their syntax and semantics are quite different.


Not the answer you're looking for? Browse other questions tagged or ask your own question.