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 / gosubs in functions

Author
Message
jayell leedham
18
Years of Service
User Offline
Joined: 24th Jan 2006
Location: North Yorkshire
Posted: 9th Apr 2007 20:57
Am I doing this right? - The following does not return ch$ from the subroutine back to the function!
fred=1
george= d(fred)
wait key
end
function d(name)
if name=1
gosub sun:print "ch$= ",ch$
rem this returns "" as ch$
endif
endfunction 0

sun:
ch$="sdf":print "ch$ in the subroutine is ",ch$
return
BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 9th Apr 2007 22:52
Never exit a function except by the endfunction or exitfunction statement. It's bad practise to mix & match functions and subroutines, but to jump from one to the other will cause you many headaches and debugging woes.
TDK
Retired Moderator
21
Years of Service
User Offline
Joined: 19th Nov 2002
Location: UK
Posted: 10th Apr 2007 00:49 Edited at: 10th Apr 2007 00:51
The reason it doesn't return the correct value is because all variables in a function are local - in other words, any variables inside a function cannot be seen outside of it.

This also works in reverse, so the variable called ch$ which is created in the function does not exist in the procedure - it's a completely different empty variable with the same name.

So, you can have five functions and use ch$ in all of them. Each will be a completely different ch$ and all have different contents - none of which can be seen by the other functions or the main program.

So, the rule to apply is that you should always call functions (or other procedures) from procedures - never procedures from functions.

TDK_Man

Dabbler
17
Years of Service
User Offline
Joined: 3rd Mar 2007
Location: Minnesota
Posted: 10th Apr 2007 04:42 Edited at: 10th Apr 2007 05:19
@BatVink and TDK
Yes, I agree, there are some very iffy programing practices in this code. However:

This does work, and no return stacks are compromised.

@ jayell leedham
Listen to these guys. They know what they are talking about.

This is the better option:



Whatever...
jayell leedham
18
Years of Service
User Offline
Joined: 24th Jan 2006
Location: North Yorkshire
Posted: 10th Apr 2007 13:53
Many thanks folk.
TDK
Retired Moderator
21
Years of Service
User Offline
Joined: 19th Nov 2002
Location: UK
Posted: 10th Apr 2007 21:36
Dabbler:

Yes, your code snippet works fine. I specifically didn't mention global variables because jayell leedham might have been using DB Classic where they don't exist - and I might have been confusing the issue even more for him.

If only people read the stickies at the top of the boards, it would avoid this sort of confusion...

TDK_Man

Dabbler
17
Years of Service
User Offline
Joined: 3rd Mar 2007
Location: Minnesota
Posted: 11th Apr 2007 04:55
Thank you TDK for pointing that out. I did not realize that DBC did not have global variables. Had I known that, I would not have posted the first snippet.

And, mabey this will serve as an example to others. Please state the language you are using.

Thanks again. I admire your patience.


Whatever...
TDK
Retired Moderator
21
Years of Service
User Offline
Joined: 19th Nov 2002
Location: UK
Posted: 11th Apr 2007 09:16
Quote: "Had I known that, I would not have posted the first snippet."


No, no, that's the problem when users don't state which version of DB they are using. If he is using DBP then your snippet is more valid than my comments.

As he didn't say, we have no way of knowing which of us has wasted time...

TDK_Man

Login to post a reply

Server time is: 2024-09-25 21:27:00
Your offset time is: 2024-09-25 21:27:00