Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

going back to the main prog. from sub

Status
Not open for further replies.

SEWill

Technical User
Apr 30, 2003
1
0
0
US
I'm trying to finish this project and don't want to press F5 each time to run my program after completing a sub. Is there anything I can put at the end of my sub-program to [b}run[/b} my main program to get me back to the menu?

pls. help this newbie.
 
Check the help topics on [tt]CALL[/tt] and [tt]GOSUB[/tt].

vcn.gif

Suffice it to say that adding disk drives and a disk operating system to a personal microcomputer is guaranteed to increase its power dramatically.
CP/M and the Personal Computer
 
MAKE YOU MAIN LOOP IN A SUB...

THEN PUT THE EXECUTION IN A LOOP CONTROLLED BY A GLOBAL (SHARED) VARIABLE....

For Example...

Code:
DIM SHARED ENDGAME AS INTEGER

INTRO
DO
  MAIN
  PLAYAGAIN
LOOP UNTIL ENDGAME
OUTTRO

SUB INTRO
  ... CODE TO RUN 1 TIME DURING START UP ...
END SUB

SUB MAIN
 ...CODE HERE...
END SUB

SUB PLAYAGAIN
  ... BLA BLA ...
  IF ... THEN ENDGAME = 1
END SUB

SUB OUTTRO
  ... CODE TO RUN WHEN GAME ENDS ...
END SUB

*This method IS NOT limited to games...

Good Luck...

Have Fun, Be Young... Code BASIC
-Josh Stribling
cubee101.gif

 
SEWill, I think what you need to see is that [tt]SUB[/tt]s automatically return to the place from which they were called, as soon as QB reaches the end of the subroutine. Here is a simple example:
[tt]
DECLARE SUB test ()

PRINT "a"
test
PRINT "c"

SUB test
PRINT "b"
END SUB
[/tt]
The output of this program is:
[tt]
a
b
c
[/tt]
You can see how the [tt]PRINT[/tt] statetement after the call to [tt]test[/tt] in the main module is executed as soon as [tt]test[/tt] finishes. If you place a loop in the main module, then you can automatically jump back up to the top and redo the menu. Here is a sketch of that:
[tt]
DECLARE SUB menuItem1 ()
DECLARE SUB menuItem2 ()

DO
PRINT "1. Activate menu item 1"
PRINT "2. Activate menu item 2"
PRINT "3. Quit"
PRINT
DO
PRINT
"Enter selection: ";
LINE INPUT selectionString$
selection% = INT(selectionString$)
LOOP UNTIL (selection% >= 1) AND (selection% <= 3)

SELECT CASE selection%
CASE 1: menuItem1 ' invoke subroutine
CASE 2: menuItem2
CASE 3: EXIT DO ' exit the loop
END SELECT
LOOP

PRINT
&quot;Program exiting&quot;

SUB menuItem1
PRINT &quot;this is code that handles the first menu item&quot;
END SUB

SUB
menuItem2
PRINT &quot;this is code that handles the second menu item&quot;
END SUB
[/tt]
If you do not understand parts of this source code, I recommend you use the help file. For instance, to get help about [tt]SELECT CASE[/tt], you can place the cursor anywhere on the words [tt]SELECT[/tt] and [tt]CASE[/tt] and then press F1. The help file is well-written and informative.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top