Skip to content

Batch Script Functions with Return Values

Learn how to use Batch Script functions with return values by passing variables and setting them with the set command and tilde (~) parameter syntax.

Functions with Return Values

In Batch Script, functions can work with return values simply by passing the names of the variables that will contain the return values when a call is made to the function.

Call :function_name value1, value2… valuen

Return values are set in the function using the set command and the tilde character (~) along with the parameter position number.

Example

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

Example of Functions with return valuesbatch
@echo off
SETLOCAL
CALL :SetValue value1,value2
echo %value1%
echo %value2%
EXIT /B %ERRORLEVEL%

:SetValue
set "%~1=5"
set "%~2=10"
EXIT /B 0
Outputplaintext
5
10
Navigation