Example - Returning Values using Variable Reference

An Example showing how it works.

Description: This code shows how the var1 variable is being passed into a :myGetFunc function simply by passing the variable name. Within the :myGetFunc function the command processor works like this:
  1. Reads the set command into memory: set "%~1=DosTips"
  2. Expand the variables, i.e. %~1 like this: set "var1=DosTips"
  3. Finally execute the command and assign the new string to var1
Script: Download: BatchTutoFunc3.bat  
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
@echo off

set "var1=CmdTips"
echo.var1 before: %var1%
call:myGetFunc var1
echo.var1 after : %var1%

echo.&pause&goto:eof


::--------------------------------------------------------
::-- Function section starts below here
::--------------------------------------------------------

:myGetFunc    - passing a variable by reference
set "%~1=DosTips"
goto:eof
Script Output:
 DOS Script Output
var1 before: CmdTips
var1 after : DosTips

Press any key to continue . . .