English Amiga Board

English Amiga Board (https://eab.abime.net/index.php)
-   Coders. Blitz Basic (https://eab.abime.net/forumdisplay.php?f=126)
-   -   Forward statement or function (https://eab.abime.net/showthread.php?t=101637)

Nightshft 08 April 2020 16:30

Forward statement or function
 
I just start using statements/functions.
(f.e. to write a small function to calculate "atan2")

From all info I found and all I tried the function has to be written above the function call in the code, to be found.

Is it possible to forward functions and statements (so they are found even at the end of the code)?

E-Penguin 09 April 2020 01:42

Define them in a separate file and include them at the top of your main file

Nightshft 09 April 2020 03:09

Yes, that was my PlanB too.
A pity there's no "forward".
Thanks E-Penguin, stay safe!

Thomas Richter 09 April 2020 11:55

Quote:

Originally Posted by Nightshft (Post 1390455)
Is it possible to forward functions and statements (so they are found even at the end of the code)?

Yes, of course. That's a "prototype". For that, just write the function header, but omit the body of the function:
Code:

int foo(int arg1,double arg2); ... int bar(..) {  foo(1,42.0); /* use foo */ } ... int foo(int arg1,double arg2) { /* implement foo */ }

Daedalus 09 April 2020 16:27

Quote:

Originally Posted by Thomas Richter (Post 1390629)
Yes, of course. That's a "prototype".

Unfortunately, the Blitz compiler doesn't support this prototype concept that C uses. Only one function declaration is allowed throughout the code, and it must be declared before it is called. Which can be a pain when you have lots of functions calling other functions...

For bigger projects, I tend to keep functions in a separate file as suggested and include it near the top of the main source file. Alternatively, if you want to keep the definitions in the main file, you can add labels to the code so it's easy to skip past the function declarations. For example:

Code:

Function MyFunc.b{var.w}
[...]
End Function

; *** Start of main code
.maincode

[...] Main loop here

Putting the dot in front of the maincode label here makes it show up in the navigation pane to the right of the code editor. You can then click on it to jump the editor directly to that point instead of scrolling through.

Nightshft 17 April 2020 01:02

Sorry for the late answer but real life (and Revision) got in the way.

I was asking mainly because I was surprised because contrary to functions, "Gosub" finds subroutines no matter where they are. Guess that's one of the reasons why Gosub is more often used :laughing

Anyway, just checked out INCLUDE and XINCLUDE. They work nicely, so that's a good possibility to include statements/functions. Thanks for the clarification! :)


All times are GMT +2. The time now is 16:51.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.

Page generated in 0.04088 seconds with 11 queries