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. JAVA设计模式——代理(静态代理)

    定义 为其它的对象提供一种代理,以控制这个对象的访问 使用场景 当不想直接访问某个对象的时候,就可以通过代理 1.不想买午餐,同事帮忙带 2.买车不用去厂里,去4s店 3.去代理点买火车票,不用去车站 ...

  2. Hive分区

    注意:必须在表定义时指定对应的partition字段. 一.指定分区 1.单分区 建表语句:create table day_table(id int, content string) partiti ...

  3. C# WINFORM 应用程序动态读写xml config文件,获取数

    在实际项目里,我们需要用一个应用程序去连接多个数据库,有的进行测试,有的是数据库基本结构相同,数据不同, 我们不可能总去程序的连接字符串里去修改,更不能让用户去修改,所以需要动态去修改连接数据库配置信 ...

  4. SqlServer2017下载地址

    https://www.microsoft.com/zh-CN/download/details.aspx?id=55994

  5. linux系统/var/log目录下的信息详解

    一./var目录 /var 所有服务的登录的文件或错误信息文件(LOG FILES)都在/var/log下,此外,一些数据库如MySQL则在/var/lib下,还有,用户未读的邮件的默认存放地点为/v ...

  6. 解决“Eclipse中启动Tomcat后,http://localhost:8080/无法访问”的问题

    这个问题是eclipse造成的,我们可以修改配置来实现通过eclipse启动tomcat可以访问http://localhost:8080 打开Server试图(打开前不要启动tomcat),双击其中 ...

  7. css点滴2—六种方式实现元素水平居中

    本文参考文章<六种方式实现元素水平居中> 元素水平居中的方法,最常见的莫过于给元素一个显式的宽度,然后加上margin的左右值为auto.这种方式给固定宽度的元素设置居中是最方便不过的.但 ...

  8. [WeChall] Training: MySQL I (MySQL, Exploit, Training)

    Training: MySQL I (MySQL, Exploit, Training) MySQL Authentication Bypass - The classic This one is t ...

  9. 用H5开发微信还是开发APP?

    用H5开发微信还是开发APP? 随着技术的飞速发展,HTML第五版技术标准的更新,在移动端,由于其相对较低的开发成本及强大的跨平台运行能力,越来越多的信息型产品也开始选择这样轻量级的H5页面进行快速迭 ...

  10. setjmp/longjmp

    1.setjmp/longjmp属于传统的错误处理 2.setjmp/longjmp是对goto语句的补充,goto仅仅能实现局部跳转.setjmp/longjmp能够实现全局跳转 3.setjmp/ ...