TRIO-basic支持函数(强类型)编程,与PLC来相比较的话类似于定义的功能块可以重复调用,和C,C#......等一些高级的编程语言的函数类似。上一次的demo中决定尝试TRIO的函数来做一些例子,以后大家在开发中可以更据自己的实际情况来决定是否使用函数。
下面介绍指令及例子:
FUNCTION
Type:
Function
Syntax:
FUNCTION name([param1 AS type[, param2 AS type[, param3 AS type …. ]]]) 函数 名称(参数1 AS 类型[, 参数2 AS 类型[, 参数3 AS 类型................]]])
Description:
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.
用户定义的函数驻留在函数库中,供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个),应在括号内指定; 相同数据类型的参数可以用逗号分隔。
A return parameter is also optional and should be specified at the end of the line.
返回参数也是可选的,应在行尾指定
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。
Parameters:
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个参数 |
Array parameters:
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库文件。
Local variables:
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不同。
Process variables:
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语句在库文件中声明变量,这些变量对库中的所有函数都是可见的,但是利用库函数的每个进程都维护自己的副本。 变量中包含的数据是持久的; 因此,如果一个函数更改了一个变量,那么另一个函数也会看到更改的值,但只能在同一个过程中。
Returning data to the caller:
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语句。
Examples:
下面是测试的一些简单的函数:
编写的函数库代码:
函数库代码:
'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
若大家有不同的想法或者测试过一些函数等可以在评论区留言分享。
来源:https://www.cnblogs.com/httpcc/p/10748045.html