Help with subs

 BBS: Inland Empire Archive
Date: 04-03-92 (23:35)             Number: 106
From: STEVE HALKO                  Refer#: NONE
  To: ZACK JONES                    Recvd: NO  
Subj: Help with subs                 Conf: (2) Quik_Bas
ZJ>'with the program.  If I'm wrong, please point it out.  I'm trying to
ZJ>'pass the value of Source$ from the SUB back to the main program.
ZJ>'Everytime I run this the print Source$ prints nothing.

ZJ>DECLARE SUB GetFile (Source$) 'declare the subprogram
ZJ>COMMON SHARED Source$ 'Make Source$ avail to all subs
ZJ>CLS
ZJ>DEFINT A-Z
ZJ>Source$ = RTRIM$(LTRIM$(COMMAND$)) 'gets rid of any spaces
ZJ>IF Source$ = "" THEN               'if no parameters are present
ZJ>   GetFile (Source$)               'call subprogram
ZJ>END IF
ZJ>PRINT Source$                      'after sub is done print contents
ZJ>                                         'return to main program.

ZJ>                                   'of Source$
ZJ>SUB GetFile (Source$)              'I've tried running it like this
ZJ>DO WHILE Source$ = ""              'and with the STATIC keyword.
ZJ>LOCATE 5, 2: PRINT "Name of file to get?"
ZJ>LOCATE 5, 23: PRINT "               "
ZJ>LOCATE 5, 23: INPUT "", Source$          'Get Source$
ZJ>LOOP
ZJ>LOCATE 6, 23: PRINT Source$              'Print value just to show what's
ZJ>END SUB                                  'there

  First of all, if you're going to pass Source$ as a parameter, you
  don't need to make it SHARED unless there are other SUBs which also
  need it.  And if you're going to make it SHARED, then you don't need
  to pass it as a parameter.  It won't do any harm either way, though.

  Also, if you want to make it shared, you only need COMMON SHARED to
  share it with other modules.  If you only have one module like you
  posted here, just use DIM SHARED Source$.

  Anyway, the problem is in the way you're calling GetFile.
  GetFile (Source$) means that you're passing Source$ by value.  That
  means that your SUB gets a copy of the variable Source$.  So GetFile
  can use it, but it can't change the original Source$ from your main
  code.  You need to pass it by reference like this:

          CALL GetFile (Source$)

                 or

          GetFile Source$     <-------- without parentheses

  To summarize:

    To pass a parameter by value -      CALL MySub ((Parameter))
                                                or
                                        MySub (Parameter)

    To pass a parameter by refernce -   CALL MySub (Parameter)
                                                or
                                        MySub Parameter

 * SLMR 2.1a * DMD $ W(   .

--- DB B1063/002487
 * Origin: Gulf Coast BBS -QuickSHARE #2- (904)563-2547 HST/V.32bis (1:365/12)
Outer Court
Echo Basic Postings

Books at Amazon:

Back to BASIC: The History, Corruption, and Future of the Language

Hackers: Heroes of the Computer Revolution (including Tiny BASIC)

Go to: The Story of the Math Majors, Bridge Players, Engineers, Chess Wizards, Scientists and Iconoclasts who were the Hero Programmers of the Software Revolution

The Advent of the Algorithm: The Idea that Rules the World

Moths in the Machine: The Power and Perils of Programming

Mastering Visual Basic .NET