# creating a stacked function that returns the multi varibles

I'm a beginner in Mathematica with little programming background but I'm determined to learn this.

I'm trying to make a function that calculates two variables r2 & ψ. from my search I should use Module inside the function to achieve that. Yet I can't get it to return the variables correctly... can you tell me what am I doing wrong?

calc[r1_, r3_, r4_, ϕ_, ψ_] :=
Module[{r2_},
r2 = N[√((r3 Cos[ϕ Degree] -
r1 Cos[ψ Degree])^2 + (r3 Sin[ϕ Degree] -
r1 Sin[ψ Degree])^2)]; {r2};
Module[{a, b, ob},
a = AngleVector[{r1, ϕ Degree}];
b = AngleVector[{r3, ψ Degree}];
ob = AngleVector[{-r4, 0}]; {a, b, ob}
]
]


Phi & Psi are both greek symbols for angles.

1. How to control which variable the module returns?
2. Am I including two modules correctly in terms of syntax in the function?
3. How to control which variable the big function returns?
• For one thing, get rid of the underbar in Module[{r2_},....
– ciao
Aug 16, 2015 at 6:42
• @ciao YUP that made the whole thing works after removing the semicolon too. THANKS! Aug 16, 2015 at 7:27

Everything in Mathematica has a precedence, including semicolon! That is similar to 3+4*5 meaning the multiplication is done before the addition. So the way you have it written above says the definition of your calc function ends with your first semicolon and then your second Module will calculate a result without waiting for you to later use your calc function.

Try writing really really simple Modules that just print which module just executed and see if you can figure out how your code would run.

If you were to write calc[r1_, etc]:= (Module[stuff];Module[morestuff]) then the () changes the order of operations. So when you use calc it will do the first module, throw away the result, do the second module and return that as the value of your calc function.

So what can you do? Well you could do this

calc[r1_, r3_, r4_, ϕ_, ψ_] :=
{ N[√((r3 Cos[ϕ Degree] - r1 Cos[ψ Degree])^2 +
(r3 Sin[ϕ Degree] - r1 Sin[ψ Degree])^2)],
{AngleVector[{r1, ϕ Degree}],
AngleVector[{r3, ψ Degree}],
AngleVector[{-r4, 0}]
}
};


That will calculate both results, put them in a list and return that as the value of your calc function. Then you could use calc[stuff][[1]] to extract the first value or calc[stuff][[2]] to extract the second list of three values.

Or you could add an extra final argument to calc, which would be either True or False, and use an If function inside the definition of calc to decide which calculation to do.

Notice that I didn't even use Module in this. That was because you didn't have enough reason to have "local variables", etc. and just the expressions you wanted were able to be used on their own. In other words I tried to simplify your code down to just the bare amount needed.

Hopefully this gives you some ideas to think about and perhaps you can progress to the next level of understanding.

• Thanks so much for the quick response. So you basically used this syntax calc[] := {operation1, {operation2}} which corresponds to the format of the results I got too. So in general it's like this: F[]:= {op1,op2,{op31,op32}} Am I correct? Aug 16, 2015 at 7:19
• Correct. I modified your calc function to return a list of both results. Then your code using calc could extract the desired value from that list.
– Bill
Aug 16, 2015 at 7:26
• Thanks Bill! I appreciate it... Aug 16, 2015 at 7:30
• One more question, If I want to use the on of those results in the same function can I do it this way? or should I use another way? Aug 16, 2015 at 7:44
• Maybe a spelling error in that last comment? I don't understand. Or maybe you put back in your Module which can let you use previous results while still inside the Module. Please correct or explain if I have misunderstood.
– Bill
Aug 16, 2015 at 7:48