Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Discussion / subroutines in functions?

Author
Message
Bizar Guy
20
Years of Service
User Offline
Joined: 20th Apr 2005
Location: Bostonland
Posted: 15th May 2008 16:02
I've been moving parts of my code into functions, and I've recently put a piece of code and the gosubs it calls into a function. Suddenly my code would just end after calling the function, not giving any error, but just ending the program. It was as if the function was not returning to where I'd called it from. After some testing, I found out that this only happened when there were gosub subroutines in the function, regardless of whether I was calling them or not.

Can I not put subroutines in a function, seeing as it is something of a subroutine itself? That would be awful, I think, as I want to eventually export this function to and #include file, and it would be much harder to deal with without subroutines.

jinzai
18
Years of Service
User Offline
Joined: 19th Aug 2006
Location: USA
Posted: 15th May 2008 16:46 Edited at: 15th May 2008 16:47
I can't imagine that working, as subs and functions are fundamentally different in how they are used in the program. They both have a similar, but mutually exclusive scope. (You cannot have a function in a sub, either.)

Actually, a subroutine is more like a function that accepts no parameters and returns no value. Functions create call instructions, and use the stack. Subroutines create call instructions , but do not use the stack beyond pushing a return address on there.

No worries, I do that all the time. You anticipate scope problems, but there are none. Most folks prefer subs because they are not aware that functions do not have to have scope problems at all. you just have to get better at data management when using functions that operate on global variables.

So, you will probably have to learn how to manage your variables yourself to make it work, but that's not a bad thing, really.
TDK
Retired Moderator
22
Years of Service
User Offline
Joined: 19th Nov 2002
Location: UK
Posted: 15th May 2008 21:49
As functions and subroutines 'return' to where they were called automatically, they have to store this information. For this, the stack is used.

Bear in mind that when you go into a function, DB assumes that the value it puts on the stack is going to required at the end of that function and putting a procedure in the middle can corrupt the stack - sending the program counter to the wrong part of the program, crashing it.

TDK_Man

Libervurto
18
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 16th May 2008 00:36
have you tried keeping all the subs inside the function like a mini program.
I haven't tried it but could work.

It is far better to complete a 10 line program than to start a 10,000 line program.

Login to post a reply

Server time is: 2025-06-05 21:13:28
Your offset time is: 2025-06-05 21:13:28