Results 1 to 2 of 2

Thread: Functions that return Arrays

  1. #1
    thinBasic MVPs ReneMiner's Avatar
    Join Date
    Oct 2012
    Blog Entries
    Rep Power

    Functions that return Arrays

    it is possible with a few drenches and quenches already to make a thinbasic function return a result that is an array. The indirect way - passing an array byref to a function - we know all from many functions that put some data for us into a list which is the byref passed array and the function returns additional a count how many of the returned elements in the array are to take serious or is it 0 but dimensioned to a ubound of 1 that we can ignore then.

    When i setup a function as
    Function myFunc(byval param1 as whatever, 
                             byref param2 as Anytype [,
                              ... ]                    ) As lonely Long
    you will see and know from experience the "As lonely Long" returned function-result-parameter never came " As [Array of] Long()" in the past.
    And thoughts as: "oh my god - if returns an array the stupid user has to ready a bucket to receive all numbers. No no no this can not be and anyway there is the byref-option, blah blah blah, lazy excuses only...

    Back to the previous sentence and study the words "... the "As lonely Long" returned function-result-parameter ..." in the sense of Albert Einsteins "NOTHING is faster than the speed of light" - the thing that makes the universe expand in hyper-light-speed is that NO-THING that no one ever saw because it was gone already when the light arrived

    Is it really a parameter? Outside of the parenthesis? Yes it is. the opening parenthesis signals the start of function parameters, the closing parenthesis only ends the list of parameters that can be overloaded by the user and even if we can not see what is on the other side of that equal sign where our function result will end up.
    "...) As Long" tells us the user is ready to receive what is represented in a Long variable.

    Now jump over there in your mind where the user wrote something like
    Dim lResult As Long = myFunc(x,y)
    and think what you could give to his hands here that will rescue him if myFunc would not return the expected 4 bytes but something from 0 * 4 to 2^31*4 bytes -
    that (last words italics, bold) tells you already what could come: a sequence of data - to interpret as not so lonely long - and altogether it's pretty near to the variadic parameters that can be passed into a function,
    specifying "paramName(ANY) as paramType"
    now back down left of the equal sign again where the users result-parameter is waiting- let him borrow the "(ANY)" for a moment or two
    Dim lResult(ANY) As Long = myFunc(x,y)
    'or two
    Redim lResult(ANY) = myFunc(u,v)
    and think
     Function myFunc( byval param1 as whatever,
                              byref param2 as AnyType[,
                             ...]                ) As Long() | As Array[Of  Long  ]
    ' where 
    ...As Long()
    ... As ArrayOf(Long)
    ' or 
    ... As Long Array
    ' in reality means 
    ... As String
    ' the returned value will be a sequence of homogenous, same sized elements
    'that i will grab after ordering and joining them (thats what the function is for) 
    'and i will return that bunch of data and dump it to the users bucket. 
    ' The user knows its 0, 1 or more of Long variables to expect as returned parameter
    'and should provide space for it.
    Now back to the left of equal, we are at Dim, Redim ... initial but (ANY) and lets remember times we used dynamic strings to substitute not available dynamic udt-subelements
    we wrote like
    dim myData(StrptrLen(Strptr(s))\sizeOf(Long)) As Long At Strptr(s)
    there we knew the name of the string-variable s and s was an existing dynamic string for that we had a variable name.
    Now if we have the function name that equals/results a dynamic string as myFunc() above and we use
    Dim|ReDim|Local|Global <varname>(ANY) [As Type] =
    ' clarify : [As Type] can not be when ReDim is used
    clm   '(clear mind)
    Dim|Redim|Local|Global <varname>(ANY) ... =
    focus on the "(ANY) = " and see the user actually orders to "dim" a "variable [As Type]" with unknown count of elements "(ANY)" AT a sequence of bytes that is available through assignment "=" - an expression that follows after evaluation on the right side of the equl sign, thats length divided by SizeOf([As Type]) will determine the count of elements. while Dim <varname>(0) ... makes not much sense, the assignment could be an empty string that would mess it all up. Except if the assignment of an empty string
    to varname(ANY) will SetAt(varname, 0)
    now user can test his result:

    If GetAt(lResult) then 
    ' lResults ubound and content is valid
    End If

    Did you follow? Did you get it? Or did i omit and overlooked an important detail as

    that expression following after "=" is the result of some function that remains valid until?
    Function called again or current function scope terminates?
    can it exist as local variable without a name maybe dimensioned through Dim <varname>(ANY) that would dim varname [As Type] and _varname$ As String in one go and actualy assign _varname$ and create varname virtually upon after assignment on the right hand side ?

    And - did anyone read or understand it at all? Maybe one of those "new" users, barely logged in with a lot of forum privileges?

    No one can see it. Mind it. I am no one too
    running thinBasic in HP Pavillon Desktop 590 a0xxx 8GB Ram AMD A6-9225 Radeon R4 5Compute Cores 2C+3G @2.6GHz

    Windows 11 Enterprise x64 Build 22000.100 (21H2) - "native boot" from .vhdx
    (says the systeminfo. anything else says it were Windows 10 - but hey: thinbasic runs well )

  2. #2
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Brno - Czech Republic
    Blog Entries
    Rep Power
    Hi Rene,

    I understand the idea behind the proposal, I am just probably already too used to passing arrays ByRef.

    What you propose is basically returning array byVal - a copy of data leaves the function and gets copied to the variable.

    It will be slower than byRef - would it be still worth it for you, as a form of syntactical sugar?

    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB

Similar Threads

  1. How to retrieve a return value from thinBasic_FunctionSimpleCall?
    By Michael Hartlef in forum Module SDK (Freebasic version)
    Replies: 6
    Last Post: 10-09-2010, 22:44
  2. FILE_Move unexpected return value
    By AlexOZ in forum thinBasic General
    Replies: 1
    Last Post: 23-05-2008, 06:45
  3. New RETURN keyword change
    By ErosOlmi in forum Suggestions/Ideas discussions
    Replies: 4
    Last Post: 08-01-2008, 04:51
  4. Sorting arrays with built-in sorting functions
    By ErosOlmi in forum Execution speed tests
    Replies: 4
    Last Post: 18-03-2007, 23:05
  5. PC module return values
    By Petr Schreiber in forum Fixed or cleared errors in help material
    Replies: 1
    Last Post: 09-01-2006, 13:48

Posting Permissions

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