Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Create a procedure
#1
When I type

GOSUB prindol
or
CALL prindol

Am I giving QBASIC a reference for when I type in the subroutine or procedure? I thought typing in CALL prindol would create a procedure that I could go into but it doesn't.
Reply
#2
Here's an example of using subs:

Code:
DECLARE SUB Hello ()

Call Hello 'The "Call" isn't necessary


SUB Hello ()

'Content of the Sub Hello
PRINT "Hello"

END SUB
B 4 EVER
Reply
#3
If you type CALL subroutine (variable1, variable2) etc, you are specifying when in your program you want the sub to be called. To actually edit the sub, you type (on a new line):

SUB subroutine (variable1, variable2)

QBasic will sense that you made a sub, and make a new window with the words END SUB at the bottom of the sub. You put your code between the SUB and END SUB lines.

Finally, you have to tell the compiler that the sub exists. At the top of your program, before any executable lines (PRINT, DIM etc), you write:

DECLARE SUB subroutine (variable1, variable2)

Or you can save you prog and the program will automatically insert the correct line for you.
Reply
#4
Thanks alot. Is it the same with GOSUB?


*edit* Well I tried GOSUB (subroutine) and it says 'label not defined' when I try to run it. But it works fine if I just put:

(subroutine name)
Reply
#5
GOSUB works similar to GOTO, just jumps to a label, and when it finds the RETURN keyword it returns to the line after the last GOSUB was called (so you can nest GOSUB calls without problems):

Code:
PRINT "Yabba"
   GOSUB MySubroutine
   PRINT "Doo"
   END

MySubroutine:
   PRINT "Dabba"
   RETURN

SUBs work in a different way. They are independent pieces of code (so independent that you can also put them in another files) that you can also pass parameters too. In most cases, they are more handy than GOSUBs:

Code:
PRINT "Yabba"
CALL Shout("Dabba")
PRINT "Doo"

SUB Shout(whatToShout$)
   PRINT whatToShout$
END SUB

The CALL statement is no longer needed since QB 4.0, so you can do:

Code:
PRINT "Yabba"
Shout "Dabba"
PRINT "Doo"

SUB Shout(whatToShout$)
   PRINT whatToShout$
END SUB

More details in QB help.
SCUMM (the band) on Myspace!
ComputerEmuzone Games Studio
underBASIC, homegrown musicians
[img]http://www.ojodepez-fanzine.net/almacen/yoghourtslover.png[/i
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)