Batch Script - Calling a Function



A function is called in Batch Script by using the call command. Following is the syntax.

Syntax

call :function_name

Following example shows how a function can be called from the main program.

Example

@echo off 
SETLOCAL 
CALL :Display 
EXIT /B %ERRORLEVEL% 
:Display 
SET /A index=2 
echo The value of index is %index% 
EXIT /B 0

One key thing to note when defining the main program is to ensure that the statement EXIT /B %ERRORLEVEL% is put in the main program to separate the code of the main program from the function.

Output

The above command produces the following output.

The value of index is 2
batch_script_functions.htm
Advertisements