Call a Function from another bas file

New to FreeBASIC? Post your questions here.
demosthenesk
Posts: 4
Joined: Jul 15, 2021 7:23
Location: Greece

Call a Function from another bas file

Postby demosthenesk » Jul 21, 2021 20:12

Hello,
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....
fxm
Moderator
Posts: 10441
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Call a Function from another bas file

Postby fxm » Jul 21, 2021 20:20

main.bas

Code: Select all

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

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

sum.bas

Code: Select all

Function sum( a As Integer, b As Integer ) As Integer
    Return a + b
End Function
demosthenesk
Posts: 4
Joined: Jul 15, 2021 7:23
Location: Greece

Re: Call a Function from another bas file

Postby demosthenesk » Jul 21, 2021 20:31

i did this and i get:

23:30:28: Compilation: "/home/user/Bin/FreeBASIC-1.08.1-ubuntu-20.04-x86_64/bin/fbc" -b "main.bas" -exx -i "/home/user/Bin/FreeBASIC-1.08.1-ubuntu-20.04-x86_64/include/freebasic" -p "/home/user/Bin/FreeBASIC-1.08.1-ubuntu-20.04-x86_64/lib/freebasic/linux-x86_64" 2> "/home/user/Bin/VisualFBEditor.1.2.9.1/Temp/Compile.log"

ld: main.o: in function `main':
main.c:(.text+0x7c): undefined reference to `SUM'

23:30:28: Do not build file.

i use Visual FB Editor 1.2.9
demosthenesk
Posts: 4
Joined: Jul 15, 2021 7:23
Location: Greece

Re: Call a Function from another bas file

Postby demosthenesk » Jul 21, 2021 20:41

i found it
the compiler does not compile sum.bas... it takes only main.bas...

it must be a bug on Visual FB Editor.

i run fbc main.bas sum.bas and it works
Thanks anyway
dodicat
Posts: 6988
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Call a Function from another bas file

Postby dodicat » Jul 22, 2021 11:14

Normally you would
#include "somefile.bas"
Does Visual FB Editor have a quick run or run only option?
If it does you could try this.

Code: Select all



dim  as string s
s+="#define x 1"+chr(13,10)
s+="Function sum( a As Integer, b As Integer ) As Integer"+chr(13,10)
s+="Return a + b"+chr(13,10)
s+="End Function"+chr(13,10)


sub save(filename as string,s as string)
    var f=freefile
open filename for output as #f
print #f,s
close
end sub

save("sum.bas",s)


'#include "sum.bas"


#ifdef x
Dim a As Integer
a = sum(1, 2)
Print "sum = ";a
#else
print "Run again, this time #include ""sum.bas"""
#endif

'kill "sum.bas"
sleep

When you have tested uncomment the kill "sum.bas" to clean up.
Xusinboy Bekchanov
Posts: 411
Joined: Jul 26, 2018 18:28

Re: Call a Function from another bas file

Postby Xusinboy Bekchanov » Jul 22, 2021 11:32

I added the ability to compile multiple files:
https://github.com/XusinboyBekchanov/Vi ... /issues/87

Return to “Beginners”

Who is online

Users browsing this forum: No registered users and 2 guests