Page 1 of 1

Runtime error from same-line commands?

Posted: 12 October 2009, 17:02 PM
by pjc30943
My impression was that two or more lines of code can be inserted on the same line, like so:

Code: Select all

CodeA : CodeB : CodeC 
However, when CodeA is a subroutine, it fails to execute, and CodeB is the first thing run. Here's an example, where x and a are bytes, and subA is a short subroutine.

Code: Select all

subA : x = a 
This does NOT work, as subA is never called, but 'x' is correctly set to 'a'. But this configuration does work, of course:

Code: Select all

subA 
x = a
Thoughts? This is merely a readability issue, but if no error is thrown then it seems that all commands should run in both configurations.

Posted: 12 October 2009, 17:15 PM
by GTBecker
SubA: is interpreted as a label?

Posted: 12 October 2009, 18:49 PM
by dkinzer
GTBecker wrote:SubA: is interpreted as a label?
Quite so. An identifier at the beginning of a line followed by a colon is taken to be a label. You can work around this restriction in one of two ways:

Code: Select all

Call SubA() : x = a
: SubA : x = a

Posted: 12 October 2009, 18:56 PM
by pjc30943
Ah...of course. Thanks.
I guess labels are supported for compatibility with assembly.

Posted: 12 October 2009, 19:13 PM
by mikep
pjc30943 wrote:I guess labels are supported for compatibility with assembly.
No not really. ZBasic is based on BasicX which in turn is based on VBasic. Just out of interest here is the VBasic documentation for labels and gotos.

Posted: 12 October 2009, 19:21 PM
by dkinzer
pjc30943 wrote:I guess labels are supported for compatibility with assembly.
No. Their purpose is to be the target of a GOTO instruction. A GOTO is rarely necessary but occasionally it can be used to good effect when the alternative is even less palatable.

Posted: 12 October 2009, 19:41 PM
by dkinzer
mikep wrote:Just out of interest here is the VBasic documentation for labels and gotos.
Although valid, the example is somewhat contrived. The Exit Sub could have been used directly instead of using the goto/label.

Code: Select all

Private Sub ValidateValue(ByVal intValue As Integer)
  If intValue > 10 Then 
    Exit Sub
  Else
    ProcessValue(intValue)
    MessageBox.Show("Valid Number Entered")
  EndIf
End Sub
On the other hand, the Exit Sub statement and related Exit statements serve exactly the same purpose as a goto/label combination. It's just that the target is implicit instead of being an explicit label.

Posted: 13 October 2009, 11:16 AM
by pjc30943
Woops...I didn't realize ZBasic supported goto, but I should have since it's vb based (and looking at it now, there it is in the docs). Hence the assumption that labels was for use with asm GOTOs. Thanks for the clarifications.