# Why does the same function defined by Set and SetDelayed look different? [duplicate]

f[n_] = Sum[(1+x)^j, {j,1,n}];
g[n_] := Sum[(1+x)^j, {j,1,n}]

When I try to evaluate the code above I get:

f[2]

$$\dfrac{(1+x)(-1+(1+x)^2)}{x}$$

g[2]

$$1+x+(1+x)^2$$

Why is this the case?

• This is because in the definition with Set the expression is evaluated first. Mathematica can symbolically manipulate Sums even when n does not have a value. To see this, you can evaluate the expression Sum[(1 + x)^j, {j, 1, n}] when n does not have a value, and see that it evaluates to the same thing as you can see in f//Definition. Nov 15 '14 at 11:28
• Also compare f[2]//Expand and g[2]//Expand. Lastly I think the question looks quite alright :). Nov 15 '14 at 11:39
• Yeap, I got it. Thank you Nov 15 '14 at 12:00
• Hi, welcome to Mathematica.SE, please consider taking the tour so you learn the basic rules of the site. Your question may be put on-hold because it seems to be off-topic and a duplicate. Please don't be discouraged by that cleaning-up policy. Your questions are and will be most welcomed. Learn about good questions here. Nov 15 '14 at 12:28