Call a Function from another bas file in FreeBASIC

290 views Asked by At

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....

2

There are 2 answers

7
demosthenes On BEST ANSWER

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
10
ExagonX On

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