2
In[1] Goto["place2"]; Label["place1"]; Return[10]; Label["place2"]; Return[20];

Out[1] Return[20]

In[2] f[x_] := (Goto[x]; Label["place1"]; Return[10]; Label["place2"]; Return[20];)

In[3] f["place2"]

Out[3] 20

Q1) Can you tell me why Out[1] is Return[20], not 20 ?

Q2) How to modify In[1] as slightly as possible, so that Out[1] becomes 20 ?

imida k
  • 4,285
  • 9
  • 17

2 Answers2

6

First, why on earth do you want to use Goto??

But, if you must, the problem you're encountering is that Return typically returns from a control structure (e.g., If), and your example has none. When there is no control structure, the Return wrapper doesn't get stripped. To fix things, you can use the undocumented 2-arg form of Return (specifying CompoundExpression in this case, becase that is the FullForm of a; b; c):

Goto["place2"]; Label["place1"]; Return[10]; Label["place2"]; Return[20,CompoundExpression];

20

Carl Woll
  • 130,679
  • 6
  • 243
  • 355
0

I think in mathematica,
when dealing with Goto command,
to make any final output on the screen,
following 4 things are necessary.

  1. create a final variable
  2. create a way-out
  3. assign a value for the final variable
  4. go to the way-out

In the below example, 1),2),3),4) are EndValue,"End",Endvalue=10 or 20,Goto["End"], respectively.

Goto["place1"]; Label["place1"]; EndValue = 10; Goto["End"];Label["place2"]; EndValue = 20; Goto["End"]; Label["End"]; EndValue

Also there is other method like using Return[something, CompoundExpression] as Carol answered.

imida k
  • 4,285
  • 9
  • 17