# What is the the proper way to define a function from an expression? [duplicate]

Say after some long computation we get an expression

expr=x^2


We do not know what the value of expr beforehand. Now we want to turn this in a function. We can use either

f[x_]=expr


or

f[x_]:=Evaluate[expr]


as suggested in this question.

However, when we do

f[x_]=Expand[expr]


or

f[x_]:=Evaluate[Expand[expr]]


The Expand will not have any effect.

Is there any way to make this work? Of course, we can define another function

g[x_]:=Expand[f[x]]


But is there any way to do it a bit more concisely?

Update:

If you do what suggested by Kuba in the comment, you get

In[89]:= With[{expr = expr}, f11[x_] := Expand[expr]]
In[90]:= f11[a + b]
Out[90]= x^2
In[91]:= ?? f11
Notebook34$907690f11 f11[x$_]:=Expand[x^2]

• I think f[x_] = Expand[expr] actually works fine. It expands expr and then turns x into a function slot. Just try it with expr = (1 + x)^10 and then evaluate f[y] after defining f. Or were you expecting something different? – Sjoerd Smit Dec 4 '18 at 15:47
• I was expecting to have f[a+b]==Expand[(a+b)^2]=a^2+2 a b+b^2 – ablmf Dec 4 '18 at 16:36
• In that case I would say that the g[x] := Expand[f[x]] method is really the way to go here, because it makes the evaluation process easiest to follow. Any other method is just going to be confusing one way or another. – Sjoerd Smit Dec 4 '18 at 17:02
• Closely related: (69590) – Mr.Wizard Nov 11 '19 at 14:27

Sorry, I was too hasty in comments:

With[{expr = expr}, SetDelayed @@ Hold[f[x_], Expand[expr]]]


SetDelayed @@ Hold is needed because of: Enforcing correct variable bindings and avoiding renamings for conflicting variables in nested scoping constructs

• why not With[{expr = Expand[expr]}, SetDelayed @@ Hold[f[x_], expr]] – Ali Hashmi Dec 4 '18 at 12:36
• @AliHashmi Because the point is not to evaluate Expand. Compare ?f in my and your case. – Kuba Dec 4 '18 at 12:37
• sorry i misinterpreted the question. I thought the purpose was for Evaluate to do its job before the definitions are saved. – Ali Hashmi Dec 4 '18 at 14:49
(f[x_] := Expand@#) &@expr

• FYI, I'd accept that. This is what I usually do anyway :p – Kuba Dec 5 '18 at 9:24

Here are a couple more possibilities. Using Block:

Clear[f]
Block[{Expand}, f[x_] = Expand[expr]];
Definition[f]


f[x_]=Expand[(1+x)^2]

Using With:

Clear[f]
With[{expr = expr, lhs = f[x_]},
lhs := Expand[expr]
];
Definition[f]


f[x_]:=Expand[(1+x)^2]

This works, and the general idea is useful for other things too,

ToExpression["f[x_]:= Expand[" <> ToString[InputForm[expr]] <> "]"]


To test it, let's try something not already expanded,

expr = (1 + x)^2


Using the above way to make f, one finds that Definition[f] // InputForm returns

f[x_] := Expand[(1 + x)^2]


as required. But we don't want to have to remember how to do all that, so let's just do it once for any f and any expr,

MakeFunction[f_, expr_]:=
ToExpression[ToString[f] <> "[x_]:=
Expand[" <> ToString[InputForm[expr]] <> "]"]


Next thing to do would be define Options for MakeFunction so that instead of just Expand, you could instead use Simplify, etc.

Dear @ablmf you can use the following syntax in Mathematica.

f[x_]:=x^2;


When you input any desired value for x, Mathematica gives you its value in f[x]. Say, you want f[2], Mathematica gives you 4

f[2]


4

You can also use it to obtain a list. Consider the following code

ClearAll["Global*"];
f[x_] := x^2;

Table[
f[x]
, {x, 0, 10}]


it gives you

{0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100}

• I'm sorry, but this isn't what OP asks for, is it? – xzczd Dec 4 '18 at 12:09
• Sorry, this is not what I asked. – ablmf Dec 4 '18 at 13:07