Re: Re[4]: Return from nested subroutines (still goto discussion)
- Posted by Tone Škoda <tskoda at email.si> Nov 13, 2004
- 468 views
here is one trick i just thought of, but i think it's not too good coding style: btw i don't think "if then" takes so much time. integer G procedure r () ... if r(x+1, i) then G = 3 return end if ... end procedure procedure main (0 G = 1 while 1 do if G >= 3 then exit end if r () G += 1 end while end procedure () "while" isn't slower than "if", is it? unknown wrote: > > Ya, this is what I mean by decreasing performance. Having to check > return value for each iteration (even when the return value is 0) > would consume maybe about 10% of the time. > > How about using threads? I never program in threads, but is it > possible to run r() in seperate thread then kill the thread > immediately when the "success" is printed. > > Kat wrote: > G> Jumping into/out of proceedures has never been on my wish list, quite the > G> contrary. > > GOTO never been necessary for me except for exiting the stack :P > -- without decreasing performance > > > G> Does this work? I got x = 5 and y = 3. The trick is to use functions > G> and pass down a 'success' value, like here I use 1 for success, 0 for > G> failure. Whenever there is a success (or 1) I exit the current loop > G> and/or return. I've actually gotten used to this style of coding and I > G> prefer it over goto. This just seems more logical that jumping around > G> code all willy-nilly. > > G> }}} <eucode> > G> sequence d > G> d = {0,0,0,0,0} > > G> function sum( sequence x ) > G> -- calculate the sum of > G> -- all values in x > G> atom val > G> val = 0 > G> for i = 1 to length(x) do > G> val += x[i] > G> end for > G> return val > G> end function > > G> function r(integer x, integer y) > G> d[x] = y > G> if x = 5 then > G> if sum(d) = 10 then > G> printf(1, "x = %d\ny = %d\n", {x,y}) > G> puts(1, "success\n") > G> return 1 > G> end if > G> else > G> for i = 0 to 3 do > G> if r(x+1, i) then > G> return 1 > G> end if > G> end for > G> end if > > G> return 0 > G> end function > > G> procedure main() > G> for i = 0 to 3 do > G> if r(1, i) then > G> exit > G> end if > G> end for > > G> puts(1, "program end\n") > G> end procedure > > G> main() > G> </eucode> {{{ > >