MC-Basic:SUB ... END SUB/zh-hans

From SoftMC-Wiki
Jump to: navigation, search
语言: English  • 中文(简体)‎

缩写

格式

SUB <subprogram name>
          <subroutine code to execute>
END SUB

                             SUB <name> ({ {ByVal} <p_1> As <type_1>}…{, {ByVal} <p_n> As <type_n>})
       {local variable declaration}
       {subroutine code}
END SUB

试用版本

所有版本

描述

子集和最终关键字子集用于划分任务内的子程序. 子程序必须出现在代码的主程序之后 (由 PROGRAM..END PROGRAM划分).局部变量, 只用DIM来定义,出现在Sub关键字后,在子程序之前,用CALL 关键词来执行子程序.

<name> 是子程序的名字.
<type_1> <type_n> 是参数变量的类型.
<p_1><p_n>是传递给子程序参数的名字.参数是变量在子程序内部使用的代码;当子程序被调用,变量的值就会被调用的值所代替.缺省值,参数通过引用传递.

{ByVal}是一个指定参数通过值传递的选项.

{local variables declaration} 向子程序声明局部变量.

{subroutine code}定义计算返回值的算法。子程序可以循环的.

数组只能通过引用传递.假如你想通过值来传递整个数组, 将会报错. 数列的语法:

SUB <name> ({<p_1>([*])+ as <type_1>}…{, <p_n>([*])+ as <type_n>})

{ local variable declaration }

{ subroutine code }

END SUB

where

<p_l> : 数组变量的名字

<p_n>: 数组变量的名字

[*] :维数组没有指定的界限

+? : 表示一个或更多

类型

Parmeters: Long, Double, String, Joint, Location, user defined structures, Generic Axis, Generic Group, Moving Frame (by reference only), Cam (by reference only), Comp (by reference only), Semaphore (by reference only), user defined Error and Note (by reference only).

取值范围

单位

默认

作用域,任务

使用范围

限制

数组只有通过引用的方式传递.

例子 =

DIM SHARED lastloop as LONG

PROGRAM
       lastloop=10
       Attach a1
       Move a1 100
       Call a1_Move(LastLoop)

END PROGRAM

SUB a1_Move(lastLoop as Long)               'Pass parameters by reference

DIM Index as LONG
       For Index = 1 to lastloop
                   Move a1 1 abs = 0
       Next Index

END SUB


Array Example:

SUB mean(x[*][*] as DOUBLE, TheMean[*] as LONG)

DIM sum as DOUBLE

DIM I as LONG

FOR i = 1 to 100

sum = sum + x[i][1]

NEXT i

TheMean[1] = sum/100

END SUB

参见