Announcement

Collapse
No announcement yet.

Declaring all FUNCTION paramaters as OPTIONAL

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Mike Trader
    replied
    So you'd either need to declare it BYVAL or provide a variable when calling.
    I can pass it as a pointer. thats fine too. Thx

    Leave a comment:


  • Michael Mattias
    replied
    You know, you're right about that. I had forgotten there was such a thing as strings.

    Let's just call it a 'senior moment' and forget I ever mentioned it, OK?

    Leave a comment:


  • Kev Peel
    replied
    No I think it should be kept, it's a really handy feature to have and sets PB apart from other languages. In C for instance, you need to define a variable first if you want to pass a parameter by reference (&)

    If that feature wasn't supported, you wouldn't be able to do the following because "szText" will not be a valid location in memory:

    Code:
    SUB DisplayValue(BYREF SZTEXT AS ASCIIZ)
     ? szText
    END SUB
     
    [..]
     
    DisplayValue("TEST")

    Leave a comment:


  • Michael Mattias
    replied
    >PB will "donate" you a free variable if you pass a literal value with a BYREF parameter

    Really?

    I never tried this because it's, well, silly, to pass a literal by reference.

    I would think that should be a compile-time error "May not pass literal parameter by reference.. change to a variable or use explicit override at point of call."

    MCM

    Leave a comment:


  • Kev Peel
    replied
    Yes, I forgot about that

    PB will "donate" you a free variable if you pass a literal value with a BYREF parameter, as opposed to an actual variable. You can even assign a value to it from within the function as well.

    Leave a comment:


  • Michael Mattias
    replied
    An optional BYREF parameter is present when its address is non-zero...
    Code:
    FUNCTION Foo (OPTIONAL X AS LONG)
    
     IF ISTRUE VARPTR(X) THEN
         MSGBOX "X is present and equals" & FORMAT$(X) 
     ELSE
        MSGBOX "X was not passed, or you overrode it with 'BYVAL %NULL' at the point of call"
     END IF

    Leave a comment:


  • Kev Peel
    replied
    From help file under DECLARE - OPTIONAL:

    When a parameter is declared optional, all subsequent parameters in the declaration are optional as well, whether or not they specify an explicit OPTIONAL or OPT directive.
    The problem is that the function is expecting a valid address in GrowSz but is getting NULL when the parameter is omitted. So you'd either need to declare it BYVAL or provide a variable when calling.

    Leave a comment:


  • Mike Trader
    started a topic Declaring all FUNCTION paramaters as OPTIONAL

    Declaring all FUNCTION paramaters as OPTIONAL

    I would like to be able to call an intialization wrapper function in one of two ways. Either with two parameters specified or with no parameters specified.
    CALL InitArrays( 1024, 512 )
    or just
    CALL InitArrays

    in the second case default values inside the function would be used.

    I can declare and compile the function like this:
    Code:
    FUNCTION InitArray( OPTIONAL BYVAL InitSz AS DWORD, GrowSz AS DWORD ) AS DWORD
    
        IF InitSz = 0 THEN InitSz = 999
        IF GrowSz = 0 THEN GrowSz = 99
    
      FUNCTION = Init( InitSz, GrowSz ) '
    
    END FUNCTION
    This GPF's
    Manual says:
    When optional parameters are omitted in the calling code, the stack area normally reserved for those parameters is zero-filled, so BYVAL parameters default to the value 0
    but doesn't explicitly mention declaring ALL parameters as OPTIONAL.
    Is it possible?
Working...
X