- 1 year ago
batch function with two parameters, returning one
To create a batch function with two parameters and return a value, you can use the %1
and %2
placeholders to represent the input parameters and the echo
command to output the result. Here's an example:
batch@echo off
REM Define the function
:myFunction
setlocal
set "param1=%~1"
set "param2=%~2"
REM Perform some operations
set /a result=%param1% + %param2%
REM Return the result
echo %result%
endlocal
exit /b
REM Calling the function
call :myFunction 5 3
echo Result: %errorlevel%
In this example, the myFunction
batch function takes two parameters %1
and %2
. The function performs an addition operation on the input parameters and stores the result in the result
variable.
To call the function, use the call
command followed by the function name and the two parameters. The echo
statement after the function call displays the returned value.
The endlocal
command is used to clean up the local variable scope, and the exit /b
command is used to exit the script without further execution.
The Result: %errorlevel%
line shows the value returned by the function using the %errorlevel%
system variable.
You can modify the function to perform any operations or calculations based on your requirements.