2
$\begingroup$

I have an exercise class where I need to understand a specific function.

without posting the whole big code into here I'd like to understand the basic

funct[a_,b_,c_] := Module[{p,k,etest,crit}, .....  ];

Inside are additionally some while loops etc. but as for now I'd like to know why there is a 'Module' in front. Maybe also important is the fact that inside the curly brackets {p,k,etest,crit} is pretty much every single variable which occurs in the function in these while loops.

$\endgroup$
1
$\begingroup$

Module[{x,y,z}, ...] treats whatever you define in the curly brackets as local variables. That is, you can think of them as being available only for computations carried out within the Module[]. When it's finished, they are no longer available in memory.

Also see the documentation.

$\endgroup$
  • $\begingroup$ Yes thanks now I understand it! $\endgroup$ – Benjamin Jabl Jan 8 '19 at 18:37
3
$\begingroup$

You need to look up module and to try a few examples. A module makes the variables in the curly brackets local to the module and does not change variables outside the module that happen to have the same name.

Try this. First I define a function that contains the local variables a and b. Then I define global values for a and b, use the function and look at the values before and after the use of the function.

f[x_] := Module[{a, b}, a = 3; b = 2; a + b x]

a = 5; b = 10;
{a, b, f[4], a, b}

{5, 10, 11, 5, 10}

So you can see that the values of a and b that were global were not used in the module and were not effected by the local variables a and b within the module.

$\endgroup$
  • $\begingroup$ Perfect your example made it clear what you meant! Thanks $\endgroup$ – Benjamin Jabl Jan 8 '19 at 18:37

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