Subroutine Declaration order?

Coding and general discussion relating to the compiler

Moderators: David Barker, Jerry Messina

Post Reply
Gunplumber
Posts: 38
Joined: Wed Nov 02, 2022 10:31 am

Subroutine Declaration order?

Post by Gunplumber » Sun Dec 04, 2022 6:13 am

Hi Guys

I've noticed that the order of declaration of subroutines matters during compilation. If you reference a subroutine from inside a subroutine that is higher on the list, the compiler gives the error of "Identifier Not declared", which i guess makes sense because in the order of compilation the referenced sub does not yet exist.
This does present one issue however in that what if i wanted to have two subs that reference each other?

IE

Code: Select all


sub A()
	//code
	B ()
end sub


Sub B()
	//code
	A()
end sub

This could never work as one would always be above the other? Is there a way around this?

Also is there a setting to show the subs after the main code or even hide them until you click on them? It gets rather tiresome to scroll past all the subs (which is the bulk of the code in most applications) to find the main body of code..

Cheers
Lee

User avatar
David Barker
Swordfish Developer
Posts: 1214
Joined: Tue Oct 03, 2006 7:01 pm
Location: Saltburn by the Sea, UK
Contact:

Re: Subroutine Declaration order?

Post by David Barker » Sun Dec 04, 2022 11:53 am

Swordfish generally acts like "Turbo Pascal" or "Delphi" when it comes to scope and declaration order so yes, you normally need to declare something before use. You can override this behaviour using "prototype". For example:

Code: Select all

prototype sub subA()
prototype sub subB()

sub subA()
   subB()
end sub

sub subB()
end sub

subA()
allows SubA() to call SubB() before it is declared. However, it is something I personally avoid as a top top algorithmic design approach usually negates the need to do that. But if you need to, prototype should work OK.

Also note that because Swordfish uses a compile time stack and not a runtime one, you still cannot perform any recursive calls. For example:

Code: Select all

prototype sub subA()
prototype sub subB()

sub subA()
   subB()
end sub

sub subB()
   subA()
end sub

subA()
will generate a circular reference error. Note that using a compile time stack is much more efficient with using 18F series architecture and that is why Swordfish uses it.

Your second question I think refers to code folding, which the IDE does not support. You could use a main() entry point sub and get to that quickly using the code explorer? For example:

Code: Select all

main()
   // code here
end main

// call main
main()
or use

Code: Select all

Inline Sub main()
End Sub
if a redundant calls bothers you...

Gunplumber
Posts: 38
Joined: Wed Nov 02, 2022 10:31 am

Re: Subroutine Declaration order?

Post by Gunplumber » Sun Dec 04, 2022 9:19 pm

Hi David

Thanks for the detailed reply. I suspected the use of "prototype" or similar for declaring subs but i couldn't find any reference to it in the help files.
Thanks also for your tips on dealing with the main code.. I think the "Inline sub" solution will work quite nicely. :D
Over all i really do like this compiler. I have come from old school Microsoft Quick Basic (and its modern counterpart QB64), so i feel right at home with Swordfish..

Cheers
Lee

User avatar
David Barker
Swordfish Developer
Posts: 1214
Joined: Tue Oct 03, 2006 7:01 pm
Location: Saltburn by the Sea, UK
Contact:

Re: Subroutine Declaration order?

Post by David Barker » Mon Dec 05, 2022 9:36 am

Yes, the help file is a little outdated. Prototype was added early on but after the compiler release.

Thanks for your kind comments on the compiler...

Dave

Post Reply