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. jsp笔记----jsp常用的的获取项目的根路径

    <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" ...

  2. 转自:stuff字符串拼接方法

    下文讲述数据表中将多列合并到一列的方法分享 转自:http://www.maomao365.com/?p=6796

  3. c/c++ 线性表之单向链表

    c/c++ 线性表之单向链表 线性表之单向链表 不是存放在连续的内存空间,链表中的每个节点的next都指向下一个节点,最后一个节点的下一个节点是NULL. 真实的第一个节点是头节点,头节点不存放数据, ...

  4. Windows Server 2016-Win Ser 2016新增功能

    本来想着整个系列都是与Active Directory相关的内容,上一章节我们应读者要求补充了Window Server 2016标准版与数据中心版的区别,鉴于读者的疑惑,从本章节开始补充三到五章与W ...

  5. kmp算法python实现

    kmp算法python实现 kmp算法 kmp算法用于字符串的模式匹配,也就是找到模式字符串在目标字符串的第一次出现的位置比如abababc那么bab在其位置1处,bc在其位置5处我们首先想到的最简单 ...

  6. Ajax入门例子

    在customer.php的文件中,代码如下: <html> <head> <script type="text/javascript"> fu ...

  7. 序列对象(bytearray, bytes,list, str, tuple)

    列表: L.append(x) # x追加到L尾部 L.count(x) # 返回x在L中出现的次数 L.extend(m) # Iterable m的项追加到L末尾 L += m # 功能同L.ex ...

  8. 原生js获取鼠标坐标方法全面讲解:clientX/Y,pageX/Y,offsetX/Y,layerX/Y,screenX/Y

    关于js鼠标事件综合各大浏览器能获取到坐标的属性总共以下五种 event.clientX/Y event.pageX/Y event.offsetX/Y event.layerX/Y event.sc ...

  9. 1.01-url-open_code

    import urllib.request def load_data(): url = "http://www.baidu.com/" #get的请求 #http请求 #resp ...

  10. UVA11059-Maximum Product(动态规划)

    Problem UVA11059-Maximum Product Accept:4769  Submit:38713 Time Limit: 3000 mSec  Problem Descriptio ...