0

Building your own Function?

Hello,

Please van you give some code examples how to define a custom function? 

Forum description: ++++++

Creating User defined functions locally with the function command:

function name(argument:data type,...)  
do <script instructions> end

Note that a custom function can appear anywhere in a script, but they must come before any call to them, including calling another custom function.

 

Arjan G

15 replies

null
    • Tacho
    • 6 yrs ago
    • Reported - view
    • Arjan_Groen
    • 6 yrs ago
    • Reported - view

    Yes, that's the reason of my question! It is'nt clear how to implement!

     

    Arjan

    • Sean
    • 6 yrs ago
    • Reported - view

    function fnVolume(len1 : number,len2 : number,len3 : number) do
    len1 * len2 * len3
    end;
    fnVolume('Length 1', 'Length 2', 'Length 3')

    'Length 1', Length 2', 'Length 3' are database fields

    function fnConcatenate(FirstName : text,LastName : text) do
    FirstName + " " + LastName
    end;
    fnConcatenate('First Name', 'Last Name')

    'First Name', 'Last Name' are database fields

    • Arjan_Groen
    • 6 yrs ago
    • Reported - view

    Thank you for the exampes and the Quick respons! 

    Arjan

    • ioannis.1
    • 4 yrs ago
    • Reported - view

    Hi 

    I am looking in the manual and I cannot find the possible  data types that can be used in a user defined function as in

    function name(argument:data type,...)

    By trial and error I found the following:

    function name(argument : number, argument : text, argument : date )

    Any other ideas?

    I am trying to pass a table name -- 'Table Name' as a variable in a function

    Thank you

    • Sean
    • 4 yrs ago
    • Reported - view

    The argument types are limited as you found out. What do you want to do with the table name once it is passed to the function?

    • ioannis.1
    • 4 yrs ago
    • Reported - view

    I would like to use in (select 'Table Name' )

    I am trying to make a function for searching data in all databases

    Somethng more sofisticated than the built-in search

    So i would like to be able to use one function for all tables instead of having to use one function for each

    Thank you

    • Sean
    • 4 yrs ago
    • Reported - view

    You can't use a variable in place of the table name when you use the select statement. If you want to use a function that you can pass a table name into, you can use a switch statement to test the input and then execute the appropriate select statement. For example...

     

    function getTable(tblName : text) do

    switch tblName do

    case "Table1":

    select Table1

    case "Table2":

    select Table2

    .

    .

    .

    case "TableN":

    select TableN

    end

    end

     

    You can use a Text field as the input for the function as well.

    • ioannis.1
    • 4 yrs ago
    • Reported - view

    Thank you for the answer 

    I am trying to use it in simple example but cannot figure exactly the syntax

    let tblnm := "TABLE";
    switch tblnm do
    case "TABLE":
    (select 'Remedies')
    end;

    "TABLE".[name = "NINOX"]

    'Remedies' is the name of the table that exists and 'TABLE' is the one i would like to replace 

    • ioannis.1
    • 4 yrs ago
    • Reported - view

    I think I managed the syntax

    function getTable(tblnm : text) do
    switch tblnm do
    case "remedies":
    (select Remedies)
    case "rajan sankaran collection":
    (select 'Rajan SANKARAN')
    end
    end;
    let tblnm := "rajan sankaran collection";
    let tbl := getTable(tblnm);
    for i in tbl do
    tbl.Abr
    end

    Thre is a problem though. THE RESULT IS WRONG!!!!!!

    It will not display the field  named 'Abr' but another field  named 'Short'

    • Sean
    • 4 yrs ago
    • Reported - view

    When you assign the result of a select statement to a variable, the variable will be an array not a proxy name for the table. I can't get my version of tbl.Abr to work at all. The editor shows Field not found: Name (my version of Abr). Since tbl is an array, you would access the array elements using item(tbl, i).Abr, but I still get the same error message in the editor.

    • ioannis.1
    • 4 yrs ago
    • Reported - view

    Thank you for your time 

    I think this is a bug because this version works

    function gtTbl(tblnm : text) do
    switch tblnm do
    case "rajan sankaran collection":
    (select 'Rajan SANKARAN')
    end
    end;
    let tblnm := "rajan sankaran collection";
    let tbl := gtTbl(tblnm);
    for i in tbl do
    i.Abr
    end

    If I add a second case item in the function before this one the hole thing breaks

    function gtTbl(tblnm : text) do
    switch tblnm do

    case "Example":
    (select 'Table')
    case "rajan sankaran collection":
    (select 'Rajan SANKARAN')
    end

    If i add the same case item but  after, then the code works perfectly, but always only for the first item in the row

    • Gunther.1
    • 4 yrs ago
    • Reported - view

    You can use:

     

    function myfunction(TempTable: tblnm) do

    let tb1 := select TempTable ...

    The point is to use the real Tablename as a format!

    • ioannis.1
    • 4 yrs ago
    • Reported - view

    Hi Gunther

    Thank you for your input

    I am not sure what do you mean by "use the real Tablename as a format!"

    • Gunther.1
    • 4 yrs ago
    • Reported - view

    Hi,

     

    let tblnm: ="rajan sankaran collection"

Content aside

  • 4 yrs agoLast active
  • 15Replies
  • 4835Views