# How to get function to 'expand' while inactivating inner functions

This is sort of a two question problem:

Question 1

So I have a function as follows

f[x_] := x + 2;


I want a way to have a statement such as:

f


to output as follows:

(* output: 1+2 *)


I thought the expression below would work:

Inactivate[f, Except[f]]


but it appears Inactivate doesn't work on the inner functions that make up the definition of f. Instead I get:

(* output: 3 *)


This makes sense to me, however is there a function/expression that can do what I'm trying to do? Call it:

ExpandFunction[f]
(* output: 1+2 *)


Question 2

In addition, to go a step further, is there a way of making it so that the symbols in the definition of f do not get replaced? That is to say:

y=1;
f[x_] := x + 2;
StopSubstitutionsButExpandFunction[f[y]]
(* output: y+1 *)


Ideally, then, if this is possible, we could have a recursive behavior where, say:

z=3;
f[x_]:=x+1;
g[y_]:=f[y]*2;
StopSubstitutionsButExpandFunction[g[z]]
(* output: f[z]*2 *)
StopSubstitutionsButExpandFunction[g[z],2]
(* output: (z+1)*2 *)


Again, my thought was that a HoldAll attribute would work, for example:

SetAttributes[f, HoldAll];
y = 1;
f[x_] := x + 2;
Inactivate[f[y], Except[f]]


Expected:

(* output: y+2 *)


Actual:

(* output: 3 *)

• At least for your first question, you can use the step function by @Mr.Wizard. – Carl Woll Oct 27 '20 at 15:12

f[x_] := x + HoldForm;

f


yields 1+2

f[z]


yields z+2

Further,

g[y_] := HoldForm[f[y]]*2;

g[z]


returns 2 f[z]

If you need then to calculate with these functions use ReleaseHold first.

Have fun!