# Define a function f based on function g such that f doesn't change if g changes later

I would like to do something like

f[x_]:=g[x];


But I don't want that definition of f to change if the definition of g changes later.

How can I proceed?

• f[x_] =g[x] ? – LLlAMnYP Jul 13 '17 at 14:50
• oooh well. Sorry for my question it seems that I still don't know some basics of language :D. I thought we could only create functions with the := symbol – StarBucK Jul 13 '17 at 14:52
• @LLlAMnYP If g[x_?NumericQ] = x then f will change if g changes – Coolwater Jul 13 '17 at 14:52
• Not so basic, especially if x is allready defined and you don't want to use the definition of x. I think this latest problem has been treated in a post. – andre314 Jul 13 '17 at 16:42
• I can't find the post I mentioned just above. Among the possibilities one can 1) of course, change de variable name 2) use a formal parameter 3) use Block[{x},f[x_]:=g[x]], though I don't know if 3) is a good practise. – andre314 Jul 13 '17 at 16:53

If g[x] evaluates to what you need, you can define f with either = or :=:

f[x_] = g[x];
(f[x_] := #) &[g[x]]


If a single evaluation of g doesn't return all that matters, assign it to another symbol whose definitions are to be kept fixed:

  UpValues[g2] = UpValues[g]   /. g -> g2;    DownValues[g2] = DownValues[g]   /. g -> g2;
Attributes[g2] = Attributes[g] /. g -> g2;       Options[g2] = Options[g]      /. g -> g2;
SubValues[g2] = SubValues[g]  /. g -> g2;  FormatValues[g2] = FormatValues[g] /. g -> g2;


and replace g by g2 in your definition for f

Here's a simple idea of how you can explore the use of functions:

g[x_?NumericQ] := x
f[x_] := g[x]


Testing:

f[x]


g(x)

f


2

g[x]


g(x)

g


2