Shop OBEX P1 Docs P2 Docs Learn Events
Help understanding how to handle a return value — Parallax Forums

Help understanding how to handle a return value

Don MDon M Posts: 1,653
edited 2012-09-09 13:09 in Propeller 1
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:
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

  • Don MDon M Posts: 1,653
    edited 2012-09-09 08:34
    A little more clarification.

    My main code might be this:
    Pub  Main
    
      some code
      some_method
      some more code
    
    

    So after I return from some_method how do I react to the value returned?
  • StefanL38StefanL38 Posts: 2,292
    edited 2012-09-09 08:51
    HI Don,

    for this yofine a global long
    The call for your method is then used as assignemt to this variable see code example below
    VAR
      long MyGlobalVarForResponse
    
    PUB Main
      MyGlobalVarForResponse := some_method
    
    
    if MyGlobalVarForResponse == 0
      Do that
    else
      Do this
    
    
    pub some_method  |  response    some code   if this happens     response := 1   else     response := 0    return response
    

    best regards
    Stefan
  • JonnyMacJonnyMac Posts: 9,194
    edited 2012-09-09 08:52
    Easy-peazy:
    someValue := some_method
    


    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.
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2012-09-09 10:03
    Don_M,

    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 (|).
    pub some_method  :  response
    
      some code
      if this happens
        response := 1
      else
        response := 0
    

    -Phil
  • Don MDon M Posts: 1,653
    edited 2012-09-09 12:47
    Thanks guys. I have tried all 3 implementations and now understand.
  • msrobotsmsrobots Posts: 3,709
    edited 2012-09-09 13:09
    A method in spin is actually a function returning a long value. Allways.

    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
    pub some_method  |  response
    
      some code
      if this happens
        response := 1
      else
        response := 0
    
      return response 
    

    is the same as
    pub some_method  
    
      some code
      if this happens
        RESULT:= 1
      else
        RESULT:= 0
    

    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 |

    pub some_method :  response
    
      some code
      if this happens
        response := 1
      else
        response := 0
    

    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
    PUB Main | myLocalVarForResponse
      myLocalVarForResponse:= some_method
    
    if myLocalVarForResponse == 0
      Do that
    else
      Do this
    
    

    and this without any variable
    PUB Main 
    if some_method== 0
      Do that
    else
      Do this
    
    

    I hope this will answer your question.

    Enjoy!

    Mike
Sign In or Register to comment.