1

how do i call a function where it is declared in another bas file? For example i have 2 bas files.

sum.bas

Declare Function sum( As Integer, As Integer ) As Integer

Function sum( a As Integer, b As Integer ) As Integer
Return a + b
End Function

main.bas

Dim a As Integer
a = sum(1, 2)
Print a
Sleep

I set main.bas as the main module but i cannot call sum function....

demosthenes
  • 1,151
  • 1
  • 13
  • 17

2 Answers2

1

The solution is the Declare statement to be written in main.bas

sum.bas

Function sum( a As Integer, b As Integer ) As Integer
Return a + b
End Function

main.bas

Declare Function sum( As Integer, As Integer ) As Integer
Dim a As Integer
a = sum(1, 2)
Print a
Sleep
demosthenes
  • 1,151
  • 1
  • 13
  • 17
  • declare doesnt work in this way Declare is needed in classes and libraries as a reference to external functions is not required on standalone code. – ExagonX Oct 02 '21 at 13:39
  • @ExagonX These _are_ external functions. `sum.bas` is a different module than `main.bas`. In the final executable, both will exist, so the linker can find the definition at the final linking step, but in order to know that such a function is supposed to exist, it has to be `Declare`d for the compiler to know its signature if the implementation is in a different module. See [docs for `Declare`](https://documentation.help/FreeBASIC/KeyPgDeclare.html): "FreeBASIC, as QB, does not require the declaration of the functions **unless they are defined in a different source file**" – CherryDT Feb 11 '22 at 17:54
  • @CherryDT Thanks for your clarification, but given my limited experience it is difficult for me to understand what you wrote. The author asked how to execute a function placed in another source file, FreeBASIC does not run uncompiled sources, so unless a library is created and specified with #inclib the declare will not work without including the source via #include. Obviously my opinion is based on my ignorance about it. I would be happy if you would show me examples that show my error. – ExagonX Feb 12 '22 at 11:59
  • You can specify multiple source files, each of which will be a separate module, and you have to declare functions from other modules if you want to use them. See second example [in the docs](https://www.freebasic.net/wiki/CompilerCmdLine). For the OP the command would be `fbc main.bas sum.bas`. – CherryDT Feb 12 '22 at 18:37
  • Every compilation process consists of several steps: code generation for each module (to asm or c files depending on selected backend), compilation of each such generates code file to an object file (*.obj), linking of all object files plus any external libraries to a final executable. Each specified source file is a module and as such the code generation & object file compilation part is done separately. To reference some function not defined in the same compilation unit (but in another module or in a library), `Declare` is used. It's the linker's job at the end to resolve the references. – CherryDT Feb 12 '22 at 18:58
  • It was not me who gave this example but the author himself who voted for himself, I know very well that it is wrong in fact in the next answer there is my example without the declare. – ExagonX Feb 12 '22 at 22:01
  • No the issue was that the author originally put the `Declare` into `sum.bas` instead of putting it into `main.bas` (or a header file). Their answer then shows how it's done correctly (the `Declare` is correct but must be in `main.bas` and not (only) `sum.bas`). The author was using modules as evident by their sentence "I set main.bas as the main module", so the `Declare` has to exist in the module in which the function _isn't_ defined. – CherryDT Feb 13 '22 at 00:09
0

In FreeBASIC like other languages it is possible to separate the code into multiple files, where the main file must include the child files that contain other pieces of code using #include "filename.bas"

sum.bas

Declare Function sum( As Integer, As Integer ) As Integer

Function sum( a As Integer, b As Integer ) As Integer
Return a + b
End Function

main.bas

#include "sum.bas"
'with "INCLUDE" it is as if the code contained in another file was written 
'at that point.

Dim a As Integer
a = sum(1, 2)
Print a
Sleep
ExagonX
  • 141
  • 10
  • That works bit it's a bit of an abuse of `#include` (just like in C where you aren't really supposed to include `c` files, only `h` files). You are supposed to include just a headers file (`bi`) that has the declaration for the functions you want to use. You would then compile to different `bas` files as modules and link them into the same executable at the end. – CherryDT Feb 11 '22 at 17:52
  • @CherryDT Thank you for your Answer. I don't understand why it should be considered an abuse since FreeBASIC's #include works to separate the main code from the functions the same goes for C In fact C distinguishes the closed header between and parts of the closed code between "source.c" It seems to me like another war between Pointers and Globals, FreeBASIC as C was created precisely to be versatile, imposing a type of programming cancels its versatility. – ExagonX Feb 12 '22 at 11:45
  • Okay maybe the word "abuse" was too harsh. What I'm trying to say is creating one huge blob of code as a single compilation unit has many downsides, such as sharing a namespace unless you take manual steps to avoid that, and most importantly when you do a small change, the whole code has to be recompiled instead of only the module that was affected. It allows for a lot more modularity and clear separation boundaries if you compile every source file as module and only include headers that declare the functions (`Declare`), globals (`Extern`) and types (`Type`) each module exports. – CherryDT Feb 12 '22 at 18:48
  • Yes, now I understand and I agree with you, but at this point we use #inclib "lib" in files with the definitions necessary to use the library in question freebasic uses header with extension ".bi" – ExagonX Feb 12 '22 at 19:53
  • This is unrelated to libraries... I'm talking about multiple modules of source files, not libraries. – CherryDT Feb 12 '22 at 20:56
  • @CherryDT if you need to compile only a part of the program that part must be in a library (.so, .dll etc) and be included in the main with #inclib "mylib" Libraries must be compiled separately. example: mylib.bas sub myage (byval age as integer = 0) extern print "My Age are:" & age end sub fbc -dll mylib.bas this make a file mylib.dll now whe create the main program main.bas #inclib "mylib" declare sub myage (byval age as integer = 0) dim askage as integer print "How old are you?" input askage myage (askage) sleep fbc main.bas This make main.exe – ExagonX Feb 12 '22 at 21:45
  • FreeBASIC does not support code execution in SOURCE format, it must be compiled. – ExagonX Feb 12 '22 at 21:53
  • 1
    Again, I'm not talking about libraries or "executing in source format"... I'm talking about multiple source files = compilation units = modules compiled together. For example `fbc main.bas sum.bas`. This will do the same as `fbc main.bas -c -o main.o` (create main.o from main.bas) and `fbc sum.bas -c -o sum.o` (create sum.o from sum.bas) followed by `fbc -m main main.o sum.o` (create main.exe from main.o and sum.o). That's how you'd normally split your project code files into individual parts. – CherryDT Feb 12 '22 at 22:02
  • Ahh you mean the object files, yes now everything is clear, even if I prefer to use the libraries because I can update only the library without necessarily compiling the entire software. Anyway ok sorry for my misunderstanding. – ExagonX Feb 12 '22 at 22:08
  • 1
    I wrote a full explanation now, to clear up any misunderstandings: https://gist.github.com/CherryDT/8e8c10c7139179774c2e6dff88433559 – CherryDT Feb 12 '22 at 23:37