Using sub routines in batch programs

[Autolink] Menu

 
@ECHO OFF
REM ------------------------------------------------------------------
REM
REM *** sample code to show how to use subroutines in OS/2 batch files
REM
REM ------------------------------------------------------------------

REM *** set a sample environment variable
REM
SET myEnvVar=1

REM ------------------------------------------------------------------

ECHO. [MAIN] The value of the environment variable "MyEnvVar" is %MyEnvVar%

ECHO. [MAIN] Now MAIN calls "subroutine" SUB1 ...

REM *** save the return address in an environment variable and call
REM     the "subroutine"
REM
SET retAddr1=RET001
GOTO SUB1

REM *** SUB1 jumps to this label to return to the caller
:RET001

REM ------------------------------------------------------------------

ECHO. [MAIN] The value of the environment variable "MyEnvVar" is %MyEnvVar%

ECHO. [MAIN] Now MAIN calls "subroutine" SUB2 ...

REM *** save the return address in an environment variable and call
REM     the "subroutine"
REM
SET retAddr2=RET002
GOTO SUB2

REM *** SUB2 jumps to this label to return to the caller
:RET002

ECHO. [MAIN] The value of the environment variable "MyEnvVar" is %MyEnvVar%

REM ------------------------------------------------------------------
REM *** do something ...
PAUSE

REM *** set a sample environment variable
REM
SET myEnvVar=2

REM ------------------------------------------------------------------
ECHO. [MAIN] Now MAIN calls "subroutine" SUB1 ...

ECHO. [MAIN] The value of the environment variable "MyEnvVar" is %MyEnvVar%

REM *** save the return address in an environment variable and call
REM     the "subroutine"
REM
SET retAddr1=RET003
GOTO SUB1

REM *** SUB1 jumps to this label to return to the caller
:RET003

REM ------------------------------------------------------------------

ECHO. [MAIN] The value of the environment variable "MyEnvVar" is %MyEnvVar%

ECHO. [MAIN] Now MAIN calls "subroutine" SUB2 ...

REM *** save the return address in an environment variable and call
REM     the "subroutine"
REM
SET retAddr2=RET004
GOTO SUB2

REM *** SUB2 jumps to this label to return to the caller
:RET004

ECHO. [MAIN] The value of the environment variable "MyEnvVar" is %MyEnvVar%

REM ------------------------------------------------------------------
REM *** house keeping
REM
SET retAddr1=
SET retAddr2=
SET myEnvVar=

REM *** and end the program
GOTO End

REM ------------------------------------------------------------------
REM *** This is the "subroutine" SUB1
REM     SUB1 has a local variable scope
REM
:SUB1
SETLOCAL
ECHO. [SUB1] *** This is SUB1 (local variable scope)
ECHO. [SUB1]  The value of the environment variable "MyEnvVar" is %MyEnvVar%
ECHO. [SUB1]  Setting the variable "myEnvVar" to 11
SET myEnvVar=11

ECHO. [SUB1]  The value of the environment variable "MyEnvVar" is %MyEnvVar%

ECHO. [SUB1]  Now SUB1 calls "subroutine" SUB2 ...
SET retAddr2=SUB2_1
GOTO SUB2

REM *** SUB2 jumps to this label to return to the caller
:SUB2_1

ECHO. [SUB1]  The value of the environment variable "MyEnvVar" is %MyEnvVar%

ECHO.
ECHO. [SUB1]  Now I am returning control back to the label %retAddr1%

ENDLOCAL
GOTO %retAddr1%

REM ------------------------------------------------------------------
REM *** This is the "subroutine" SUB2
REM     SUB2 has a global variable scope
REM
:SUB2
ECHO. [SUB2] *** This is SUB2 (global variable scope)
ECHO. [SUB2]  The value of the environment variable "MyEnvVar" is %MyEnvVar%

ECHO. [SUB2]  Setting the variable "myEnvVar" to 22
SET myEnvVar=22

ECHO. [SUB2]  Now I am returning control back to the label %retAddr2%
GOTO %retAddr2%

REM ------------------------------------------------------------------
REM *** label marking the program end
REM
:END


[Back: OS/2 Batch Programming]
[Next: Simulating SELECT in batch programs]