トピック
受付中
Talakori Laubwerk

Subroutines and conditional branching

First thing first: Can i call multiple sub-routines in succession? E.g. in sub-routine @X i call sub-routine @Y in which i then call sub-routine @Z. The upcomming RETURN will return to @Y, the next RETURN returns to @X, and the next RETURN returns to the inital caller? Second: How to execute multiple commands after THEN or ELSE (aside from GOTO)?
2そうだね
プレイ済み
返信[1]
親投稿
Talakori Laubwerk
Third: Will RETURN do anything if i go to a @Label with GOTO (so i can use @Labels with GOTO and GOSUB as fit)? Fourth: Any general advice for how to write multi condition heavy and branch heavy code?
0そうだね
プレイ済み
返信[2]
親投稿
Hanzo rzsense
First : Yes. Return point will be stacked up on a stack memory when a GOSUB is called, and the stacked pointer will be erased when a RETURN is called. Second : Write as follows. IF A THEN B:C:D ELSE E:F:G or IF A THEN B:C:D ELSE E:F:G ENDIF Third : No. It will cause "Stack Overflow" or "RETURN without GOSUB" errors.
0そうだね
未プレイ
返信[3]
親投稿
Hanzo rzsense
Fourth : I recommend you to indent as follows. IF A THEN IF B THEN C D ELSE IF E THEN F ENDIF ENDIF ELSE G ENDIF
0そうだね
未プレイ
返信[4]
親投稿
Talakori Laubwerk
@Hanzo: Thanks that's very helpful. If i understand your explanation to "Fourth" correctly, then C and D are executed IF B. So i deduce the following are synonymic. IF A THEN B:C IF A THEN B C ENDIF Are the following possible? 1) IF A AND B THEN C 2) IF A OR B THEN C 3) IF A AND (B OR C) THEN D 4) IF (A AND B) OR C THEN D
0そうだね
プレイ済み
返信[5]
親投稿
Nicole moesaku
Those two are the same, yes. And yes, those are possible, but you should use && and || instead of AND and OR. AND/OR are for bit operations.
1そうだね
未プレイ
返信[6]
親投稿
Nicole moesaku
By the way, I would recommend making functions with DEF, instead of making subroutines with labels and GOSUB. GOSUB is an older way of doing things, and doesn't let you give parameters like DEF does. 'User command which returns nothing DEF FOO X PRINT "X = ";X END 'User function which returns a value DEF SQUARE(X) RETURN X * X END FOO 100 PRINT SQUARE(10)
1そうだね
未プレイ
返信[7]
親投稿
Talakori Laubwerk
@Nicole: Thanks for the recommendation. I just started SmileBasic so the number of "Instructions" i can make use of (or even know of) is still pretty limited. I had to read a bit further into "DEF" to understand its purpose, but it seems fairly useful. Though i have to evaluate rewriting my current code or keeping it as is. Might not be worth the hassle, as it serves its purpose.
1そうだね
プレイ済み
返信[8]
親投稿
Talakori Laubwerk
Writing IF A == B || C THEN is apparently wrong no matter what, but writing IF A == B || A == C THEN results in an ugly inconceivably wall of text due to a rather long array name of A. Is there a way to keep it tidy aside from shortening variable names further?
0そうだね
プレイ済み
返信[9]
親投稿
Nicole moesaku
As a workaround, it's possible to do this: DIM A[0] A=ARRAY_WITH_LONG_NAME This is actually not slow like you might think, because with arrays and strings, doing this doesn't copy the whole thing. Rather, A and ARRAY_WITH_LONG_NAME actually both point to the same data, so modifying one modifies the other too. (To make an actual separate copy, you'd do COPY A,ARRAY_WITH_LONG_NAME instead.)
0そうだね
未プレイ