Batch Script - Functions with Parameters


Advertisements

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

Syntax

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.

Following example shows how a function can be called with parameters.

Example

@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

As seen in the above example, ~1 is used to access the first parameter sent to the function, similarly ~2 is used to access the second parameter.

Output

The above command produces the following output.

The value of parameter 1 is 5
The value of parameter 2 is 10
batch_script_functions.htm
Advertisements