You can add starting variables to the function by adding
<parameter> to it's label. These starting variables can be accessed with
%n where n is the starting variable's number (
%1 for the first,
%2 for the second. This
%n method works for %1 - %9. For parameter 10 - 255, you will need to use the Shift command).
:function <var1> <var2>
Once you use
call :function param1 param2,
param1 can be accessed with
<parameter> isn't strictly necessary, but it helps with readability.
A neat trick that is useful when many variable are flying about is to use
endlocal in tandem with
endlocal essentially make the function it's own separate instance of the command prompt, variables set in it only stick around while it's in the frame.
If you are using
endlocal, and you are returning global values use this.
endlocal & set var=variable
This sets the global value
variable. You can chain these together for multiple variables.
endlocal & set var=variable & set var2=variable number 2
This sets the global variable
variable and the global value
variable number 2.
Since code in code blocks are also performed simultaneously, you can do this as well.
if "%var%"=="" ( endlocal set %~2=10 )
But, you cannot do this.
if "%var%"=="" ( set %~2=10 endlocal )