2
$\begingroup$

questions are in the below code.

Although I'm pretty sure of the answer, I still feel a little uncomfortable, because I feel that the format of the body of Module[] is a bit "dead", seems that the Return statement had to be put at last line.

Is the value of Module[] is always equal to the last line which is not followed by semicolon;?

Is there a concept of "Return value of a function" including Module[] in Mathematica? and whenever you use the function in any expression, the function is always equal to the return value of the function?

i don't know how to ask, seems no questions here but a little uncomfortable for me(i'm not sure whether you can get the points of my uncomfortablement). Is there existing some reference about this?

f[x_,y_,z_]:=Module[{a,b,c},a+b;b+c;...;x+y+z]

(* is equal to the following c-function? *)

Head[x+y+z] f(Head[x] x ,Head[y] y, Head[z] z)
{ 
  Head[a] a; Head[b] b; Head[c] c;
  a+b; b+c; ....;
  return x+y+z;
}

(*------------------------------------------*)

f[x_,y_,z_]:=Module[{a,b,c},a+b;b+c;...;x+y+z;] (*in my test return Null*)

(* is equal to the following c-function? *)

void f(Head[x] x ,Head[y] y, Head[z] z)
{ 
  Head[a] a; Head[b] b; Head[c] c;
  a+b; b+c; ....;
  return ;
}
$\endgroup$
6
  • $\begingroup$ To a rough approximation, yes, your Mathematica Modules are equivalent to your C functions. But there is a lot of nuance that you should become familiar with. For example, semi-colons are not really line delimiters. The value of a Module is not "equal to the last line which is not followed by semicolon", but instead is equal to whatever the expression in its body evaluates to. reference.wolfram.com/language/tutorial/… reference.wolfram.com/language/tutorial/… $\endgroup$
    – lericr
    Jan 15, 2023 at 15:41
  • 1
    $\begingroup$ Are you looking for this?: mathematica.stackexchange.com/q/204614/1871 $\endgroup$
    – xzczd
    Jan 15, 2023 at 15:43
  • 1
    $\begingroup$ In the good old days, one requirement by a manager I had was that each function should have one COMMON exit at the end of the function. So if one needs to return early on, then they should set up the final value and do a GOTO to the common exit label. This was the only time we are allowed to use GOTO in the code anywhere, which is to jump to common exit. This worked well, since one has to look only at the common exit code in the function to see what is being returned from the function instead of having to look at all of the code to see where it returns.... $\endgroup$
    – Nasser
    Jan 15, 2023 at 20:25
  • 1
    $\begingroup$ ... Code was in PLI. return from middle of function was not allowed. Will be fired if found in code review. All programmers I knew liked this setup. But these days mood in programming world has changed and I do not see this any more. $\endgroup$
    – Nasser
    Jan 15, 2023 at 20:30
  • $\begingroup$ @Nasser Probably off-topic here, but doesn't that rule just shift the problem to the variable that's being returned? I.e. instead of needing to look for the return statements, you now have to look for all the places assigning values to the variable being returned? $\endgroup$
    – Lukas Lang
    Jan 15, 2023 at 20:55

2 Answers 2

6
$\begingroup$

Effectively, Module[{body}, expr] "returns" the value of expr after evaluating according to the rules described in the documentation of Module. And the value of a;b;c is the value of c after evaluation (see CompoundExpression). So yes, the "return" value of Module is simply the value of the expression after the last ; (or Null if the expression ends with a ;).

I put "return" in quotes because I don't think you should think about expressions/functions "returning" something in Mathematica. Rather, expressions evaluate to something. What they evaluate to is controlled by the rules assigned to the various symbols. Almost everything works this way, even control flow constructs such as If and Switch follow this logic:

So, if you want to have the equivalent of something like

int foo(int a, bool b)
{
  int res = 2;
  if (b)
    return res*a*a;
  else
    return res+a+a;
}

(i.e. with the return not necessarily on the last line), you could do

foo[a_, b_]:=
  Module[
    {res = 2},
    If[b,
      res*a*a,
      res+a+a
    ]
  ]

This works because the value of If[a,b,c] will be b if a is True, and c if a is False. If you don't need any local variables, it gets even simpler:

foo[a_, b_]:=
  If[b,
    2*a*a,
    2+a+a
  ]

(i.e. there is in general no need for Module around every function body)

$\endgroup$
2
  • $\begingroup$ after some learning, as a supplement to this sentence -- " So yes, the "return" value of Module is simply the value of the expression after the last ;" , another case is Hold[a;b;c;] // FullForm => CompoundExpression[a,b,c,Null],will return Null $\endgroup$ Jan 16, 2023 at 11:00
  • $\begingroup$ @Aerterliusi Good point, I added something to improve clarity $\endgroup$
    – Lukas Lang
    Jan 16, 2023 at 11:13
0
$\begingroup$

with the help of enthusiastic replyers and your links,my uncomforablement is gone.

about this uncomforablement,

First should know the function CompoundExpression[]. Main references are https://reference.wolfram.com/language/ref/CompoundExpression.html and https://mathematica.stackexchange.com/a/18617/89613

Secondly personally i realize that In fact, compound-expressions, which seem to be “dead” and inflexible, but considering that in fact, in general, “return value” is generally in the last position of a function, and the syntax design will not actually cause much inconvenience. That is, a less complex C function, I believe can always be translated into Mathematica statements in the form of compound-expressions with flow control statements such as If[]

Finally in Mathematica, it is probably better to adopt the idea of evaluating expr than the idea of ”return value“

$\endgroup$
1
  • $\begingroup$ As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center. $\endgroup$
    – Epideme
    Jan 17, 2023 at 16:06

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.