# function evaluation - I don't get it

this function returns Pi/4 for a[1,4]

In[...]:= a[r_, n_] :=
Module[{an = Pi/n, h = r Cos[an], w = 2 r Sin[an]}, an];
a[1, 4]

Out[...]= Pi/4


so the kernel knows that an evaluates to Pi/4

This function returns something that seems inconsistent with how it worked before

In[...]:= a[r_, n_] :=
Module[{an = Pi/n, h = r Cos[an], w = 2 r Sin[an]}, n/2 h w];
a[1, 4]

Out[...]= 4 Cos[an] Sin[an]


I get that Mathematica evaluation can seem non-intuitive but how this is as simple as it can get - comparing the 2 functions and their behaviors seems to show inconsistency: it knows that an=Pi/4, yet when an is used in simple algebraic expressions, an is returned unevaluated.

I must be missing something, but so far it eludes me.

EDIT: after following user kglr's recommendation of reading the Trace I got the idea of moving the defn. of an outside of Module - that "fixes" things and gives me an actual answer but I am not yet quite sure why.

• inspect Trace[a[1, 4]] to see how an is processed. – kglr Aug 11 at 16:41

a[r_, n_] := Module[{an, h, w},