I have a few students who are having trouble re-defining a function.
For instance, if you were to input:
then later on, you want to re-use f as a function name:
If I go to plot f:
it correctly plots the "new" function (1/x), but if I try to evaluate a limit, such as:
it outputs 0, so it is obviously using the FIRST definition of the function (x^2). Why is it being selective about when to use the first definition of f and when to use the second definition? This happened to only 3 of my 15 students. Any ideas or suggestions?
I assume that you cannot replicate this behavior but only that it has been reported by the students themselves. If so then it's likely that they are (a) typing something in differently than what you wrote or (b) executing things in a different order (e.g., re-executing the first definition after the plot is created. or (c) something else slightly more subtle.
Here is an example of (c): writing the =definitions of f differently insofar as their two argument patterns are concerned:
f[x_] := x^2
f[x__] := 1/x
This would cause the behavior that is observed. If your students encounter the problem (whatever it is) have them execute
to see if there is more than one definition in f's rule list.
Thanks for the quick reply. I did in fact observe it myself on the students' worksheets, but I cannot replicate it myself. I will try to get their worksheets and use the ?f command to see what is happening. Thank you so much!!
It is interesting that in the documentation center for defining a function, Mathematica gives this warning:
"When you have finished with a particular function, it is always a good idea to clear definitions you have made for it. If you do not do this, then you will run into trouble if you try to use the same function for a different purpose later in your Mathematica session. You can clear all definitions you have made for a function or symbol f by using Clear[f]. "
Maybe they acknowledge this issue can occur?
It is generally a good practice to use ClearAll (or Clear, but ClearAll does more work). The documentation is observing that if one makes multiple definitions for a function which has different patterns in its argument definition, then all will be saved in that function's DownValues (i.e., the list of rules that Mathematica goes through to see which one it should use when you call the function in a particular form). Mathematica will save all definitions for the function, but will generally (but not always) replace one of a specific pattern if a new version with that specific pattern is evaluated as a new definition for it. Mathematica attempts to order definitions for a function in the order from more specific to more general, though sometimes that it is not possible to make that determination. So if you define
then when you execute g you get "cheese" and not 4.
In the odd example I gave previously I gave different definitions for f[x] and f[x] which are different patterns, so both are saved since x and x__ are different patterns.
But, to come back to the beginning.... it's always good practice to use Clear or ClearAll when you want to redefine a function from scratch.