qbasicqb64

QBasic: how to run a program from within another program?


I have made two different programs in QBasic and they both are saved in different .bas files, i.e one is 1.bas and the other 2.bas.

How to open program 1.bas while I am in program 2.bas, without closing it?

Program 1 should run inside program 2 for some time, and when it ends I should again be in program 2. Is there any way to do that?

I would like to know if there is a syntax for this that works in QBasic and/or QB64.


Solution

  • In Qbasic you can use CHAIN command to pass control to another .BAS file and when it is finished it will return to the first .BAS file. You can combine it with COMMON to also share variables between the two programs.

    You could also use RUN but in QBasic you can't pass variables (not sure but I think the control will not return). And in QB64 it is possible to pass variables using RUN

    See the standard COM1_EX.BAS and COM2_EX.BAS as an example, contents of COM1_EX.BAS:

    ' == COM1_EX.BAS - COMMON statement programming example ==
    DIM Values(1 TO 50)
    COMMON Values(), NumValues
    
    PRINT "Enter values one per line. Type 'END' to quit."
    NumValues = 0
    DO
       INPUT "-> ", N$
       IF I >= 50 OR UCASE$(N$) = "END" THEN EXIT DO
       NumValues = NumValues + 1
       Values(NumValues) = VAL(N$)
    LOOP
    PRINT "Leaving COM1_EX.BAS to chain to COM2_EX.BAS"
    PRINT "Press any key to chain... "
    DO WHILE INKEY$ = ""
    LOOP
    
    CHAIN "com2_ex"
    

    contents of COM2_EX.BAS:

    ' == COM2_EX.BAS - COMMON statement programming example ==
    ' Notice that the variables Values() and NumValues from COM1_EX
    ' will be called X() and N here in COM2_EX
    DIM X(1 TO 50)
    COMMON X(), N
    
    PRINT
    PRINT "Now executing file com2_ex.bas, reached through a CHAIN command"
    IF N > 0 THEN
       Sum = 0
       FOR I = 1 TO N
          Sum = Sum + X(I)
       NEXT I
       PRINT "The average of the values is"; Sum / N
    END IF