TRIO-basic支持函數(強類型)編程,與PLC來相比較的話相似於定義的功能塊能夠重複調用,和C,C#......等一些高級的編程語言的函數相似。上一次的demo中決定嘗試TRIO的函數來作一些例子,之後你們在開發中能夠更據本身的實際狀況來決定是否使用函數。編程
下面介紹指令及例子:數組
Function app
FUNCTION name([param1 AS type[, param2 AS type[, param3 AS type …. ]]]) 函數 名稱(參數1 AS 類型[, 參數2 AS 類型[, 參數3 AS 類型................]]])編程語言
User defined function residing in a function library for use by any BASIC program in the Motion Coordinator multi-tasking system. A function must as a minimum have a name and finish with the ENDFUNC keyword. The contents of the function can take any BASIC commands provided that they work in the context of the required function operation. ide
用戶定義的函數駐留在函數庫中,供Motion Coordinator多任務系統中的任何BASIC程序使用。 函數必須至少具備名稱並以ENDFUNC關鍵字結束。 函數的內容能夠採用任何BASIC命令,只要它們在所需的函數操做的上下文中工做。函數
FUNCTION requires a matching ENDFUNC keyword to complete a function definition. Parameters are optional (maximum of 16) and should be specified within parentheses; parameters of the same data type can be separated by commas. 測試
FUNCTION須要匹配的ENDFUNC關鍵字才能完成函數定義。 參數是可選的(最多16個),應在括號內指定; 相同數據類型的參數能夠用逗號分隔。ui
A return parameter is also optional and should be specified at the end of the line. this
返回參數也是可選的,應在行尾指定spa
FUNCTION may be used only within a Function Library.
FUNCTION只能在函數庫中使用。
BASIC Library files support a smaller subset of commands compared with standard program files. For example, GOSUB and GOTO are not supported within a BASIC Library file.
Library functions can have a nested hierarchy of calls to other library functions to a maximum depth of 5 levels, the maximum number of parameters that can traverse through this hierarchy is 200.
庫函數能夠具備對其餘庫函數的嵌套層次結構,最大深度爲5級,能夠遍歷此層次結構的最大參數數爲200。
param1: |
First optional parameter to be passed into the function 要傳遞給函數的第一個可選參數 |
param2: |
The second parameter if required 若是須要第二個參數 |
param3: |
The third parameter if required 若是須要第三個參數 |
paramN: |
Up to 16 parameters may be passed 最多能夠傳16個參數 |
Support for passing arrays into functions is available and enabled by using optional brackets () after the parameter data type, note that there is no need to specify array dimensions, for example;
經過在參數數據類型以後使用可選的括號(),能夠支持將數組傳遞給函數,例如,注意不須要指定數組維度;
FUNCTION f1(data AS INTEGER(), b AS FLOAT) AS BOOLEAN
Array attributes are provided to support generic arrays. Therefore a function can be re-used with arrays of varying dimensions.
提供數組屬性以支持通用數組。 所以,函數能夠與不一樣維度的數組一塊兒使用。
<array name>.dims – integer value indicating the number of array dimensions
<array name> .dims - 表示數組維數的整數值
Note that array attributes may be used within any program and are not restricted to BASIC library files.
請注意,數組屬性能夠在任何程序中使用,而且不限於BASIC庫文件。
Functions can declare their own local variables using DIM statements, for example
例如,函數可使用DIM語句聲明本身的局部變量
FUNCTION myfunc AS INTEGER
DIM a, b AS INTEGER
DIM t AS TARGET
DIM x AS FLOAT(5)
… ENDFUNC
Local variable names can be reused within multiple functions in the same library file. A variable named xx in one function is different to xx in another function.
能夠在同一庫文件中的多個函數中重用局部變量名。 一個函數中名爲xx的變量與另外一個函數中的xx不一樣。
Variables can be declared within a library file using DIM statements outside of the context of FUNCTION..ENDFUNC structures, these variables are visible to all functions within the library but each process that utilises the library functions maintains its own copy. The data contained within the variables is persistent; hence if one function changes a variable then another function will also see the changed value, but only within the same process.
可使用FUNCTION..ENDFUNC結構上下文以外的DIM語句在庫文件中聲明變量,這些變量對庫中的全部函數都是可見的,可是利用庫函數的每一個進程都維護本身的副本。 變量中包含的數據是持久的; 所以,若是一個函數更改了一個變量,那麼另外一個函數也會看到更改的值,但只能在同一個過程當中。
The keyword RETURN is not new but behaves differently within the context of a function. Used within a function this command returns execution back to the caller but is also used to return data back to the caller when the function has a return data type defined. Multiple RETURN statements are permitted within a single function definition.
關鍵字RETURN不是新的,但在函數的上下文中表現不一樣。 在函數中使用此命令將執行返回給調用者,但也用於在函數定義了返回數據類型時將數據返回給調用者。 在單個函數定義中容許多個RETURN語句。
下面是測試的一些簡單的函數:
編寫的函數庫代碼:
函數庫代碼:
'TRIO-basic函數庫 'printf_function 輸出函數 FUNCTION printf() PRINT#0,"hello,world" ENDFUNC 'sun function 加法函數 num1 num2 整型的值 FUNCTION sum(num1 AS INTEGER,num2 AS INTEGER) DIM sum_value AS INTEGER sum_value = num1+num2 PRINT#0,sum_value ENDFUNC '比較函數 num1 num2 整型的值 FUNCTION return_fun(num1 AS INTEGER,num2 AS INTEGER) AS BOOLEAN DIM return_value AS BOOLEAN IF num1 > num2 THEN return_value = TRUE ELSE return_value = FALSE ENDIF RETURN return_value ENDFUNC '遞歸函數 value 輸入的值 FUNCTION d_fun(value AS INTEGER) AS INTEGER IF value <= 1 THEN RETURN 1 ELSE RETURN value + d_fun(value - 1) ENDIF ENDFUNC
調用函數的代碼:
DIM return_value,d_value AS INTEGER '調用函數 printf() 'hello world PRINT CHR(10) sum(5,8) '13 PRINT CHR(10) return_value = return_fun(0,4) '比較函數 PRINT return_value 'false PRINT CHR(10) d_value = d_fun(5) PRINT d_value '15
若你們有不一樣的想法或者測試過一些函數等能夠在評論區留言分享。