Page 1 of 2 12 LastLast
Results 1 to 10 of 20

Thread: unlimited function-parameters?

  1. #1
    thinBasic MVPs
    Join Date
    Oct 2012
    Location
    Germany
    Age
    54
    Posts
    1,544
    Rep Power
    172

    unlimited function-parameters?

    is it possible in thinBasic to call a function with a dynamic count of function-parameters?
    ( all of the same, fixed-size type ).

    Somewhat like MKL$( param1[, param2[, param3,...]] ) ?

    or is the only way to pass a list (string) made up like MKL$, MKBYT$, MKDWD$, Memory_Get for UDt etc. and to place some layover there to read it out. But i don't want to create a string as first to pass the list of parameters to the function...

    short example what i mean:

    ' i want to pass maybe 3, but maybe 33 or 333 longs, so have to do it alike
    
    myFunc( MKL$(123, 234, 345) )
    ' or
    myFunc( MKL$(123, 234, 345, 456, 567, 678,...) )
    
    Function myFunc( Byval sLongs As String )
    
       Local lLong(StrPtrlen(StrPtr(sLongs))/4) As Long At Strptr(sLongs)
    
       ' now the function has a list of long values available
       ' UBound(lLong) tells how many
    
    End Function
    
    and i want to do it without the MKL$, so to say- instant array alike

    myFunc( 123, 234, 345 )
    
    myFunc( 123, 234, 345, 456, 567, 678,... )
    
    Function myFunc( ByVal lLong() As Long )
     
    End Function
    
    no doubt: this "ByVal lLong()" has to be the only - or last passed parameter.
    None may follow.

    Else it would need a syntax like on array-assignement using brackets here
    (which i don't like)
    myFunc( [123, 234, 345, 456, 567, 678,...] )' including [] brackets passed
    
    In the other direction would be very interesting if one could automatically create a list from a function-result like this:

    Function myFunc(Byval parameters As whatever) AS STRING
    
      Local lResult(123) As Long
      
      Function = Memory_Get( Varptr(lResult(1)), SizeOf(lResult) )
    
    'EDIT:  ' cool would be "Function = Array All lResult" instead
    
    End Function
    
    
    '------------------------------------------------
    Dim lLong() AS LONG = myFunc(somedata)
    
    ' when assigning a STRING (on dimensioning) to something with
    ' empty parenthesis that is NOT a STRING it could instantly create fitting array...
    
    Redim lLong() = myFunc( someOtherData ) '...
    
    not far from returning dynamic arrays as function-result -
    but the intention is indeed that i want to pass a list of same-type-parameters where nobody knows in advance how many.
    Last edited by ReneMiner; 22-10-2014 at 13:43.
    I think there are missing some Forum-sections as beta-testing and support

  2. #2
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    57
    Posts
    8,793
    Rep Power
    10
    Here an example of optional parameters passing but I doubt it can satisfy your request:
    
    MsgBox 0, "Sum of passed parameters: " & MySum(1,2,3) & $CRLF + "(9th and 10th parameter are predefined to be 100 and 1000)"
    
    
    Function MySum( 
                    N01 As Ext  ,
           Optional                  '---From here all parameters will be considered optional
                    N02 As Ext  , 
                    N03 As Ext  , 
                    N04 As Ext  , 
                    N05 As Ext  , 
                    N06 As Ext  , 
                    N07 As Ext  , 
                    N08 As Ext  , 
                    N09 As Long =  100 ,   '---Default value
                    N10 As Long = 1000     '---Default value
                ) As Ext 
    
    
      
        Function =  N01 + 
                    N02 + 
                    N03 + 
                    N04 + N05 + N06 + N07 + N08 + N09 + N10
                    
        MsgBox 0, "We are inside function: "            & Function_Name     & $CRLF & 
                  "Function has been declared to have " & Function_NParams  & " parameters."  & $CRLF & 
                  "Function has been called with "      & Function_CParams  & " parameters."
        
    End Function
    
    Consider ThinBasic function parameters are limited to 32

    What you need is some sort of variadic function, a function that can accepts an undefined number of parameters you can define at run time.
    ThinBASIC has no way to define a variadic function, unless you create a compiled module and create your own function parsing an infinite number of parameters.
    For example the following is MKL$() function developed into a module (in this case thnCore main engine module) using Power Basic (but can be easily developed using FreeBasic or other compiled languages using thinBasic SDK)
      '------------------------------------------------------------------------------
      Function Exec_MKL() As String
      '------------------------------------------------------------------------------
      'Syntax: s = MKL$(Number [, ...])
      '------------------------------------------------------------------------------
        Local eNumber As Ext                                 
        Local pp      As Long
        Local sTemp   As String
        
        pp = thinBasic_CheckOpenParens_Optional
        Do
          thinBasic_ParseNumber eNumber
          sTemp = sTemp & Mkl$(eNumber)
        Loop While thinBasic_CheckComma_Optional
        
        If pp Then thinBasic_CheckCloseParens_Mandatory
        Function = sTemp
    
    
      End Function
    
    As you can see an infinite number of numeric expressions can be parsed by ThinBASIC engine until it will encounter a comma.

    I think the best option you have now is using your first example:
     myFunc( MKL$(123, 234, 345) )
    ' or
    myFunc( MKL$(123, 234, 345, 456, 567, 678) )
     
    Function myFunc( Byval sLongs As String )
     
       Local lLong(StrPtrlen(StrPtr(sLongs))/4) As Long At Strptr(sLongs)
     
       ' now the function has a list of long values available
       ' UBound(lLong) tells how many
     
    End Function
    
    www.thinbasic.com | www.thinbasic.com/community/ | help.thinbasic.com
    Windows 10 Pro for Workstations 64bit - 32 GB - Intel(R) Xeon(R) W-10855M CPU @ 2.80GHz - NVIDIA Quadro RTX 3000

  3. #3
    AFAIK modern Oxygen supports two notations for variadic functions with unlimited number of arguments:

    function foo(...)

    and

    function foo(sys n, ...)

    wherein access to the variadic args is provided through the internal array called param[].

    The former one looks more formal and suitable in case the first argument would supply some additional information on the other arguments actually passed or when such information is known to the function from some other, e.g. global, sources. The latter one seems more handy when one needs to iterate somehow through all the arguments actually passed, in which case n should be the count of elements in param[] since Oxygen does not support the concept of an array's UBound.

    For example:

    sub f(n, ...)
    indexbase 0
    ' n is in fact param[0]
    for x = 1 to n
    print param[x]
    next
    end sub


    Perhaps this new feature and the O2-to-TB interface can help the user bypass the existing limitation.
    Last edited by mike lobanovsky; 22-10-2014 at 09:43.
    Mike
    (3.6GHz i5 Core Quad w/ 16GB RAM, nVidia GTX 1060Ti w/ 6GB VRAM, x64 Windows 7 Ultimate Sp1)

  4. #4
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    57
    Posts
    8,793
    Rep Power
    10
    Not just round the corner but I had an inspiration and I would like to fix some ideas for future thinBasic feature.

    What about something like that: No variadic functions but variadic parameters.

    Variadic parameters are defined as standard arrays passed BYVAL ad indicating 3 points between round parentheses.
    Example of a function having 2 standard parameters and 2 variadic parameters:
    Function MyVariadic(ByVal Whatever As Long, ByVal FirstVariadicParam(...) As Ext, ByVal SecondVariadicParam(...) As String, ByVal x as Long) As Long
      '...any code
      '...FirstVariadicParam and SecondVariadicParam will be just like LOCAL arrays already filled with values
    End Function
    
    FirstVariadicParam and SecondVariadicParam will be just like LOCAL arrays already filled with values and on which programmer will be able to operate like any other local array.



    Calling such a function will be like calling a standard functions but passing variadic parameters MUST be enclosed into round parentheses.
    Example using above function:
    MyVariadic(123, (32.8, val("123"), 100), ("ABC", "ZZZ", STR$(Sin(0.4)) ), 456 )
    

    What do you think? Tell me that you like and sooner or later I will do it!
    Last edited by ErosOlmi; 22-10-2014 at 12:37.
    www.thinbasic.com | www.thinbasic.com/community/ | help.thinbasic.com
    Windows 10 Pro for Workstations 64bit - 32 GB - Intel(R) Xeon(R) W-10855M CPU @ 2.80GHz - NVIDIA Quadro RTX 3000

  5. #5
    thinBasic MVPs
    Join Date
    Oct 2012
    Location
    Germany
    Age
    54
    Posts
    1,544
    Rep Power
    172
    that "variadic"-expression is driving me nuts in the meantime and sounds like the "heuristic" known from virus-scanners...

    i don't care how it's called- it enables us to pass an array or a list of values without having to create local variables in advance to pass them to a function neither do we have to perform half a dozen type-casts? cool.

    If it would work into the other direction too were fantastic... like

    local myVariablabala(...) Like sType = myFunction()
    
    but why the dots? wouldn't just empty parents [or brackets] serve?

    or

    myNum(%) - numeric list
    myText($) - string list
    Last edited by ReneMiner; 22-10-2014 at 13:06.
    I think there are missing some Forum-sections as beta-testing and support

  6. #6
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    57
    Posts
    8,793
    Rep Power
    10
    I need the dots into function declaration to understand that a parameter is variadic.
    I need round parentheses to surround elements passed to variadic parameter when calling the function because I need to distinguish between normal parameter sequence and elements of a variadic parameter.

    Working the other way round ... is something completely different.
    On step after another.
    www.thinbasic.com | www.thinbasic.com/community/ | help.thinbasic.com
    Windows 10 Pro for Workstations 64bit - 32 GB - Intel(R) Xeon(R) W-10855M CPU @ 2.80GHz - NVIDIA Quadro RTX 3000

  7. #7
    thinBasic MVPs
    Join Date
    Oct 2012
    Location
    Germany
    Age
    54
    Posts
    1,544
    Rep Power
    172
    just for interest, would it also be possible to pass an array at once to fill a variadic function-parameter?

    like
     
    Function myFunc( Byval passedItems(...) As Long )
    '...
    End Function
    
    Dim myArray(123) As Long
    
    myFunc( ( Array All myArray ) )  
    
     ' that syntax does not exist but the keywords do...
    
    Last edited by ReneMiner; 22-10-2014 at 13:50.
    I think there are missing some Forum-sections as beta-testing and support

  8. #8
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    57
    Posts
    8,793
    Rep Power
    10
    Yes it can be done.
    It would be contrary to your request not to have to declare a variable but can be done.

    First I need to develop the main functionality.
    If the base idea is there, a lot of options can be added later.
    www.thinbasic.com | www.thinbasic.com/community/ | help.thinbasic.com
    Windows 10 Pro for Workstations 64bit - 32 GB - Intel(R) Xeon(R) W-10855M CPU @ 2.80GHz - NVIDIA Quadro RTX 3000

  9. #9
    thinBasic MVPs
    Join Date
    Oct 2012
    Location
    Germany
    Age
    54
    Posts
    1,544
    Rep Power
    172
    Quote Originally Posted by ErosOlmi View Post
    ...
    It would be contrary to your request not to have to declare a variable but can be done....
    why? i do as usual some virtual layover onto some array@heap
    - that data exists already so no allocation-time
    I think there are missing some Forum-sections as beta-testing and support

  10. #10
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    57
    Posts
    8,793
    Rep Power
    10
    OK, I could not resist !
    I think I've already developed VARIADIC function parameters ... so far ONLY FOR NUMERIC parameters.

    Attached a new test thinCore.dll (thinBasic Core Engine).
    To test it extract and copy thinCore.dll into \thinBasic\ directory replacing your current one.
    BE SURE TO HAVE thinBasic 1.9.14 already installed.

    I HAD TO abandon the idea to use ... (ellipsis) to indicate variadic parameters and use (ANY) clause. If someone has a better idea, I'm open to try to change this.

    See example below:
    Uses "Console"
    
    
    Function Variadic1(FirstVariadicParam(Any) As Ext, sBuffer As String) As Long
      PrintL "From inside " & Function_Name
      PrintL "Variadic parameter 'FirstVariadicParam' number of elements:", UBound(FirstVariadicParam)
      PrintL "----------------------------------------------------------------------------"
    
    
      PrintL "Listing all values:"
      For lCount As Long = 1 To UBound(FirstVariadicParam)
        PrintL " - Element", lCount, "has value of", FirstVariadicParam(lCount)
      Next 
      PrintL
      
      Function = UBound(FirstVariadicParam)
    
    
    End Function
    
    
    Randomize Timer
    '---Because first parameter is variadic, pass any number of numeric expressions in surrounding round brackets
    PrintL "Function exit value", Variadic1((123, 456, 789, Sin(Rnd), Cos(Rnd)), "ABCDE")
    
    
    WaitKey
    
    Next days I will go on adding variadic parameter functionality also to other data types: strings, variants, possibly UDT

    Ciao
    Eros
    Attached Files Attached Files
    Last edited by ErosOlmi; 22-10-2014 at 22:45.
    www.thinbasic.com | www.thinbasic.com/community/ | help.thinbasic.com
    Windows 10 Pro for Workstations 64bit - 32 GB - Intel(R) Xeon(R) W-10855M CPU @ 2.80GHz - NVIDIA Quadro RTX 3000

Page 1 of 2 12 LastLast

Similar Threads

  1. Multidimensional indexing in unlimited dimensions
    By ReneMiner in forum General purpose scripts
    Replies: 1
    Last Post: 01-12-2015, 21:52
  2. Sub/Function-Optional-parameters
    By ReneMiner in forum Fixed or cleared errors in help material
    Replies: 6
    Last Post: 16-06-2013, 11:28
  3. Do you know: Passing UDT parameters
    By dcromley in forum Do you know ...
    Replies: 1
    Last Post: 28-04-2009, 22:09
  4. Default values for function parameters
    By ErosOlmi in forum thinBasic vaporware
    Replies: 2
    Last Post: 19-06-2007, 10:20

Members who have read this thread: 0

There are no members to list at the moment.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •