call & Subroutines
call runs something and then comes back. Pointed at another batch file, it runs that file and returns; pointed at a label in the same file (call :name), it turns that label into a subroutine: a reusable block of code with its own arguments, the closest thing CMD has to a function.
A first subroutine
@echo off
call :greet Ada
call :greet Grace
echo main part finished
goto :eof
:greet
echo Hello, %1!
goto :eofHello, Ada!
Hello, Grace!
main part finished
How it works:
call :greet Adajumps to:greet, remembering where it came from. The words after the label become the subroutine's arguments:%1isAda.- The subroutine runs until it reaches
goto :eof(orexit /b), which returns to the line after thecall. - The
goto :eofafter the main part stops the script from falling into the subroutine a third time.
Note the colon: call :greet calls a label, while call greet would look for a program or batch file called greet.
Arguments
Inside a subroutine, %1 to %9 are its own arguments, and %* is all of them. Quote arguments that contain spaces, and use %~1 to read them without the quotes:
@echo off
call :show one "two words" three
goto :eof
:show
echo first: %1
echo second, raw: %2
echo second, unquoted: %~2
echo all: %*
goto :eoffirst: one
second, raw: "two words"
second, unquoted: two words
all: one "two words" three
Arguments are split on spaces, commas, semicolons and =, so call :show a,b passes two arguments. Quotes keep them together. Every modifier from Arguments: %1 & %~ works here too.
Returning values
A subroutine can't return a value the way a function in other languages does, but there are two well-established ways out.
1. Set a variable whose name the caller passes in. This is the standard pattern. The subroutine receives the name of the result variable and assigns it:
@echo off
call :square 7 result
echo 7 squared is %result%
call :square 12 big
echo 12 squared is %big%
goto :eof
:square
rem %1 = number, %2 = name of the variable that receives the result
set /a %2=%1*%1
goto :eof7 squared is 49
12 squared is 144
2. Return a small number as the exit code with exit /b n. The caller reads it from %errorlevel%. This is meant for success/failure codes (see Exit Codes & errorlevel):
@echo off
call :is_even 10
if %errorlevel% equ 0 (echo 10 is even) else (echo 10 is odd)
call :is_even 7
if %errorlevel% equ 0 (echo 7 is even) else (echo 7 is odd)
goto :eof
:is_even
set /a r=%1 %% 2
exit /b %r%10 is even
7 is odd
exit without /b would close the whole Command Prompt window, not just the subroutine. Always use /b in scripts.
Local variables
Variables in batch files are global: a subroutine that sets rem or i overwrites the caller's variables with the same names. Wrap the subroutine's body in setlocal / endlocal to keep its helper variables private. To still hand back a result, use the endlocal & set trick: the whole line is expanded before endlocal runs, so the value survives:
@echo off
set "i=main's value"
call :sum_to 4 total
echo sum 1..4 = %total%
echo i is still: %i%
goto :eof
:sum_to
setlocal
set /a acc=0
for /l %%i in (1,1,%1) do set /a acc+=%%i
set "i=changed in the subroutine"
endlocal & set "%2=%acc%"
goto :eofsum 1..4 = 10
i is still: main's value
The subroutine's i and acc disappear at endlocal, but %acc% was already expanded into the set on the same line. setlocal & Delayed Expansion explains this in depth.
Calling subroutines from loops
A for loop body can't contain goto without ending the loop, but it can call a label once per item. That also avoids delayed expansion, because each call is a fresh piece of code:
@echo off
set /a total=0
for %%n in (4 8 15 16) do call :add %%n
echo total = %total%
goto :eof
:add
set /a total+=%1
echo added %1, running total %total%
goto :eofadded 4, running total 4
added 8, running total 12
added 15, running total 27
added 16, running total 43
total = 43
Recursion
Subroutines can call themselves. Each call has its own %1, so recursion works; just give it a stop condition, and keep the depth modest because CMD's call stack is small:
@echo off
call :countdown 3
echo liftoff
goto :eof
:countdown
if %1 leq 0 goto :eof
echo %1...
set /a next=%1-1
call :countdown %next%
goto :eof3...
2...
1...
liftoff
Calling other batch files
To run another batch file and come back, you must use call:
call tools\build.bat release
echo this line runs after build.bat finishesWithout call, control transfers to build.bat permanently and the rest of your script never runs. This is a classic mistake when a script runs npm, mvn or gradle, because on Windows those are batch files (npm.cmd, mvn.cmd, gradle.bat). Write call npm install, not npm install, inside a batch file.
The called file shares the environment, so variables it sets remain visible to the caller (unless it uses setlocal).
call for a second round of expansion
A side effect of call: the line after it is expanded twice, and %% turns into % in the first round. Old scripts use this to read a variable whose name is itself in a variable:
@echo off
set "color_1=red"
set "color_2=green"
set /a pick=2
call echo picked: %%color_%pick%%%picked: green
After the first round the line is call echo picked: %color_2%; call expands it again into green. Delayed expansion (!color_%pick%!) does the same thing more clearly and faster.
Comparison with other shells
| Task | CMD | Bash | PowerShell |
|---|---|---|---|
| define | :name ... goto :eof |
name() { ...; } |
function Name { ... } |
| call | call :name a b |
name a b |
Name a b |
| arguments | %1, %~1, %* |
$1, "$@" |
param(...), $args |
| return status | exit /b 1 |
return 1 |
return / throw |
| local variables | setlocal |
local |
scopes are automatic |
Learn more: Bash Functions, PowerShell Functions, Arguments: %1 & %~.
Test yourself
What is the difference between call :build and goto build?
- There is none
- call returns to the next line when the subroutine ends; goto does not
- goto passes arguments, call does not
- call only works with other files
A batch file runs mvn package and then echo done, but done never prints. Why?
- mvn crashed
- mvn is a batch file, and running a batch file without call never returns
- echo needs @
- The script needs setlocal
Inside :sum, how should the result be handed back to a caller-provided variable name in %2?
- return %acc%
- set /a %2=acc
- echo %acc%
- exit %acc%