# Solution of a function that is in a variable [duplicate]

If I want to get the solution for a simple function, e.g. a^x, I can use:

a = 2;
Function[x, a^x][2]


However, if I have the function in a variable, I can't get it to work:

a = 2;
func = a^x
Function[x, func][2]


Similarly, this doesn't work as wished, which is that the last line provides the result if 2 is put into x:

a = 2;
func = a^x;
der = D[func, x]
Function[x, der][2]

• Function is HoldAll so Function[x, #][2] &[func], let me find a duplicate. – Kuba Dec 4 '15 at 10:10
• duplicates?: 14686, 22033 related: 10067, 40445, 31985 – Kuba Dec 4 '15 at 10:17
• Intersting. But this doesn't work: # &[2] &[func] – Mockup Dungeon Dec 4 '15 at 10:30
• Because it isn't what I've suggested ;) – Kuba Dec 4 '15 at 10:31
• @Kuba Sure! You didn't suggest it, I was just wondering whether this works as well. – Mockup Dungeon Dec 4 '15 at 11:38

Using Evaluate,

a = 2;
func = a^x;
Function[x, func][2]
Function[x, Evaluate@func][2]
(* 2^x *)
(* 4 *)


and

a = 2;
func = a^x;
der = D[func, x];
Function[x, der][2]
Function[x, Evaluate@der][2]
(* 2^x Log[2] *)
(* 4 Log[2] *)

• Thanks. Interestingly, it seems the following does not work: Evaluate[#] &[2] – Mockup Dungeon Dec 4 '15 at 10:34