SUB

From QB64 Wiki

Revision as of 21:19, 11 September 2011 by Clippy (Talk | contribs)
(diff) ← Older revision | Current revision (diff) | Newer revision → (diff)
Jump to: navigation, search

A SUB statement is placed as the first line of a SUB procedure when it is created.


Syntax:

SUB Procedure_name [(parameter, list)]


  • SUB procedures are programs within programs. Cannot have ON events, DATA or ON ERROR statements in them.
  • Qbasic's IDE can create the SUB and END SUB lines for you. Use the Make SUB option in the Edit Menu. A box will come up for you to enter a name for the SUB.
  • If there are no parameters passed or they are SHARED the parameters and parenthesis are not required in procedure.
  • Variable names within the procedure do not have to match the names used in the CALL, just the value types.
  • All variable values return to 0 or null strings when the procedure is exited unless STATIC is used.
  • Enter the SUB procedure's code between SUB and END SUB lines only.
  • Entries before SUB or after END SUB line are not permitted except for a DEF statement before the SUB line.
  • Qbasic's IDE may place a DEFINT, DEFSNG, DEFLNG, DEFDBL or DEFSTR statement before the SUB line if it is used in the main module. It may even be the wrong variable type needed. It can be removed if necessary.
  • SUB procedures can return multiple values unlike functions.
  • Procedure code can use linenumbers, linelabels, GOSUB and GOTO inside of the procedure only.
  • For quick exits use an IF statement with EXIT SUB.
  • SUB procedures can save program memory as all memory used in a SUB is released on procedure exit.
  • Once a SUB is created and called, the IDE will DECLARE it when the file is saved. Note: Will be placed at top of main module code.
  • An uncalled SUB will not be DECLAREd in Qbasic if it is not used. QB64 ignores DECLARE statements!
  • WARNING! Do not place DATA fields after SUB or FUNCTION procedures! QB64 will FAIL to compile properly!
Qbasic allowed programmers to add DATA fields anywhere because the IDE separated the main code from other procedures.


Example: Text PRINT screen centering using PEEK to find the SCREEN mode width. Call and SUB procedure code:

DEFINT A-Z SCREEN 13 Center 10, 15, "This text is centered." ' example module sub call END DEFINT A-Z ' only code allowed before SUB line is a DEF statement or a comment SUB Center (Tclr, Trow, Text$) DEF SEG = &H40 Columns = PEEK(&H4A) DEF SEG Middle = (Columns \ 2) + 1 ' reads any screen mode width Tcol = Middle - (LEN(Text$) \ 2) COLOR Tclr: LOCATE Trow, Tcol: PRINT Text$; ' end semicolon prevents screen roll END SUB

Explanation: The procedure centers text printed to the screen. The parameters are the text color, row and the text itself as a string or string variable. The maximum width of the screenmode is found and divided in half to find the center point. The text string's length is also divided in half and subtracted from the screen's center position. The procedure will also work when the WIDTH statement has been used. When adding variables to Text$ use the + concatenation operator. Not semicolons!


See also:



Navigation:
Go to Keyword Reference - Alphabetical
Go to Keyword Reference - By usage
Go to Main WIKI Page