# nesting functions

I am trying to nest some functions together so that I have one final equation I can call (this is herons formula for triangles), which utilizes those previous functions I created. I've done this before but I'm having some trouble here. Here is what I have so far:

 sum[a_, b_, c_] := (a + b + c)/2;
sideA[b_, c_, s_] := 2 s - b - c;
sideB[a_, c_, s_] := 2 s - c - a;
sideC[a_, b_, s_] := 2 s - a - b;
area[a_, b_, c_, s_] := Sqrt[sum[a, b,
c]*(sum[a, b, c] - sideA[b, c, s])*(sum[a, b, c] -
sideB[a, c, s])*(sum[a, b, c] - sideC[a, b, s])];

N[area[2, 4, 4, 5]]

• Find different names for your functions and their arguments, this should fix your issues...
– ktm
Oct 18, 2016 at 20:14
• you mean for a, b and c?? I tried using sideA, sideB, etc and no luck. But I will mess around with it. Thanks @user6014 Oct 18, 2016 at 20:18
• Currently in your area function Mathematica thinks that the function names are the same variables as the function's input, i.e. 1,2,3,4, explaining the current output you get.
– ktm
Oct 18, 2016 at 20:20
• s, a, b, c are functions on the LHS of the equations. They are variables on the RHS. this is causing the problems. Oct 18, 2016 at 20:21
• @dan7geo I edited my code above, how does that look? Oct 18, 2016 at 20:24

I think this is what you want to do:

s[a_, b_, c_] := (a + b + c)/2;
area[a_, b_, c_] := Sqrt[s[a, b, c]*(s[a, b, c] - a)*(s[a, b, c] - b)*(s[a, b, c] - c)];

area[3, 4, 5]
(*6*)


Or all in one:

Clear[area]

area[a_, b_, c_] :=
With[{s = (a + b + c)/2},
Sqrt[ s*(s - a)*(s - b)*(s - c) ]
]


Or all in one line :)

area := Sqrt[Plus@##/2 Times @@ (Plus@##/2 - List@##)] &

• Oh....Yeah I thought I might need all those sideA, sideB, sideC equations because how would Mathematica know the formulas for those values based off of just the sum equation? I guess it solves them somewhere...Thank you Oct 18, 2016 at 20:31
• No upvotes/accept? :) Oct 18, 2016 at 20:36
• gotcha :P my fault! Oct 18, 2016 at 20:37
• and yeah I have no clue why I had all those functions because side A would just be the length of "a" itself...duh. silly misunderstanding Oct 18, 2016 at 20:37