Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm wondering how to do the following:

    expr = L + R;

    foo[L_, R_] := expr;

    foo[1, 2]

(* L + R *)

and have it substitute the function variables into the expression. I realize that I could create dummy variables and do it like

foo[l_, r_] := expr /. L->l /. R->r

but my actual expression has a lot of free variables, and so I would like to know if there is a simpler way.

share|improve this question
Check out Evaluate. – chuy Jan 21 '14 at 17:45
foo[L_,R_]=expr; The := is delayed evaluation which is usually correct but not in this precise case. – Ymareth Jan 21 '14 at 18:30
Use = instead of :=. It's not necessary to Evaluate IMO because it would be completely equivalent to just using =. I'd consider this question a duplicate of What is the difference between Set and SetDelayed?. – Szabolcs Jan 21 '14 at 19:30
up vote 1 down vote accepted

As noted in the comments, use Set (=) instead of SetDelayed (:=) while making sure that L and R have no value assigned:

expr = L+R
foo[L_, R_] = expr

foo[1, 2]
(* ==> 3 *)
share|improve this answer
I went ahead and changed this, since it was community wiki. I think Evaluate would be completely equivalent to just using Set here. Hope you don't mind, please check the edit. – Szabolcs Jan 21 '14 at 19:32
@Szabolcs Sure, even if these were not CW I welcome good edits:) – Ajasja Jan 21 '14 at 20:13

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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