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.

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

1 Answer 1


In a module, the local-variable declarations aren't executed one after the other, but rather independently. If you want to execute code sequentially, put it inside the module, not in the variable declaration:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.