This article focuses on the following issues:
1. What is a function and how to create a function?
2. How to call a function?
3. How does a function work?
4. How to pass parameters to a function?
5. How to return the value of a function and return the value of a local variable.
1. Create a function (what is a function)
The function in batch script starts with a tag and ends with Goto: EOF, as follows:
Script
: Mydosfunc-start of the function. Use a tag to identify the echo. Function body. You can execute many commands echo. Goto: EOF
Ii. Call Functions
Script: 01.
Call: mydosfunc
Iii. How functions work
The script that calls the function divides it into two parts.
1. Main Script: starts from the first line and ends with the Goto: EOF command
2. function: it is composed of multiple functions and called by main script.
Script:
@ Echo offecho. start calling the function call: mydosfuncecho. returns mydosfuncecho from the function. & pause & Goto: EOF: Functions: -- function starts with: --------------------------------------------------------: mydosfunc-here starts my function identified by it's labelecho. here the mydosfunc function is executing a group of commandsecho. it cocould do a lot of thingsgoto: EOF
3. how to pass Parameters and obtain the parameter values in the function
1. Separate parameters with spaces or commas
2. Enclose string parameters with spaces in double quotes
call:myDosFunc 100 YeePEEcall:myDosFunc 100 "for me"call:myDosFunc 100,"for me"
Get the parameter, using % 1 ~ % 9 to get the value of each parameter. % 0, indicating the batch processing file itself
:myDosFunc - here starts myDosFunc identified by it`s labelecho.echo. here the myDosFunc function is executing a group of commandsecho. it could do %~1 of things %~2.goto:eof
Script with Parameters
@echo offecho.going to execute myDosFunc with different argumentscall:myDosFunc 100 YeePEEcall:myDosFunc 100 "for me"call:myDosFunc 100,"for me"call:myDosFunc 100,for meecho.&pause&goto:eof::--------------------------------------------------------::-- Function section starts below here::--------------------------------------------------------:myDosFunc - here starts my function identified by it's labelecho.echo. here the myDosFunc function is executing a group of commandsecho. it could do %~1 of things %~2.goto:eof
Iv. function return values
1. The call command does not return values as in other languages. The most common practice is to save the value in a global variable in the function.
Use this global variable directly. As follows:
Usage:
set "var1=some hopefully not important string"echo.var1 before: %var1%call:myGetFuncecho.var1 after : %var1%
Script:
:myGetFunc - get a valueset "var1=DosTips"goto:eof
The Script output is as follows:
Var1 before: Some hopefully not important string
Var1 after: dostips
2. By referencing the return value, the caller stores the return value by passing a variable to the function.
Usage:
call:myGetFunc var1echo.var1 after : %var1%
Script:
:myGetFunc - passing a variable by referenceset "%~1=DosTips"goto:eof
The Script output is as follows:
Var1 after: dostips
Complete script:
@echo offset "var1=CmdTips"echo.var1 before: %var1%call:myGetFunc var1echo.var1 after : %var1%echo.&pause&goto:eof::--------------------------------------------------------::-- Function section starts below here::--------------------------------------------------------:myGetFunc - passing a variable by referenceset "%~1=DosTips"goto:eof
V. Local variables of functions
How can we ensure that local variables do not conflict with global variables? The setlocal command allows the processor to use endlocal to remove local variables.
The endlocal is automatically called. When the batch processing is executed to the end of the file, that is, Goto: EOF. Setlocal can effectively protect the variables in the function from conflict with those outside the function.
@echo offset "aStr=Expect no changed, even if used in function"set "var1=No change for this one. Now what?"echo.aStr before: %aStr%echo.var1 before: %var1%call:myGetFunc var1echo.aStr after : %aStr%echo.var1 after : %var1%echo.&pause&goto:eof::--------------------------------------------------------::-- Function section starts below here::--------------------------------------------------------:myGetFunc - passing a variable by referenceSETLOCALset "aStr=DosTips"set "%~1=%aStr%"ENDLOCALgoto:eof
Script output:
Astr before: Failed CT no changed, even if used in Function
Var1 before: no change for this one. Now what?
Astr after: Wrong CT no changed, even if used in Function
Var1 after: no change for this one. Now what?
Returns a local variable.
---- How can I skip the endlocal barrier and return the local variable value?
If you use variable extension, the value of the global variable between setlocal and endlocal is backed up. When you exit endlocal, the value is restored. Let the command processor execute the endlocal and set commands.
@echo offset "aStr=Expect no changed, even if used in function"set "var1=Expect changed"echo.aStr before: %aStr%echo.var1 before: %var1%call:myGetFunc var1echo.aStr after : %aStr%echo.var1 after : %var1%echo.&pause&goto:eof::--------------------------------------------------------::-- Function section starts below here::--------------------------------------------------------:myGetFunc - passing a variable by referenceSETLOCALset "aStr=DosTips"( ENDLOCAL set "%~1=%aStr%")goto:eof:myGetFunc2 - passing a variable by referenceSETLOCALset "aStr=DosTips"ENDLOCAL&set "%~1=%aStr%" &rem THIS ALSO WORKS FINEgoto:eof
Script output:
Astr before: Failed CT no changed, even if used in Function
Var1 before: Wrong CT changed
Astr after: Wrong CT no changed, even if used in Function
Var1 after: dostips
6. Compile recursive functions
Make the transformation of local variables of the function visible to the caller, and call the function cyclically to make the variables reusable. Compile a function to calculate the Fibonacci series.
@echo offset "fst=0"set "fib=1"set "limit=1000000000"call:myFibo fib,%fst%,%limit%echo.The next Fibonacci number greater or equal %limit% is %fib%.echo.&pause&goto:eof::--------------------------------------------------------::-- Function section starts below here::--------------------------------------------------------:myFibo -- calculate recursively the next Fibonacci number greater or equal to a limit:: -- %~1: return variable reference and current Fibonacci number:: -- %~2: previous value:: -- %~3: limitSETLOCALset /a "Number1=%~1"set /a "Number2=%~2"set /a "Limit=%~3"set /a "NumberN=Number1 + Number2"if /i %NumberN% LSS %Limit% call:myFibo NumberN,%Number1%,%Limit%(ENDLOCAL IF "%~1" NEQ "" SET "%~1=%NumberN%")goto:eof
VII. Conclusion: Define a standard DOS batch script Function
:myFunctionName -- function description here:: -- %~1: argument description hereSETLOCALREM.--function body hereset LocalVar1=...set LocalVar2=...(ENDLOCAL & REM -- RETURN VALUES IF "%~1" NEQ "" SET %~1=%LocalVar1% IF "%~2" NEQ "" SET %~2=%LocalVar2%)GOTO:EOF
[DOS batch processing] Function Definition and usage