Group Abstract Group Abstract

Message Boards Message Boards

0
|
4.6K Views
|
6 Replies
|
0 Total Likes
View groups...
Share
Share this post:

How to stop overwriting the previous function/expression with the next one?

Posted 2 years ago

Hello,

Is it possible to be stopped overwriting the previous function/expression with the next one if they have the same name?

Let's say that we have a longer math file and we define a new function with the same name as a previous function, and the result is that the new function will overwrite the previous/old function.

The idea is that the new function defined with the same name does not overwrite the old function. The action of the new function to begin from its definition down, and not up. Is it possible this? (I do not know, by setting an option in the settings or something like that?)

enter image description here enter image description here enter image description here

f[x_] := x
Plot[f[x], {x, 0, 10}]
f[2]
f[x_] := x^2

Thank you.

POSTED BY: Cornel B.
6 Replies
Posted 2 years ago

Expanding on Eric's hint about contexts: Under the Evaluation menu one can set the notebook to have separate contexts for each cell group.

enter image description here

Example notebook below. Each f[x_] is localized to its respective section. Changing the definition of f in section 2 will not affect the definition in section 1, and vice verse,

POSTED BY: Hans Milton
Posted 2 years ago

I would be curious why you need this, and maybe understanding your use case would help us make more effective suggestions. But in the meantime, the basic answer is that there is no way to set up spacial sections of your notebook as scoping constructs for symbol definitions. There are other things you can do, however, to avoid name clashes. Probably the easiest thing to do is use Block. You might also want to try using contexts. Maybe parameterizing the functions would get you something close to what you want.

POSTED BY: Eric Rimbey
Posted 2 years ago

Why would someone want that? Because if you have a larger mathematica file and you inadvertently define the same function somehow below the first function, then the first function is overwritten by the second function, that is if you do not initialize the first function again (pressing enter+shift again on the first function) past calculations/plots will be based now on the new function defined after the old function. I see that the option is to have different names for the functions, such as: f1[x], f2[x], etc, so that you don't have to worry about writing Clear[f] and then initializing from the previous new function. It would have been good if the first function with the same name was not overwritten by the 2nd function with the same name, and the effect of the 2nd function starting from the definition of the 2nd function down and having no effect on what is defined above.

POSTED BY: Cornel B.
Posted 2 years ago

Okay, if it's as simple as just wanting to avoid inadvertantly overwriting function defintions, I suggest that you Protect your functions after you define them.

POSTED BY: Eric Rimbey

Write ClearAll[f] before assigning a new definition should work.

POSTED BY: Sander Huisman
Posted 2 years ago

I see that it works also with Clear[f] You can look at the sequence number that I followed for In[1], In[2], etc - this was the running sequence.

So, the user must be careful to delete the definition of the new function and then to initialize the old function to be able to use the old function. And if he wants to use the new function again, he has to initialize the new function again... I thought there was a setting that would automatically take this into account.

enter image description here enter image description here enter image description here

POSTED BY: Cornel B.
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard