SUB must be defined before main program?

New to FreeBASIC? Post your questions here.
Post Reply
bulrush
Posts: 61
Joined: Nov 02, 2010 12:56
Location: Michigan

SUB must be defined before main program?

Post by bulrush »

Hi,
I'm a programmer but haven't programmed in BASIC in 20+ years.

In Freebasic, do all my subs and functions have to be defined in my .BAS file before the main program?

Also, I use include files. Do included HAVE to be named ".BI"? Or can I name them ".BAS"?

Thanks. Just getting used to the nuances of Freebasic.
RayBritton
Posts: 306
Joined: Jun 02, 2005 7:11
Contact:

Post by RayBritton »

You can write the functions or the functions prototype before the code, but yes, either of those must be both the function is called

ie:

Code: Select all

declare function foo(bar as integer) as integer

'main code including call to foo

function foo(bar as integer) as integer
    'code
end function
or

Code: Select all

function foo(bar as integer) as integer
    'code
end function

'main code including call to foo
agamemnus
Posts: 1842
Joined: Jun 02, 2005 4:48

Post by agamemnus »

The extension of the file has no meaning for the compiler as far as I know. It's just standard to use .bi for type/sub/function definitions and .bas for the code.
kiyotewolf
Posts: 1009
Joined: Oct 11, 2008 7:42
Location: ABQ, NM
Contact:

Post by kiyotewolf »

Code: Select all

#include "menuinc.bas"

rem variables and  type definitions

rem declare sub
rem declare function

I hate having *.BI, and whatever, *.A, *.DLL, .. all that garbage.

To include a file, make sure, that all the code, is non executable.

Meaning, if you put everything in a SUB or FUNCTION, and don't have a MAIN routine part of the program, you can include that, as if it was simply typed into your existing program for you.

I could never get *.BI's to work in QBasic, or QuickBasic, so I don't use them here either.



~Kiyote!

You may run into places, where you should pull out variable declarations, and put them into other places, like included files.

You can actually alter the scope, meaning, can this part of the program see that part of the program, based on where the files are included.

Are you including the file, from within another included file?

You can daisy chain,.. like so.

[main program] -- #include "llama.bas" -- [llama.bas] -- #include "crt_junk.bas"

That's a graphical representation, not anything code or otherwise, or any FB dialect.

Everything in the crt_junk.bas, is seen by the llama.bas, and then seen by the main program.

Stuff declared in the main program, may not be accessable by routines in the crt_junk.bas.

So, you might pull out those declarations, and put them in the crt_junk.bas, so the crt_junk.bas can work on those variables, as well as the main program.
kiyotewolf
Posts: 1009
Joined: Oct 11, 2008 7:42
Location: ABQ, NM
Contact:

Post by kiyotewolf »

You can get away with not declaring them, until one routine trys to call another.

Based on which order, in the main program, they are, SUB's will see other SUB's further down the line, but they will not see what is above them.

Or visaversa. I forget which.

It's best to declare them, however, because it tells the FB compiler, OK HEY,.. THIS EXISTS. Point all calls to this routine by this name here, and we won't get confused.

:.:

It's not absolutely necessary, to get a program to work, but after you shuffle things around, if they are NOT declared, you'll get loads of errors.

Just do this.

Highlight the SUB blah blah, and then, go to the top of the program.

Type out

DECLARE

Then, press space, and then SHIFT-INS, and paste the same line, as the beginning of the SUB declaration.

You want them to be identical, from the declaration to the actual SUB or FUNCTION, because if not, you'll have errors yet again.

:.:



~Kiyote!

It's either one way or the other. But, upstream, they'll see the other SUB's and FUNCTION's. Downstream routines, they'll be blind to. That's why declaring them, makes that issue disappear.
counting_pine
Site Admin
Posts: 6323
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

Post by counting_pine »

The idea of includes is to separate out code and definitions (types, declares, etc). A feature of header files is that, because they contain no actual runnable code, they can be included by multiple modules without causing duplicated function bodies.

An alternative, sloppier way is just to split up code into multiple .bas files and daisychain them all together into one effective module using #includes.
bulrush
Posts: 61
Joined: Nov 02, 2010 12:56
Location: Michigan

Post by bulrush »

Thank you. I do not "daisy chain" .bas files. I have one main .bas file which contains my main program code, and that includes all my other .bas files. I even have one include file (included at the top of the main.bas file) which has only constants!

To make using colors easier in the COLOR statement, I made a bunch of constants, one for each color. So I named them colWhite, colBlack, colBlue, so it's easy to choose the color instead of having to remember the color number.
I Daniel
Posts: 43
Joined: May 13, 2010 10:34
Location: Centurion, South Africa

Declare Subs and Functions

Post by I Daniel »

Going retro; If you are still using the QB4.5 dialect then it is easier to start of with QB4.5 which automatically declares all subs and functions you have created, save it as a text file and then go to the FBIde. Add what you want and then compile it.
Ok it's not so FB and it sounds like foofees and it is slightly convoluted.

Could'nt we have the same in FB please?. Nope. Oh well I tried

The best method is to avoid problems and you must therefore Declare them at the beginning of your program.
Post Reply