# InputField - use later as a function

This is my attempt at trying to write code to do the following. I want the user to Input the function - I then want to create a new function called g[x_] that I can use later to do things like evaluate g[6] or Solve equations etc...

ClearAll[f, g]
LocalizeVariables -> False;
Interpretation[
Panel[
Grid[
{
{"Function f(x):", InputField[Dynamic[f]], g[x_] := Function[f][x]}
}
]
],
]
Dynamic[g[x]]


But nothing happens - it seems g[x] does not contain the input function.

I'm going to guess this is what you want:

ClearAll[f, g]
Panel[Grid[{{"Function f(x):", InputField[Dynamic[f, (g[x_] = f = #) &]]}}]]
Dynamic[g[x]]


Or maybe this, but it requires entering your function in terms of Slot[1] and not x:

ClearAll[f, g]
Panel[Grid[{{"Function f(x):", InputField[Dynamic[f]],
g[x_] := Function[Evaluate@f][x]}}]]
Dynamic[g[x]]


So probably the first one is closer to what you want. But be careful to never assign x a numeric value. If you do, x will not work as a variable.

If you want a more bullet-proof version, here's a modification:

Panel[Grid[{{"Function f(x):",
InputField[Dynamic[f, Block[{x}, g[x_] = ReleaseHold[f = #]] &],
Hold[Expression]]}}]]


Block[{x},...] temporarily clears an values x has while the definition of g is created. The type argument Hold[Expression] keeps x from being evaluated inside the input field, so that it will behave as a variable.

• The third code Panel[Grid[{{"Funct....... did exactly what I want, I can now get on with the rest of my code. Thank you Michael E2 for taking the time to do this , I literally spent hours trying to figure out what was needed and now I am happy :-) – Donald Robertson May 7 '17 at 21:47