Author Topic: $INCLUDE  (Read 3789 times)

0 Members and 1 Guest are viewing this topic.

Offline Dimster

  • Forum Resident
  • Posts: 500
    • View Profile
$INCLUDE
« on: September 01, 2019, 04:06:40 pm »
The metacommand $INCLUDE basically permit the main program to call another program and run the code in the 2nd program to the end before returning to the main program which called it. In making multiple calls to the 2nd program, is there a way of having a common variable that can be changed during the course of running the main program?

' Jumping between Programs using Include
'COMMON SHARED count
'DIM SHARED count
PRINT
PRINT "This is the main program"
PRINT "Jumping to the 2nd program'"
SLEEP
'$Include:'Program 2.bas'
PRINT
PRINT "Now back in the main program"
PRINT
PRINT "Going Back to the 2nd program"
'$include:'Program 2.bas'
PRINT
IF count = 3 THEN
    PRINT "Count is 3 - Back in main program for the 3rd time"
    count = 4
END IF
PRINT "Now back in the main program for the 2nd time"


'This is Program 2 for the Include test
'
'COMMON SHARED count
'DIM SHARED count
count = count + 1
IF count = 3 THEN
    PRINT "Now in Program 2 for 3rd time"
    PRINT "Expect to Exit Program 2 now"
    SLEEP
    END
END IF

PRINT "Now in Program 2"
PRINT "This is the "; count; " number of times visiting Program 2"
SLEEP



In Program 2 the value of count is running. Back in the main program count is altered but that count value is not passed back to Program 2.

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: $INCLUDE
« Reply #1 on: September 01, 2019, 06:27:45 pm »
Hi Dimster

I need to ask you some questions....
1.  why don't you use the code box to type/paste the code?
2. why do you REM the declaration of variable in both programs ? IMHO you can save the DIM  SHARED in the first program...

about
Quote
The metacommand $INCLUDE basically permit the main program to call another program and run the code in the 2nd program to the end before returning to the main program which called it.
I don't agree if you don't agree that in the second program (file of code) there is only a block of code of the main (, or if you like this other version) i.e. the second program.BAS is only a main block of code.
In fact if you run this version of you code you get error.
program1 TDB.bas
Code: QB64: [Select]
  1. REM Jumping between Programs using $Include
  2. 'COMMON SHARED count
  3. 'DIM SHARED count
  4. PRINT "This is the main program"
  5. PRINT "Jumping to the 2nd program'"
  6. '$Include:'program2 TDB.bas'
  7. PRINT "Now back in the main program"
  8. PRINT "Going Back to the 2nd program"
  9. '$include:'program2 TDB.bas'
  10. IF count = 3 THEN
  11.     PRINT "Count is 3 - Back in main program for the 3rd time"
  12.     count = 4
  13. PRINT "Now back in the main program for the 2nd time"
  14.  

and program2 TDB.bas
Code: QB64: [Select]
  1. 'This is Program 2 for the Include test
  2. '
  3. 'COMMON SHARED count
  4. 'DIM SHARED count
  5. count = Increasing(c) REM  count + 1
  6. IF count = 3 THEN
  7.     PRINT "Now in Program 2 for 3rd time"
  8.     PRINT "Expect to Exit Program 2 now"
  9.     SLEEP
  10.     END
  11.  
  12. PRINT "Now in Program 2"
  13. PRINT "This is the "; count; " number of times visiting Program 2"
  14.  
  15. FUNCTION Increasing (c)
  16.     c = c + 1
  17.  
  18.  

and you cannot compile nor run it.

IMHO this question is interestin
Quote
n making multiple calls to the 2nd program, is there a way of having a common variable that can be changed during the course of running the main program?

changing the words but not the matter
Quote
a variable declared into the main of program and used in a block of main written in another file does it loose its content/value?

and the answer seems to be YES because the parser before compiling (or checking the syntax) joins the code into a linear code file.

see here
save this as program1TDB.bas
Code: QB64: [Select]
  1. REM Jumping between Programs using $Include
  2. 'COMMON SHARED count
  3. 'DIM SHARED count
  4. PRINT "This is the main program"
  5. PRINT "Jumping to the 2nd program'"
  6. PRINT "press a key to continue"
  7. PRINT " count = "; count
  8. '$Include:'program2TDB.bas'
  9. PRINT "Now back in the main program"
  10. PRINT "Count = "; count
  11. PRINT "Going Back to the 2nd program"
  12. '$include:'program2TDB.bas'
  13. PRINT "Now back in the main program for the 2nd time"
  14. PRINT "count= "; count
  15. '$include:'program2TDB.bas'
  16. IF count = 3 THEN
  17.     PRINT "Count is 3 - Back in main program for the 3rd time"
  18.     count = 4
  19.  

and this as program2TDB.bas
Code: QB64: [Select]
  1. 'This is Program 2 for the Include test
  2. '
  3. 'COMMON SHARED count
  4. 'DIM SHARED count
  5. PRINT "Now in Program 2"
  6. PRINT "count ="; count
  7. count = count + 1
  8. IF count = 3 THEN
  9.     PRINT "Now in Program 2 for 3rd time"
  10.     PRINT "Expect to Exit Program 2 now"
  11.     PRINT " Press a key to continue"
  12.     SLEEP
  13.     END
  14.  
  15. PRINT " After increasing count"
  16. PRINT "This is the "; count; " number of times visiting Program 2"
  17. PRINT " Press a key to continue"
  18.  

In sum
if you use an $Include:' ' file  into your main file.bas for goint into it many times    you are using this metacommand$ like a GOSUB /RETURN but thje lenght of the code is growing up!

