05-20-2024, 08:07 AM
It's a bit trick to work out the logic, so here it is:
There are probably a limited number of situations where CallInclude makes more sense than just copying the subroutine into your code.
When the include subroutine is called it knows nothing about the rest of the include program code, as if we just copied the subroutine into the main program.
First create and compile the include program with a subroutine we want to use.
There is a 3 step process when CallInclude is run, assuming all files exist compiled etc.
1] All calling program variable values (that have the same name in both programs) are copied to the include program.
2] The include subroutine is called.
3] All include variables (some may now be modified by the subroutine) are copied back to the main program.
Consider include program (Include.sb):
And calling program:
Step 1] In included code : x set to 2, z set to 2, y is unset (default 0) - remember the CallInclude only looks at the subroutine MySub, it knows nothing about previous code that sets y = 1 for example.
Step 2] MySub sets z = 2+2+0 = 4
Step 3] In main program : x set to 2, z set to 4
Resulting output:
Include1
SUCCESS
x=2
z=4
Press any key to continue...
There are probably a limited number of situations where CallInclude makes more sense than just copying the subroutine into your code.
When the include subroutine is called it knows nothing about the rest of the include program code, as if we just copied the subroutine into the main program.
First create and compile the include program with a subroutine we want to use.
There is a 3 step process when CallInclude is run, assuming all files exist compiled etc.
1] All calling program variable values (that have the same name in both programs) are copied to the include program.
2] The include subroutine is called.
3] All include variables (some may now be modified by the subroutine) are copied back to the main program.
Consider include program (Include.sb):
Code:
x = 1
y = 1
z = 1
MySub()
TextWindow.WriteLine(z)
Sub MySub
z = x+x+y
EndSub
And calling program:
Code:
x = 2
z = 2
inc = LDCall.Include(Program.Directory+"/Include.exe")
TextWindow.WriteLine(inc)
result = LDCall.CallInclude(inc,"MySub")
TextWindow.WriteLine(result)
TextWindow.WriteLine("x="+x)
TextWindow.WriteLine("z="+z)
Step 1] In included code : x set to 2, z set to 2, y is unset (default 0) - remember the CallInclude only looks at the subroutine MySub, it knows nothing about previous code that sets y = 1 for example.
Step 2] MySub sets z = 2+2+0 = 4
Step 3] In main program : x set to 2, z set to 4
Resulting output:
Include1
SUCCESS
x=2
z=4
Press any key to continue...