Shop OBEX P1 Docs P2 Docs Learn Events
Help with using the result value returned from a method — Parallax Forums

Help with using the result value returned from a method

DRMorrisonDRMorrison Posts: 81
edited 2014-07-10 08:54 in Propeller 1
Hi there,
I'm trying to figure out how to use the result in the calling method that is returned from the called method--hope that's clear.
This should help. This is an example only, but is representative of my code--can't get to it right now.
PUB Main
 Start_Something
 <do some other things>


PUB Start_Something : Fail
 <check some pins>
 if SomePin == 0
  <things are good here>
 else
  Fail := 1

How do I check the result returned from the Start_Something method in the Main method?
I was able to find out how to set the result to be returned in the manual, but not how to use it once returned.

Thank you, Daniel

Comments

  • kuronekokuroneko Posts: 3,623
    edited 2014-07-09 22:54
    Either by using
    if start_something
        ' response
    
    or you can assign the result to some other variable and use it later:
    value := start_something
    
  • DRMorrisonDRMorrison Posts: 81
    edited 2014-07-09 23:09
    So, would it look like this?

    PUB Main
     if Start_Something == 1
      <respond to returned result being 1>
     <do some other things>
    
    
    
    
    PUB Start_Something : Fail
     <check some pins>
     if SomePin == 0
      return
     else
      Fail := 1
    
    
    
  • kuronekokuroneko Posts: 3,623
    edited 2014-07-09 23:14
    DRMorrison wrote: »
    So, would it look like this?
    PUB Main
     if Start_Something == 1
      <respond to returned result being 1>
     <do some other things>
    
    Basically yes, == 1 may be optional in this case (any non-zero value is promoted to TRUE). Note that <do some other things> will be executed regardless of the returned result. If you want to limit it to the zero response then you'll have to use if/else.
  • DRMorrisonDRMorrison Posts: 81
    edited 2014-07-09 23:28
    Thank you. I feel that I understand that clearly.

    Yes, <do some other things> will be executed, along with many other lines following that statement.
    I'm looking for a way to determine in the Main method whether or not there was success in the
    Start_Something method. I suppose that I could have used a global variable, but why not use
    the return value, since it's there.

    And just for clarification, would this also work?
    PUB Main
     if Start_Something
      <respond to returned result being TRUE>
     <do some other things>
     <do some more things>
     <do many more things>
    
    
    
    
    PUB Start_Something 
     <check some pins>
     if SomePin == 0
      <things are good here>
     else
      return TRUE
    
  • kuronekokuroneko Posts: 3,623
    edited 2014-07-10 00:16
    Sure, why not? Depending on what <things are good here> involves you could get away with:
    PUB Start_Something 
     <check some pins>
     return SomePin <> 0
    
    And just to re-iterate, you don't have to return TRUE, any non-zero value returned from method will be accepted by an if method as TRUE.
  • DRMorrisonDRMorrison Posts: 81
    edited 2014-07-10 08:54
    Thank you for the lesson.

    Daniel
Sign In or Register to comment.