Help understanding how to handle a return value
Don M
Posts: 1,653
If I have a method that will return a value how do I handle the value on the return to the calling method?
For instance:
In the calling method I call some_method but I don't know how to bring in the response. Hope you understand what I'm asking.
Thanks.
Don
For instance:
pub some_method | response some code if this happens response := 1 else response := 0 return response
In the calling method I call some_method but I don't know how to bring in the response. Hope you understand what I'm asking.
Thanks.
Don
Comments
My main code might be this:
So after I return from some_method how do I react to the value returned?
for this yofine a global long
The call for your method is then used as assignemt to this variable see code example below
best regards
Stefan
It's important to remember that a method can only return one long. If you're clever, you can pack it as two words or four bytes. If I need a method to affect multiple values I pass the starting address of those values to the method.
In addition to Jon's comments, you can eliminate the return statement by making response the automatic return value. Just place a colon ([noparse]:[/noparse]) in front of its declaration, instead of the vertical bar (|).
-Phil
The implicit return-value (if you do not return anything with RETURN) is a variable called RESULT.
So if you not retun anything by yourself (not using RETURN statement) you can imagine a RETURN RESULT at the end of your method even if it is not standing there.
The variable RESULT is allway defined and in OPPOSITE to all local variables you declare after the | it is initialised to 0. ALL OTHER local Variables you declare ARE NOT initialised to anything. This one got me a couple of times until I found out.
So
is the same as
And if you like to rename the implicit return-value RESULT to somethin else, to make your code more self-documenting you can do this also - as Phil showed using a : after the method-definition and before the |
StefanL_38 is not right in saying you need a global variable to read the result of a function/method-call, you do not. You can use a local variable or not even a variable at all - since you can use the return-value as is.
so this will work without a global variable
and this without any variable
I hope this will answer your question.
Enjoy!
Mike