![]() |
[Beginner Q] How to use subs/functions from external file? - Printable Version +- QB64 Phoenix Edition (https://qb64phoenix.com/forum) +-- Forum: Chatting and Socializing (https://qb64phoenix.com/forum/forumdisplay.php?fid=11) +--- Forum: General Discussion (https://qb64phoenix.com/forum/forumdisplay.php?fid=2) +--- Thread: [Beginner Q] How to use subs/functions from external file? (/showthread.php?tid=3436) Pages:
1
2
|
[Beginner Q] How to use subs/functions from external file? - sublogic - 02-02-2025 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)
Error in message window: "Statement cannot be placed between SUB/FUNCTIONs on current line" Code: (Select All)
What I've tried so far:
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. RE: [Beginner Q] How to use subs/functions from external file? - RhoSigma - 02-02-2025 (02-02-2025, 09:23 PM)sublogic Wrote: Hi all, 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. RE: [Beginner Q] How to use subs/functions from external file? - sublogic - 02-02-2025 That's very interesting, thanks for getting back to me. The interesting points + (my experiences with them):
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 textAnd 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" ![]() 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. RE: [Beginner Q] How to use subs/functions from external file? - RhoSigma - 02-02-2025 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? RE: [Beginner Q] How to use subs/functions from external file? - sublogic - 02-02-2025 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: ![]() 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)
However, I can double-click on the include line helper, and it opens the .txt file in a separate window. RE: [Beginner Q] How to use subs/functions from external file? - TempodiBasic - 02-03-2025 Hi sublogic I can confirm that in windows all this code works. RE: [Beginner Q] How to use subs/functions from external file? - Pete - 02-04-2025 Always remember to close Windows before using your sub. Linux doesn't have that problem. Pete ![]() RE: [Beginner Q] How to use subs/functions from external file? - mdijkens - 02-05-2025 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 ? RE: [Beginner Q] How to use subs/functions from external file? - NakedApe - 02-05-2025 (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.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… RE: [Beginner Q] How to use subs/functions from external file? - mdijkens - 02-06-2025 (02-05-2025, 02:37 PM)NakedApe Wrote: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)(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.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… |