0

Types of parameters for a private function

What types of parameters can be used for a private function?

text -> ok

number -> ok

array -> ?

record -> ?

date -> ?

table -> ?

and more?

10 replies

null
    • Admininstrador
    • 5 yrs ago
    • Reported - view

    Please someone reply to this. I'm also trying to create a function with an array parameter but dont know how

    • Sean
    • 5 yrs ago
    • Reported - view

    As far as I can tell the only data types you can use in a user-defined function are the following...

     

    Number, Text, Date, Time and DateTime

     

    If you try use array, you will get an "Unknown type" error like this...

     

    Screen Shot 2018-12-14 at 6.57.06 PM

    • Sean
    • 5 yrs ago
    • Reported - view

    As long as an array is defined before the user-defined function, you can manipulate the array inside the function.

     

    Screen Shot 2018-12-16 at 11.39.52 AM

    • Sean
    • 5 yrs ago
    • Reported - view

    So, I had some time to kill because the retards in Birmingham, Al closed two lanes of traffic on I-459 for road construction. They could have sent traffic through downtown this being a Sunday and all!!! Here's a version of a user-defined function that replaces a single array item...

     

    let myArray := ["Python", "Ruby", "Java", "C++"];
    function replaceArrayItem(indexItem: text, index: number) do
       switch index do
       case 0:
          myArray := [indexItem, item(myArray, 1), item(myArray, 2), item(myArray, 3)]
       case 1:
          myArray := [item(myArray, 0), indexItem, item(myArray, 2), item(myArray, 3)]
       case 2:
          myArray := [item(myArray, 0), item(myArray, 1), indexItem, item(myArray, 3)]
       case 3:
          myArray := [item(myArray, 0), item(myArray, 1), item(myArray, 2), indexItem]
       end
    end;
    replaceArrayItem("Swift", 3)
     

     

    The screenshot cuts off some of the code...

     

    Screen Shot 2018-12-16 at 2.23.48 PM

    • Sean
    • 5 yrs ago
    • Reported - view

    Ooops, to get the output...

     

    replaceArrayItem("Swift", 3);

    myArray 

     

    then click run

    • Tim
    • 5 yrs ago
    • Reported - view

    I can't remember where I first saw this, but functions can also accept and return 'boolean' as an argument type, as in:

    function myfunc(arg: boolean) do
        if arg then bla bla bla end
    end

    Also, experimentation shows that functions can return more types than they can accept. For example, a function can actually return a record:

    function getrecord(key : text) do
        first(select Locations where Location = key)
    end;
    popupRecord(getrecord("keyvalue"))

    • Tim
    • 5 yrs ago
    • Reported - view

    If you really need to pass an array into a function (e.g. into one of the new user-defined global functions), one approach I've used is to pass a string argument with a known convention for delimiting array values, then splitting that argument into its array elements inside the function (using the 'split' function).

     

    function test(argstring : text) do
        let args := split(argstring, "|");
        cnt(args)
    end;
    test("abc|def|123")

     

    Of course, the usefulness of this depends upon whether you can settle on a delimiter that you know will NEVER be contained within an array element.

    BTW, a user-defined function can RETURN an array:

     

    function createarray(arraystring : text) do
        split(arraystring, ", ")
    end;
    let array := createarray('Array In')

    • Sean
    • 5 yrs ago
    • Reported - view

    Yep, I just tested that. It's too bad the concat() function doesen't have a delimiter argument like the split() function.

    • Tim
    • 5 yrs ago
    • Reported - view

    A delimiter argument for the concat function would make a lot of sense. In the meantime, I've used the replace() function to replace the default ", " delimiter in the concatenated string with the delimiter of my choice. Of course, you still have to make sure ", " does not appear in any of the actual array elements.

    • Admininstrador
    • 5 yrs ago
    • Reported - view

    This all has been very helpful. Now with global functions we can create these missing pieces

Content aside

  • 5 yrs agoLast active
  • 10Replies
  • 5964Views