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.

  与标准程序文件相比,BASIC库文件支持较小的命令子集。 例如,BASIC库文件中不支持GOSUB和GOTO。

  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  - 表示数组维数的整数值

<array name>.dimsize(n) – integer value indicating the length of the specified dimension

<array name> .dimsize(n) - 指示指定维度长度的整数值

  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#,"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#,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 <= THEN
RETURN
ELSE
RETURN value + d_fun(value - )
ENDIF
ENDFUNC

调用函数的代码:

DIM return_value,d_value AS INTEGER

'调用函数
printf() 'hello world PRINT CHR() sum(,) ' PRINT CHR() return_value = return_fun(,) '比较函数
PRINT return_value 'false PRINT CHR() d_value = d_fun()
PRINT d_value '

若大家有不同的想法或者测试过一些函数等可以在评论区留言分享。

TRIO-basic指令--函数FUNCTION的更多相关文章

  1. Javascript自执行匿名函数(function() { })()的原理分析

    匿名函数指没有指定函数名或指针的函数,自执行匿名函数只是其中一种,下文中称这种函数为:自执行函数 下面是一个最常见的自执行函数: // 传统匿名函数 (function() { alert('hell ...

  2. JavaScript自运行函数(function(){})()的理解

    今天打开JQuery源文件(jquery-1.8.3), 看到JQuery的初始化过程是这样的 (function( window, undefined ) { // .... })( window ...

  3. 深入理解javascript中的立即执行函数(function(){…})()

    投稿:junjie 字体:[增加 减小] 类型:转载 时间:2014-06-12 我要评论 这篇文章主要介绍了深入理解javascript中的立即执行函数,立即执行函数也叫立即调用函数,通常它的写法是 ...

  4. javaScript的函数(Function)对象的声明(@包括函数声明和函数表达式)

    写作缘由: 平时再用js写函数的时候,一般都是以惯例 function fn () {} 的方式来声明一个函数,在阅读一些优秀插件的时候又不免见到 var fn = function () {} 这种 ...

  5. 函数(Function)作用域 / 远程函数执行

    函数跟变量一样也是有作用域的:Global.Script.Local.Private Global:作用于整个PowerShell会话,只要PowerShell会话不结束,被Global修饰的变量和函 ...

  6. Javascript自执行匿名函数(function() { })()的原理浅析

    匿名函数就是没有函数名的函数.这篇文章主要介绍了Javascript自执行匿名函数(function() { })()的原理浅析的相关资料,需要的朋友可以参考下 函数是JavaScript中最灵活的一 ...

  7. Javascript学习之函数(function)

    在JS中,Function(函数)类型实际上是对象;每个函数都是Function类型的实例,而且都与其他引用类型一样具有属性和方法.由于函数是对象,因此函数名实际上也是一个指向函数对象的指针. 一 函 ...

  8. js立即执行函数: (function ( ){...})( ) 与 (function ( ){...}( ))

    ( function(){…} )() ( function (){…} () ) 是两种javascript立即执行函数的常见写法,最初我以为是一个括号包裹匿名函数,再在后面加个括号调用函数,最后达 ...

  9. 深入理解立即执行函数(function(){})();

    ( function(){-} )()和( function (){-} () )是两种javascript立即执行函数的常见写法,要理解立即执行函数,需要先理解一些函数的基本概念. 1,函数声明,函 ...

随机推荐

  1. Apache httpd.conf配置文件主要内容解释

    1 ServerRoot 配置 ["ServerRoot" 主要用于指定Apache的安装路径,此选项参数值在安装Apache时系统会自动把Apache的路径写入.Windows安 ...

  2. java----回文序列判断java

    import java.util.Scanner; public class test02 { public static void main(String[] args) { Scanner in ...

  3. SQL Server 锁机制

    锁兼容性图: 一.锁的粒度: 比较需要注意的是RID/KEY.HoBT/PAGE这两对儿的区别,RID和HoBT是针对堆表的,即没有聚集索引的表. 二.锁的模式: 1.关于其中的S.U.X锁: 共享锁 ...

  4. Android 自定义ListView单击事件失效

    因为自带的listView不能满足项目需求,通过实现自己的Adapter去继承ArrayAdapter 来实现自定义ListView的Item项目. 出现点击ListView的每一项都不会执行setO ...

  5. JaveScript 中的正则表达式

    1.语法: var expression = /pattern/flags ; pattern: 任何简单或复杂的正则表达式. flags: 可以是 g,i,m 或它们的组合.             ...

  6. C# -- 交错数组的使用

    C# -- 交错数组的使用 交错数组是元素为数组的数组.交错数组元素的维度和大小可以不同.交错数组有时称为“数组的数组”. 1. 举例一:子数组是长度相同的一维数组 static void Main( ...

  7. nginx+keepalived高可用及双主模式

    高可用有2中方式. 1.Nginx+keepalived 主从配置 这种方案,使用一个vip地址,前端使用2台机器,一台做主,一台做备,但同时只有一台机器工作,另一台备份机器在主机器不出现故障的时候, ...

  8. J - Abbott's Revenge 搜索 寒假训练

    题目 题目大意:这个题目就是大小不超过9*9的迷宫,给你起点终点和起点的方向,让你进行移动移动特别之处是不一定上下左右都可以,只有根据方向确定可以走的方向.思路:需要写一个读入函数,这个需要读入起点, ...

  9. SQL Alias(别名)

    通过使用 SQL,可以为列名称和表名称指定别名(Alias). SQL Alias 表的 SQL Alias 语法 SELECT column_name(s) FROM table_name AS a ...

  10. [Android] ScrollView can host only one direct child

    android 采用ScrollView布局时出现异常:ScrollView can host only one direct child.主要是ScrollView内部只能有一个子元素,即不能并列两 ...