batch-file Functions Anonymous functions in batch files


Anonymous functions technique uses the fact that CALL command uses internally GOTO when subroutine is called and abusing help message printing with variable double expansion:

@echo off
set "anonymous=/?"

call :%%anonymous%% a b c 3>&1 >nul

if "%0" == ":%anonymous%" (
  echo Anonymous call:
  echo %%1=%1 %%2=%2 %%3=%3
  exit /b 0

You can call an anonymous function only if it is defined after the CALL (or after finishing brackets context if the CALL is executed within brackets). It cannot be called from an outside script ,but is a slower than normal function call.