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 / Function Returning Values Question

Author
Message
MatriX
21
Years of Service
User Offline
Joined: 19th Oct 2003
Location: Deer Park, New York
Posted: 21st Aug 2006 09:57 Edited at: 21st Aug 2006 09:59
Can a function return more then one value? If so, what does that look like?
Don't try to run this code its for point clarification only.



In the example above I'm trying to figure out how to get all three variables back to the main routine so that you would see the updated values in the print statement.

I know I can use "endfunction X" but how would I get X,Y and Z?

Thank you TGC for creating such amazing products!

Success does not come with meeting the minimum requirements.
Latch
18
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 21st Aug 2006 10:47 Edited at: 22nd Aug 2006 02:56
A function can only return 1 value. You could mask all three values in a string, and then separate them out later:

vertices$=str$(x)+":"+str$(y)+":"+str$(z)

I put the colon in there as a marker for when you want to separate the values later.

A little more "C like" way to return three or more values (even of varying data types) would be to use a memblock - kinda like a structure.



It looks longer that it actually is because of all the comments I put in - but it's a very easy method to use.

Enjoy your day.
Scraggle
Moderator
21
Years of Service
User Offline
Joined: 10th Jul 2003
Location: Yorkshire
Posted: 21st Aug 2006 10:54 Edited at: 21st Aug 2006 10:54
Global variables are your friend

Functions can only return one value (but not the way you are doing it), however, if you make your variables global then you can change them in a function and still get the value outside of it.
Like this:



Latch
18
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 21st Aug 2006 11:07
In BarkBASIC classic, you can't declare globals. They exist, but outside of functions.

Enjoy your day.
NeilF
18
Years of Service
User Offline
Joined: 2nd Aug 2006
Location:
Posted: 21st Aug 2006 11:53
"Global variables are your friend"

If you're going to use variables to pass stuff in/out of a function, may as well use a gosub surely?
MatriX
21
Years of Service
User Offline
Joined: 19th Oct 2003
Location: Deer Park, New York
Posted: 21st Aug 2006 14:32
Very informative thank you.

Thank you TGC for creating such amazing products!

Success does not come with meeting the minimum requirements.

Login to post a reply

Server time is: 2024-11-27 09:50:06
Your offset time is: 2024-11-27 09:50:06