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.

Newcomers DBPro Corner / [DB Classic] Functions (GOTO from inside a Function?)

Author
Message
Wandering Swordsman
18
Years of Service
User Offline
Joined: 14th Jun 2006
Location:
Posted: 7th Jul 2006 08:51
Is there a way to exit the function and 're-enter' the global (What would the term be for outside the function?) program at a line of your choosing?

Non-Working Example:


Is there any command capable of this?

When I dream,
I carry a sword in one hand,
a gun in the other...
BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 7th Jul 2006 10:07 Edited at: 7th Jul 2006 10:08
No, don't do it!

You can use

EXITFUNCTION

to leave early, but you have to return to the place where you called the function from. If nothing else, it's very bad practise.



Wandering Swordsman
18
Years of Service
User Offline
Joined: 14th Jun 2006
Location:
Posted: 7th Jul 2006 10:16
Ack, I was hoping there'd be a way.
I'll start writing a work around.

BatVink:
Quote: "If nothing else, it's very bad practise."

Eh... so it is possible then? Why is it bad and how do I do it?

When I dream,
I carry a sword in one hand,
a gun in the other...
Pincho Paxton
21
Years of Service
User Offline
Joined: 8th Dec 2002
Location:
Posted: 7th Jul 2006 15:36
You could fill the buffer up, and crash the computer. Do what Batvink said, and put a GOTO after the call to the function.

Test
If so and so GOTO so and so

GOTO is not a good command anyway.

BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 7th Jul 2006 16:24
GOTO's create what is known as spaghetti code. Imagine that somebody else has to read your code and understand what it is doing. Every time you GOTO, you add confusion.

There are dozens of threads regarding this topic, but the general message is to use GOSUBs and functions, the code is clerarer and it is accepted practise.



Kevin Picone
22
Years of Service
User Offline
Joined: 27th Aug 2002
Location: Australia
Posted: 8th Jul 2006 19:17
Wandering Swordsman,

Goto is a control change. It's perfectly acceptable way to change program flow when used appropriately. However, What you are trying to do is not an appropriate use.

While the compiler should not allow it, that type of code would create a stack overflow. When a function is called. The current calling line (address) and parameters are pushed on a special internal stack. Then DB jumps into the function code. When DB hits the end of the function, it grabs the return line (address) and returns to this point. So End/Exit Function are performing some necessary clean up duties when a function is completed. If you find a way to avoid this via goto'ing out early, then any return information that was pushing on the stack when the function was called, will be left there. So sooner or the later the code will die.

Login to post a reply

Server time is: 2024-09-25 03:30:52
Your offset time is: 2024-09-25 03:30:52