Skip to main content

Batch Script Functions with Parameters

Functions with Parameters

Batch Script Functions can work with parameters by simply passing them when a call is made to the function.

Call :function_name parameter1, parameter2… parametern

The parameters can then be accessed from within the function by using the tilde (~) character along with the positional number of the parameter.

Example

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

Example of Functions with Parameters
@echo off
SETLOCAL
CALL :Display 5 , 10
EXIT /B %ERRORLEVEL%

:Display
echo The value of parameter 1 is %~1
echo The value of parameter 2 is %~2
EXIT /B 0

note

~1 is used to access the first parameter sent to the function, while ~2 is used to access the second parameter.

The value of parameter 1 is 5
The value of parameter 2 is 10