Good Coding

Programming isn't difficult, only it's  consuming time and coffee

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: $INCLUDE
« Reply #2 on: September 01, 2019, 07:00:03 pm »
You guys seem to be overthinking what $INCLUDE is/does.  It’s *NOT* a program you run while inside a second program — it’s nothing more than a means to insert a block of set text into your program.

One file, save as File1.bas:
Code: [Select]
count = count + 1
PRINT count

A second file; save as File2.bas:
Code: [Select]
’$INCLUDE:’File1.bas’
‘$INCLUDE:’File1.bas’
‘$INCLUDE:’File1.bas’

Compile and run File2 and you’ll see:
1
2
3

All QB64 does, when it sees that $INCLUDE statement, is insert the text you specify into your program at the point you specify.  File2.bas would basically be nothing more than:
Code: [Select]
count = count + 1
PRINT count
count = count + 1
PRINT count
count = count + 1
PRINT count

So, your original code is basically:
Code: [Select]
DIM SHARED count
PRINT
PRINT "This is the main program"
PRINT "Jumping to the 2nd program'"
SLEEP

'
'COMMON SHARED count
'DIM SHARED count
count = count + 1
IF count = 3 THEN
    PRINT "Now in Program 2 for 3rd time"
    PRINT "Expect to Exit Program 2 now"
    SLEEP
    END
END IF

PRINT "Now in Program 2"
PRINT "This is the "; count; " number of times visiting Program 2"
SLEEP

PRINT
PRINT "Now back in the main program"
PRINT
PRINT "Going Back to the 2nd program"
'
'COMMON SHARED count
'DIM SHARED count
count = count + 1
IF count = 3 THEN
    PRINT "Now in Program 2 for 3rd time"
    PRINT "Expect to Exit Program 2 now"
    SLEEP
    END
END IF

PRINT "Now in Program 2"
PRINT "This is the "; count; " number of times visiting Program 2"
SLEEP

PRINT
IF count = 3 THEN
    PRINT "Count is 3 - Back in main program for the 3rd time"
    count = 4
END IF
PRINT "Now back in the main program for the 2nd time"


It’s not one program calling another.  It’s just inserting the text of one into the other.  You don’t need shared variables, or anything such as that to share values, no more than you’d need them if you just copy/pasted the code directly into your source.
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline Dimster

  • Forum Resident
  • Posts: 500
    • View Profile
Re: $INCLUDE
« Reply #3 on: September 02, 2019, 12:05:11 pm »
Hello TempodiBasic and Steve

TempodiBasic, to answer your questions, I wasn't sure how to use the "code box" and the example of the problem were 2 short coding programs, and to your second question I REM'd them because neither one of those worked for me. I did try the Dim Shared in program 1 and program 2. And I agree with you, I am using $Include as a quasi sub routine.

I also agree with Steve, $Include is a means of inserting a block of set text. In this case I'm inserting the same block multiple times. What I was trying to do was to find a way to Exit the $Included block of text early. The only thing that came to mind was a Common variable that would trigger the Included block to end earlier rather than running the code to its end. I did expect, when the main and Included block of coding ran, that there was an automatic sharing of variables and I could avoid the issue of passing values as happens with Subs.

In my example programs, although both the main and the included have the same COUNT variable, the IF statement triggering the premature end of the Included file doesn't work. Or at least hasn't been working for me. I gather then that the coding in an $INCLUDE must run it's course unlike a Sub/Function which you can Exit early.

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: $INCLUDE
« Reply #4 on: September 02, 2019, 12:54:35 pm »
In this case I'm inserting the same block multiple times. What I was trying to do was to find a way to Exit the $Included block of text early. The only thing that came to mind was a Common variable that would trigger the Included block to end earlier rather than running the code to its end. I did expect, when the main and Included block of coding ran, that there was an automatic sharing of variables and I could avoid the issue of passing values as happens with Subs.

In my example programs, although both the main and the included have the same COUNT variable, the IF statement triggering the premature end of the Included file doesn't work. Or at least hasn't been working for me. I gather then that the coding in an $INCLUDE must run it's course unlike a Sub/Function which you can Exit early.

Just like any other set of code, you can exit it pretty much any time you want to, as long as you code for that possibility.  Here's a quick example which seems to do what you're talking of:

First, inc1.bas:
Code: QB64: [Select]
  1. IF ExitSet = 0 THEN
  2.     FOR i = i TO i + 4
  3.         PRINT i
  4.         IF i = 10 THEN ExitSet = -1: EXIT FOR
  5.     NEXT

And then, our main program to compile and run:
Code: QB64: [Select]
  1. PRINT "With each include of inc1.bas, we're going to increment a counter 5 times"
  2. PRINT ", starting from 0."
  3. PRINT "Here's one call."
  4. '$include:'inc1.bas'
  5. PRINT "Here's the second call."
  6. '$include:'inc1.bas'
  7. PRINT "Here's the third call."
  8. '$include:'inc1.bas'
  9. PRINT "Here's the last call."
  10. '$include:'inc1.bas'
  11. PRINT "Notice how our exit variable keep us from executing code inside the include?"
  12. PRINT "On the third call, we exited early when i was 10."
  13. PRINT "And on the last call, we excluded the include completely."
  14.  

I don't think it's a style of coding which I'd personally use, but if it's what you need to make your programs work for you, it *is* possible to do.  ;)
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline Dimster

  • Forum Resident
  • Posts: 500
    • View Profile
Re: $INCLUDE
« Reply #5 on: September 02, 2019, 03:51:18 pm »
Thanks Steve. A For loop/Exit For does seem to be the best bet.