『楼 主』:
转贴:批命令中的子程序
使用 LLM 解释/回答一下
批命令中的子程序
大多数成熟的高级编程语言允许您把常用的例程建立成独立的模块即子程序,它保
留了代码的独立性,使程序模式化,便于组织.至少可以说DOS的批命令不完全支持子程
序,但在任何时候你都可以用GOTO命令调用子程序.
通常,GOTO命令用的标号还可以接受存储在环境变量中的标号,如:
SET LABELNAME=START
GOTO%LABELNAME%
通过在子程序的开始替换标号并在子程序结束的地方设置GOTO %RETURN%语句,你
可以在DOS的批命令中建立子程序.只要SET 一个环境变量RETURN,你可在批命令的任
意专访调用子程序.例如,你可以给标号赋值并在下一行调用该子程序,招生GOTO语句
跳至该子程序的开始处,程序结构如:
REM This set the environment
REM variable and calls the
REM subroutine
SET RETURN=HERE
GOTO SUB
HERE
SUB
REM
Place subroutine statements
REM below
GOYO %RETURN%
语句GOTO SUB传送到子程序SUB,在SUB中你可以执行任何语句.执行至
GOTO%RETURN%时,批命令返回控制到号HERE,因环境变量RETURN被复赋值为HERE,如果
你
每次调用SUB都 用叭一的标号(如RETURN),则可多次多处调用SUB.
Subroutines in Batch Commands
Most mature high-level programming languages allow you to build common routines into independent modules, namely subroutines, which preserve the independence of the code, standardize the program, and facilitate organization. At the very least, it can be said that DOS batch commands do not fully support subroutines, but you can always use the GOTO command to call a subroutine.
Usually, the label used by the GOTO command can also accept a label stored in an environment variable, for example:
SET LABELNAME=START
GOTO %LABELNAME%
By replacing the label at the beginning of the subroutine and setting the GOTO %RETURN% statement at the end of the subroutine, you can build subroutines in DOS batch commands. As long as you SET an environment variable RETURN, you can call the subroutine at any point in the batch command. For example, you can assign a value to the label and call the subroutine on the next line, then the GOTO statement jumps to the beginning of the subroutine. The program structure is as follows:
REM This set the environment
REM variable and calls the
REM subroutine
SET RETURN=HERE
GOTO SUB
HERE
SUB
REM
Place subroutine statements
REM below
GOYO %RETURN%
The statement GOTO SUB transfers to the subroutine SUB, where you can execute any statements. When executing to GOTO %RETURN%, the batch command returns control to the label HERE because the environment variable RETURN is reassigned to HERE. If you use a unique label (such as RETURN) each time you call SUB, you can call SUB multiple times in multiple places.
|