Page 1 of 1 1
Topic Options
#206606 - 2013-01-29 10:59 AM Goto problem
Seppe Offline
Fresh Scripter

Registered: 2013-01-09
Posts: 7
Loc: Tenerife
Hi all,

I have a problem with the goto statement when used in a function that uses parameters.
It looks like the goto leaves the function instead of branching to the label.
Please examine the simple script below.
Thanks, Seppe

;----------
Break On ; avoid system restart
$a=0 ;
:A $a=$a+1 if $a<5 goto A EndIF ; loop 5 times
$b=Fun1(1) ; get it
Messagebox("a = $a, b = $b","Test") Exit ; show a and b
Function Fun1($a) ;
:B $a=$a+1 if $a<6 goto B EndIf ; loop 6 times
$F1=$a EndFunction ; return a value
;-------------

Top
#206607 - 2013-01-29 11:13 AM Re: Goto problem [Re: Seppe]
Arend_ Moderator Offline
MM club member
*****

Registered: 2005-01-17
Posts: 1896
Loc: Hilversum, The Netherlands
 Code:
Break On ; avoid system restart
$a=0
:A
$a=$a+1
if $a<5
  goto A
EndIF ; loop 5 times

$b=Fun1(1) ; get it
Messagebox("a = $a, b = $b","Test")
Exit ; show a and b

Function Fun1($a) ; 
  :B
  $a=$a+1
  if $a<6
     goto B
  EndIf ; loop 6 times
  $F1=$a
EndFunction ; return a value

Yeah Goto doesn't work within functions.
But it should not be used anyway, try to use loops instead:
 Code:
Break On ; avoid system restart
$a=0
while $a<5
  $a=$a+1
Loop ; loop 5 times

$b=Fun1(1) ; get it
Messagebox("a = $a, b = $b","Test")
Exit 0; show a and b

Function Fun1($a) ; 
  While $a<6
    $a=$a+1
  Loop ; loop 6 times
  $Fun1=$a
EndFunction ; return a value


Top
Page 1 of 1 1


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 1574 anonymous users online.
Newest Members
BeeEm, min_seow, Audio, Hoschi, Comet
17882 Registered Users

Generated in 0.175 seconds in which 0.16 seconds were spent on a total of 13 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org