TIP: Click on subject to list as thread! ANSI
echo: quik_bas
to: All
from: Greg Easthom
date: 2004-03-15 16:23:04
subject: Code FAQ [3 /15

'>>> Page  3  of Code FAQ begins here.

   Any number of variables can be passed to a SUB:

   CALL NewSub(Name$, Text$, File$)
   NewSub Name$, Text$, File$

   However, the SUB statement you made at the very beginning
   must match *exactly* the TYPEs and number of variables in
   the calling statement:

   SUB NewSub(Name$, Text$, File$)
      PRINT Name$
      PRINT Text$
      PRINT File$
   END SUB

   Notice that if you had SUB NewSub(Name$, Text$, File%), QB
   would not be able to run your program, since the TYPE of the
   third variable wouldn't be the same in both places.  The SUB
   would be expecting an integer, but it would get a string.
   That isn't good.

   Those are the basics of SUBroutines.  FUNCTIONs are *exactly*
   the same as SUBs, with one major difference.  A FUNCTION
   is primarily designed to return a single value to the calling
   procedure, and therefore should be used differently.
   For instance:

   a! = GetANumber!
   PRINT GetANumber! * 4

   FUNCTION GetANumber!
      INPUT "Enter any number"; GetANumber!
   END FUNCTION

   This will ask for a number, ask for another number, and print
   the value of four times the second number.  Parameters (the
   variables which are passed to a SUB or FUNCTION inside the
   parentheses) work the same way here, too:

   PRINT NewName$("David")

   FUNCTION NewName$(OldName$)
      PRINT "The old name was "; OldName$; "."
      INPUT "Enter the new name:  ", NewName$
   END FUNCTION

   Notice that there is only one legal way to call a FUNCTION, and
   to use parameters when calling a FUNCTION.

   One more note:  once you save a file after creating one of
   the above examples, QB will put a DECLARE statement at the
   beginning of the program.  For instance:

   DECLARE SUB NewSub (Name$, Text$, File$)

   If you change the parameter list after this statement is
   created, you will have to edit this statement manually to match
   the new parameter list.

2A) Sub Tutorial - learning how to use SUBs

  Check out this tutorial by Kurt Kuzba. A little something to make SUBs
  easier to understand.

[quote]

DECLARE SUB MySub (p$)
DECLARE SUB MyShare ()
DECLARE SUB Fixed (p$)
DECLARE FUNCTION MyFunc$ (p$)
'_|_|_|   SUBTUTOR.BAS
'_|_|_|   A short tutorial on SUB and FUNCTION usage.
'_|_|_|   No warrantee or guarantee is given or implied.
'_|_|_|   Released   PUBLIC DOMAIN   by Kurt Kuzba.  (10/6/96)
COLOR 15, 1: CLS
PRINT " This is a short tutorial on use of SUB and FUNCTION in QBasic"
PRINT " and Quick Basic. It explores some of the relationships of"
PRINT " variables between modules. By studying the code and following"
PRINT " with the text, one may grasp module concepts."
PRINT " Variables in a SUB or FUNCTION, unless declared as STATIC,"
PRINT " are AUTOMATIC variables. BASIC initializes them every time"
PRINT " the module containing them is called. They may have the same"
PRINT " name as variables in other modules without confusion."
PRINT " SHARED and PASSED variables will be examined. Ready?"
DO: LOOP WHILE INKEY$ = "": CLS
PRINT " We will begin with the SUB. Variables may be passed by SEG or"
PRINT " or by VAL. Variables enclosed in parentheses are passed by VALue."

'>>> Page  3  of Code FAQ ends here. Continued in next message.

 * Brought to you by Greg's Little QBasic Auto-Poster *

--- Maximus 3.01
* Origin: The BandMaster, Vancouver, B.C., Canada (1:153/7715)
SEEN-BY: 633/267 270
@PATH: 153/7715 140/1 106/2000 633/267

SOURCE: echomail via fidonet.ozzmosis.com

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.