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.

Code Snippets / [DBP] - Stacks / Returning multiple values from within functions

Author
Message
Mr Kohlenstoff
17
Years of Service
User Offline
Joined: 7th Jun 2006
Location: Germany
Posted: 8th Mar 2012 19:08
When you want to return more than one value from within a function there are several ways to do that, including arrays, memblocks, global variables, encoding the values into individual bytes of an integer and probably much more.

I wanted to find a way to do that in a relatively efficient yet convenient way, so I wrote some stack-functions that allow just that.
Note that the stacks I'm talking about are not 100% equal to what computer scientists usually refer to as stacks - the difference here is that "my" stacks have a fixed capacity and can't run into overflows. This, however, leads to the problem that when you store too many values at once, some of them will be overwritten. However, since you rarely return more than 256 (that's the current capacity, can be changed easily using a constant) values at once, this should not happen usually. The only problem you could still run into is a situation where several nested function calls all access this stack, in this case things could get pretty messed up, so you might want to avoid that in case you are going to use those functions.


First, here's the command set:
stack_init() - has to be called once initially, sets up arrays etc.

stack_pushInt(i) - stores one integer on top of the int-stack
stack_pushFloat(f#) - stores one float on top of the float-stack
stack_pushString(s$) - stores one string on top of the string-stack

stack_popInt() - takes one integer from the int-stack
stack_popFloat() - takes one float from the float-stack
stack_popString() - takes one string from the string-stack

stack_getInt() - returns one integer from the int-stack without removing it
stack_getFloat() - returns one float from the float-stack without removing it
stack_getString() - returns one string from the string-stack without removing it



When using these functions for function return values, one advantage is that the amount of returned arguments does not need to be constant. Consequently you can, for instance, write a simple split string-function that returns string-parts dynamically depending on how many are existing.

Code with examples:





Stack-Library only:




There are, as mentioned before, many other ways to accomplish this, especially if the amount of returned values is known beforehand (e.g. if you want to return exactly two values, you could simply use a global to store the second one). However, using this library provides a simple and unified way to handle it, without producing too much overhead (i.e. the functions should be quite fast, although certainly slower than simply accessing one global variable instead).

On top of that they can be used for any purpose that requires a stack (although DBP provides functions for that, but in this case it is possible that the built in commands are slower (didn't test it) since they probably allocate memory on the fly, which is not the case for the functions above).

Hope it helps, although once again, it does not really provide any functionality that wasn't there before.

greetings,
MrK

IanM
Retired Moderator
21
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 13th Apr 2012 01:04
I'm curious about why you implemented a stack system (LIFO) when a queue would seem to be a more natural fit (FIFO).

Zotoaster
19
Years of Service
User Offline
Joined: 20th Dec 2004
Location: Scotland
Posted: 16th Apr 2012 12:55
Why are queues more natural? RPN, a quite standard way of computing values works on stacks as do the way values are often returned from functions. Plus queues are less efficient (unless they are circular).

"everyone forgets a semi-colon sometimes." - Phaelax
IanM
Retired Moderator
21
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 17th Apr 2012 15:48
Maybe, but people don't work in RPN.

There's also no difference in efficiency between a circular queue and a circular stack, which is what the guy implemented in the first place - all that's needed is one extra index/counter.

Mr Kohlenstoff
17
Years of Service
User Offline
Joined: 7th Jun 2006
Location: Germany
Posted: 24th Apr 2012 09:18 Edited at: 24th Apr 2012 09:18
Quote: "I'm curious about why you implemented a stack system (LIFO) when a queue would seem to be a more natural fit (FIFO)."


Well, as can be seen in the split-function of the example code, the order indeed causes some problems. Using a queue would certainly solve them, but in this case you'd always have to store the index of the first return-value in order to access them all, since it is possible that several functions's return values are in the stack/queue at once.
So I guess both approaches have their advantages. But to be honest, using queues simply did not occur to me when I wrote the code.

Login to post a reply

Server time is: 2024-04-19 22:53:39
Your offset time is: 2024-04-19 22:53:39