# functions with mutiple variables and lines of calculations

I am almost a new user of Mathematica, my question is that how can I have something like a function or exactly a function that for example gets two inputs (a) and (b), then calculates a+b and a*b. In fact something that gets multiple inputs and does multiple calculations or tasks or in other words function with multiple variables and multiple tasks or lines of calculations.

In my Program I have these initial values : Pr, Mr, Er,r then I want to calculate the lines below :

While[Pr > 0,

f1 = -(1.47*Mr*
Er*(1 + Pr/Er)*(1 + 4*\[Pi]*r^3*Pr*.08969/Mr))/(r^2*(1 -
2*Mr*1.47/r));
g1 = 4*\[Pi]*r^2*Er*.08969;
k1 = h*f1;
m1 = h*g1;

f2 = -(1.47*Mr*
Er*(1 + (Pr + k1/2)/Er)*(1 +
4*\[Pi]*(r + h/2)^3*(Pr + k1/2)*.08969/Mr))/((r + h/2)^2*(1 -
2*Mr*1.47/(r + h/2)));
g2 = 4*\[Pi]*(r + h/2)^2*(Er + m1/2)*.08969;
k2 = h*f2;
m2 = h*g2;
f3 = -(1.47*Mr*
Er*(1 + (Pr + k2/2)/Er)*(1 +
4*\[Pi]*(r + h/2)^3*(Pr + k2/2)*.08969/Mr))/((r + h/2)^2*(1 -
2*Mr*1.47/(r + h/2)));
g3 = 4*\[Pi]*(r + h/2)^2*(Er + m2/2)*.08969;
k3 = h*f3;
m3 = h*g3;

f4 = -(1.47*Mr*
Er*(1 + (Pr + k3)/Er)*(1 +
4*\[Pi]*(r + h)^3*(Pr + k3)*.08969/Mr))/((r + h)^2*(1 -
2*Mr*1.47/(r + h)));
g4 = 4*\[Pi]*(r + h)^2*(Er + m3)*.08969;
k4 = h*f4;
m4 = h*g4;

Pr = Pr + 1/6*(k1 + 2*k2 + 2*k3 + k4);
Mr = Mr + 1/6*(m1 + 2*m2 + 2*m3 + m4);
Er = fromPressureToEnergy[Pr];
r = r + h;
]

 The results are the last Mr and r when the loop is finished.


Could you please help me that how i can write a function for this code and calculations?

• There are also Module, Block, With. – Artes Aug 28 at 10:09
• I said Module. Read documentation pages. – Artes Aug 28 at 10:19
• Pleas read through an introductory tutorial (like this one) in full. The one I linked is less than 30 pages, covers all essentials, and comes with exercises. – Szabolcs Aug 28 at 11:57
• It looks like Runge-Kutta 4 method. Notice that mathematica has all possible numerical methods for the solution of differential equations. Check NDSolve. Also try to avoid having all these numbers like .08969 around-- easy to mistype. Introduce a constant. Use functions for the parts of code that are used repeatedly. These simple rules apply to any other programming language, not only mathematica. Try to be efficient in programming. – yarchik Aug 28 at 13:51
• does the syntax f[Pr0_, Mr0_, Er0_, r0_] := Module[{Pr=Pr0,Mr=Mr0,Er=Er0,r=r0,<any-other-variables-you-want-to-be-local> },<your-code-as-is>; {Mr,r}], as implicitly suggested, not work for what you're trying to do? or was it simply difficult to parse that that was what was being suggested, given your newness to Mathematica? Welcome, by the way! :) – thorimur Aug 28 at 21:45