Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

This question already has an answer here:

I want a function that works like in the following example: it gets an expression which contains a variable var and evaluate it setting setting var = value.

I used Block to avoid expr being evaluated if variable var holds a value in the notebook. It seems that my function works correctly - I made also a trace - but I get var inside block coloured red and I think it's a warning from Mathematica. Can you explain to me why I obtain it? Is my code wrong? Is there a better way to obtain the desired behavior?

SetAttributes[myFunc, HoldFirst];

myFunc[{expr_, var_}, value_] :=
  Block[{var}, expr /. var :> value]


share|improve this question

marked as duplicate by rm -rf Apr 5 '13 at 13:38

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

There is nothing to worry about in this particular case. The only thing I would change here for some extra check is the function's signature, to be myFunc[{expr_, var_Symbol}, value_]. –  Leonid Shifrin Apr 5 '13 at 10:37
added picture for posterity - delete if you don't want it! –  cormullion Apr 5 '13 at 10:41
Thanks for your replies. With the picture the question is more clear –  Betelgeuse Apr 6 '13 at 5:44

1 Answer 1

The coloring is there to tell you of a possible conflict. Your function will be replacing var in both the Block and RuleDelayed with whatever you use as the second term of the first argument. It's possible that this is exactly what you intend, but it is usually not, hence the highlighting to warn.

If you give an example of the use of your function I can more specifically tell you how to handle it.

share|improve this answer
Thanksks for replying Mr Wizard. I thi nk in my case I just can ignore the warning because the behaviour I obtain is what I want –  Betelgeuse Apr 6 '13 at 5:52

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