Batch Script - Functions with Return Values



Functions can work with return values by simply passing variables names which will hold the return values when a call is made to the function as shown below

Syntax

Call :function_name value1, value2… valuen

The return values are set in the function using the set command and the tilde(~) character along with the positional number of the parameter.

Following example shows how a function can be called with return values.

Example

@echo off
SETLOCAL
CALL :SetValue value1,value2
echo %value1%
echo %value2%
EXIT /B %ERRORLEVEL%
:SetValue
set "%~1 = 5"
set "%~2 = 10"
EXIT /B 0

Output

The above command produces the following output.

5 
10
batch_script_functions.htm
Advertisements