Shop OBEX P1 Docs P2 Docs Learn Events
Are optional parameters possible? — Parallax Forums

Are optional parameters possible?

simonlsimonl Posts: 866
edited 2009-04-12 16:26 in Propeller 1
Hi All,

I'd like to create a method that has optional parameters, like:
PUB myMethod( x, y, z )


Where 'z' is optional; taking a default value if it's omitted in the call. Is this possible?

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Cheers,
Simon

www.norfolkhelicopterclub.com

You'll always have as many take-offs as landings, the trick is to be sure you can take-off again wink.gif
BTW: I type as I'm thinking, so please don't take any offence at my writing style smile.gif

Comments

  • Zap-oZap-o Posts: 452
    edited 2009-04-12 15:20
    Could you just set it to -1, so that in the method it would be ignored only if this condition is true?
  • simonlsimonl Posts: 866
    edited 2009-04-12 15:28
    @Zap-o: That was a rapid response - thanks.

    I guess I could, but the programmer would need to remember to add the -1; in which case they may as well just enter the default value!

    I should explain; I'm trying to write a beginners tutorial to Propeller communications, and I've just come across something where optional parameters would be useful (in a helper object). With optional parameters, the helper methods can default them to the most commonly used values, but be flexible enough to allow non-common values for the more experienced/needy.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Cheers,
    Simon

    www.norfolkhelicopterclub.com

    You'll always have as many take-offs as landings, the trick is to be sure you can take-off again wink.gif
    BTW: I type as I'm thinking, so please don't take any offence at my writing style smile.gif
  • Chuck RiceChuck Rice Posts: 210
    edited 2009-04-12 15:36
    Not that I know of, but you can make multiple methods like:

    methodXY(x,y) <-- This method just calls methodXYZ(x,y,-1)

    methodXYZ(x,y,z)

    I am not sure how expensive that would be in the overhead of nested calls, but it would make it simpler to understand (from the outside).
  • simonlsimonl Posts: 866
    edited 2009-04-12 15:43
    @Chuck Rice: Hey, that's a good plan! I'll give that a try - thanks.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Cheers,
    Simon

    www.norfolkhelicopterclub.com

    You'll always have as many take-offs as landings, the trick is to be sure you can take-off again wink.gif
    BTW: I type as I'm thinking, so please don't take any offence at my writing style smile.gif
  • simonlsimonl Posts: 866
    edited 2009-04-12 15:56
    Ah! But then they'd need to use different method names [noparse]/noparse][noparse]:boohoo:[/noparse

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Cheers,
    Simon

    www.norfolkhelicopterclub.com

    You'll always have as many take-offs as landings, the trick is to be sure you can take-off again wink.gif
    BTW: I type as I'm thinking, so please don't take any offence at my writing style smile.gif
  • Zap-oZap-o Posts: 452
    edited 2009-04-12 16:09
    simonl,

    I think I understand what you are after and frankly I don't think it is possible. You might be able to get around this limit by using a type of global variable. here is an example

    
    var byte default
    
    main
    default := -1 
    
    
    some pub (x , y  )
    
    
    Case default 
       -1 : do woek 
       other :
            do other work
    
    
    
    
  • Chuck RiceChuck Rice Posts: 210
    edited 2009-04-12 16:10
    I know. Limitation of the language I think. The alternative, but might be too advanced for beginners would be to:

    LONG ParameterList[noparse][[/noparse]10]

    PUB

    ParameterList[noparse][[/noparse] 0 ] := x
    ParameterList[noparse][[/noparse] 1 ] := y
    ParameterList[noparse][[/noparse] 2 ] := z

    MethodName(@ParameterList)

    Post Edited (Chuck Rice) : 4/12/2009 5:30:15 PM GMT
  • jazzedjazzed Posts: 11,803
    edited 2009-04-12 16:26
    Hi Simon,

    Here's the approach I use. Given the "parameter" code block below, one can build a method that takes one argument which is an expression of the form: p(v1)+p(v2)+p(vN) and call the "dequeue" in the method to get the input values v1, v2, vN. The number of values are limited to the value of QLEN.


    For example here is a method that uses the "parameter" code block (untested).

    OBJ disp : "tv_text"
    
    PUB main
      printv( p("s")+p("i")+p("m")+p("o")+p("n")+p("!") )
      repeat
    
    PUB printv(arg) | value
    ' print values passed as sum of p(vN)
    
      repeat
        disp.dec(dequeue())
        disp.out(" ")
      while count
      disp.out($d)
    
    
    



    The "parameter" code block (tested):

    PUB p(v)
    {{
    This is the "parameter" method. The user will string together a
    list of method calls to push parameters onto the print queue.
    
    For example: snprintf(@buf, len, string("values %d %d %d\n"),p(1)+p(2)+p(3))
    This will print "values 1 2 3\n" to the buffer.
    
    @param v - value that will replace the printf % format specifier.
    @returns 1. Using a list of p(v)+p(v)+p(v) as the arg to snprintf gives arg count. 
    }}
      enqueue(v)
      return 1
    
    CON
      QLEN = $3f     ' up to 31 parameters
      
    VAR
      long queue[noparse][[/noparse]QLEN+1]
      long tail, head
      
    PRI enqueue(val)
      long[noparse][[/noparse]@queue+head] := val
      head+=4
      head&= QLEN
    
    PRI dequeue : rc
      rc := long[noparse][[/noparse]@queue+tail]
      tail+=4
      tail&= QLEN
      return rc
      
    PRI count
      if head => tail
        result := head-tail
      else
        result := head+(QLEN-tail)
          
    PRI flush
      head := tail
    
    
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    --Steve


    Propalyzer: Propeller PC Logic Analyzer
    http://forums.parallax.com/showthread.php?p=788230
Sign In or Register to comment.