Any possibility of gosub returning values?

Hi,
Is there any possibility of adding so that you can assign a return value from *gosub to a variable? *set variable *gosub function arg1 arg2… or *set variable function(arg1,arg2…). Inside the gosub you would lodgicly do *return value. It’d be useful for the kind of function whose use is to return a value rather than print to the screen. You can create a variable to store a return value, but it’s certainly annoying.
I would code this functionality myself, but I really only understand some of the syntax, not how the CS engine actually works at this point.

Check out parameters: New ChoiceScript features: implicit control flow, gosub parameters

It wouldn’t be hard to add a global var called “return_val”.

*gosub label x y z

*label
*dostuff
*set return_val val
*return

Yeah. I tend to do this by declaring a temp before the gosub, which modifies it by name in its body, or by declaring the temp at the top of the scene if the sub will be called more than once. A little less awkward than dealing with a global return value. It makes me slightly sad, but not nearly as sad as the required gotos did, and not even as sad as expression parsing that requires a bunch of parens (which probably what I’d fix next if I were to fix something), and so I probably won’t try to make/fight for that patch without a compelling use case.

Not that it much matters, each to their own, but I’m curious: what do you find awkward about a single global value? It’s more manageable/legible (than an extra temp def per function). You can’t forget to define it, and you can’t write parallel choicescript code, so you’re never going to get clashes (if you forget about it and the next call overwrites it, that’s just like forgetting to assign the value in a real language).

EDIT: Definite disadvantage is you can’t build up returns (or return multiple values), but that begs a whole other question as to whether that’d be good practice in itself.

1 Like

I guess two things: using a stale return_val because the gosub didn’t set it would lead to weird bugs; and return_val strikes me as a little gross as a variable name, like x or myCoolVal.

But as you say, to each their own. I did try that way a couple times and just decided I didn’t like it.

I found a pattern that works well for me. You can pass the return variable as a reference, like you would pass a pointer in other languages.

*gosub sum 100 10 "c"
-------
*label sum
*params a b ref
*set {ref} a + b
*return
2 Likes