Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Beginner Q] How to use subs/functions from external file?
#1
Hi all,

New user here, thanks for providing this forum & please bear with me...

I am trying to learn how to build my own external library of functions and/or subroutines.

Here is what I have right now:

Code: (Select All)
'$Include: 'mylib.bas'

AddNumbers

Error in message window: "Statement cannot be placed between SUB/FUNCTIONs on current line"

Code: (Select All)
' mylib.bas

' Add two numbers. Or don't.
Sub AddNumbers (a, b)
  'AddNumbersResult = a + b
  'Print "Adding: "; _ToStr$(a); " + "; _ToStr$(b); " = "; _ToStr$(AddNumbersResult)
  Print "Hello world."
End Sub

What I've tried so far:


  1. Adding, removing parens to/from the call to AddNumbers
  2. Adding, removing parameters to/from the call to AddNumbers - AddNumbers(2,3)
  3. Adding, removing "as Integer" to/from Sub/function parameters
  4. Using variables to call the sub/function, rather than literal integers
  5. Removing spaces between sub/function name and parens
  6. Converting the sub to function and back again
  7. Moving the include directive from top to bottom
  8. Adding a declaration of the sub to the library file
  9. Changing all function/sub logic to simply "Hello world." just in case
  10. Browsing the wiki entries for sub, function, include, etc.

The changes I made have changed the error message to say different things, but I haven't once been able to run the program after guessing which changes were required.

Any help is appreciated! Version I'm using is 4.0, Manjaro Linux. I am used to programming in other languages like PHP, Perl, Raku, Object Pascal, and others, but fairly new to this kind of library-call feature in basic dialects.
Reply
#2
(02-02-2025, 09:23 PM)sublogic Wrote: Hi all,

New user here, thanks for providing this forum & please bear with me...

I am trying to learn how to build my own external library of functions and/or subroutines.

Here is what I have right now:

Code: (Select All)
'$Include: 'mylib.bas'

AddNumbers

Error in message window: "Statement cannot be placed between SUB/FUNCTIONs on current line"

Code: (Select All)
' mylib.bas

' Add two numbers. Or don't.
Sub AddNumbers (a, b)
  'AddNumbersResult = a + b
  'Print "Adding: "; _ToStr$(a); " + "; _ToStr$(b); " = "; _ToStr$(AddNumbersResult)
  Print "Hello world."
End Sub

What I've tried so far:


  1. Adding, removing parens to/from the call to AddNumbers
  2. Adding, removing parameters to/from the call to AddNumbers - AddNumbers(2,3)
  3. Adding, removing "as Integer" to/from Sub/function parameters
  4. Using variables to call the sub/function, rather than literal integers
  5. Removing spaces between sub/function name and parens
  6. Converting the sub to function and back again
  7. Moving the include directive from top to bottom
  8. Adding a declaration of the sub to the library file
  9. Changing all function/sub logic to simply "Hello world." just in case
  10. Browsing the wiki entries for sub, function, include, etc.

The changes I made have changed the error message to say different things, but I haven't once been able to run the program after guessing which changes were required.

Any help is appreciated! Version I'm using is 4.0, Manjaro Linux. I am used to programming in other languages like PHP, Perl, Raku, Object Pascal, and others, but fairly new to this kind of library-call feature in basic dialects.

Very simple solution here, what would you do if the SUB was in the main program and not in an external file?
Most probably you would place the SUBs and FUNCTIONs at the end of the main code, right?
So, now think where your $INCLUDE line must go.

From the $INCLUDE Wiki page:
Quote:Common extensions are .BI (for declarations, usually included in the beginning of a program) or .BM (with SUBs and FUNCTIONs, usually included at the end of a program).
From the SUB Wiki page:
Quote:$INCLUDE text library files with needed SUB and FUNCTION procedures can be included in programs after all sub-procedures.

BTW - Welcome to the Forums.
Reply
#3
That's very interesting, thanks for getting back to me.

The interesting points + (my experiences with them):
  • Add include at bottom of file (as mentioned - tried previously, but in isolation it did nothing)
  • Change to .bm format (this is interesting, see below - a fix but against wiki logic)

Changing the include file to .bm fixed the issue. However...

I had tried to use .bm before, but QB64pe saved this file as .bm.bas.

So, I read the wiki:

Quote:Any extension can be used, as long as the file contains code in plain text 
And decided: "If the program prefers to work with .bas files, and the wiki says any extension can be used, I'll use .bas as the extension for my include file"  Idea

It seems that this wiki statement:

Quote:Any extension can be used, as long as the file contains code in plain text 

May no longer be accurate? 

Thank you.
Reply
#4
Well, the extension does not matter definitly, could be even .txt,

It's intresting that the file got the .bas extension added when saving as .bm, if an extension is explicitly given, then the .bas shouldn't be added anymore. That could be a bug, so you are on Linux right?

Just tried here on Windows and it worked as expected, when "Save As..." and give a explicit extension, then there's no .bas added, so it could be a Linux only bug.

Can you please once more try and confirm that a "Save As..." and typing "myfile.bm" adds the .bas?
Reply
#5
OK, I tried to reproduce the issue of save with .bm.bas, but I can't here. It occurred on my other system (Kubuntu desktop) so I will try it again there when I get a chance.

For now, the secret to happy saving seems to be in this checkbox:

[Image: savefile_p.png]

That is, one should un-check "Automatically select filename extension (.bas)".

This is also true after the .bm file is saved. Because it will then try to automatically add the ".bm" extension every time you save.

Quote:could be even .txt,
I tried the .txt extension but it still will not work. It says there is a Syntax error on line 1:

Code: (Select All)
AddNumbers 1, 2

'$Include: 'mylib.txt'

However, I can double-click on the include line helper, and it opens the .txt file in a separate window.
Reply
#6
Hi sublogic
I can confirm that in windows all this code works.
Reply
#7
Always remember to close Windows before using your sub. Linux doesn't have that problem.

Pete Big Grin
Reply
#8
It would be really convenient if main code could be put before, in between and after subs and functions.
I don't see any (compatibility) issues in supporting that in the future ?
45y and 2M lines of MBASIC>BASICA>QBASIC>QBX>QB64 experience
Reply
#9
(02-05-2025, 02:18 PM)mdijkens Wrote: It would be really convenient if main code could be put before, in between and after subs and functions.
I don't see any (compatibility) issues in supporting that in the future ?
Well (of course) main code does go before subs and functions now, but how would the other options be convenient for you? I’d think it would be harder to find things if main code could be put anywhere, bwdik…
Reply
#10
(02-05-2025, 02:37 PM)NakedApe Wrote:
(02-05-2025, 02:18 PM)mdijkens Wrote: It would be really convenient if main code could be put before, in between and after subs and functions.
I don't see any (compatibility) issues in supporting that in the future ?
Well (of course) main code does go before subs and functions now, but how would the other options be convenient for you? I’d think it would be harder to find things if main code could be put anywhere, bwdik…
You could either accept declaration everywhere in between (like c/c++) and/or accept every statement but make it the responsibility of the programmer to find/manage it's code (some might prefer to put all main code at the bottom)
45y and 2M lines of MBASIC>BASICA>QBASIC>QBX>QB64 experience
Reply




Users browsing this thread: 1 Guest(s)