DOS批处理 函数定义与用法
what it is, why it`s important and how to write your own.
description: the assumption is: a batch script snippet can be named a function when:
1.... it has a callable entrance point.
2.... on completion execution continues right after the command that initially called the function.
3.... it works the same no matter from where it`s being called, even when it calls itself recursively.
4.... the variables used within a function do not conflict with variables outside the function.
5.... it exchanges data with the calling code strictly through input and output variables or a return code.
the benefits behind functions are:
1.keep the main script clean
2.hide complexity in reusable functions
3.test functions independently from the main script
4.add more functionality to your batch script simply by adding more functions at the bottom
5.don`t worry about the function implementation, just test it and use it
create a function what is a function?
call a function how to invoke a function?
example - calling a function an example showing how it works.
passing function arguments how to pass arguments to the function?
parsing function arguments how to retrieve function arguments within the function?
example - function with arguments an example showing how it works.
returning values the classic way the classic way of returning values and the limitations.
returning values via references let the caller determine how to return the function result and avoid the need of dedicated variables.
example - returning values using variable reference an example showing how it works.
local variables in functions how to avoid name conflicts and keep variable changes local to the function?
returning local variables how to pass return values over the endlocal barrier?
recursive functions tadaaah!!!
summary defining a standard format for a dos batch function
dos batch - function tutorial what it is, why it`s important and how to write your own.
create a function - what is a function
description: in dos you write a function by surrounding a group of command by a label and a goto:eof command. a single batch file can contain multiple functions defined like this. the label becomes the function name.
script:
:mydosfunc - here starts my function identified by it`s label
echo. here the mydosfunc function is executing a group of commands
echo. it could do a lot of things
goto:eof
call a function - how to invoke a function
description: a function can be called with the call command followed by the function label.
script: 01.
call:mydosfunc
example - calling a function - an example showing how it works
description: the use of batch functions will divide the script into two sections.
1.the main script: starting at line 1 ending with a goto:eof command that terminates the script.
2.the function section: filling the second half of the batch file with one or more functions to be callable from the main script.
script:
@echo off
echo.going to execute mydosfunc
call:mydosfunc
echo.returned from mydosfunc
echo.&pause&goto:eof
::--------------------------------------------------------
::-- function section starts below here
::--------------------------------------------------------
:mydosfunc - here starts my function identified by it`s label
echo. here the mydosfunc function is executing a group of commands
echo. it could do a lot of things
goto:eof
script output: script output
going to execute mydosfunc
here the mydosfunc function is executing a group of commands
it could do a lot of things
returned from mydosfunc
press any key to continue . . .
passing function arguments - how to pass arguments to the function
description: just as the dos batch file itself can have arguments, a function can be called with arguments in a similar way. just list all arguments after the function name in the call command.
use a space or a comma to separate arguments.
use double quotes for string arguments with spaces.
script:
call:mydosfunc 100 yeepee
call:mydosfunc 100 "for me"
call:mydosfunc 100,"for me"
parsing function arguments - how to retrieve function arguments within the function
description: just as the dos batch file itself retrieves arguments via %1 ⦠%9 a function can parse it`s arguments the same way. the same rules apply.
let`s modify our previews example to use arguments.
to strip of the double quotes in an arguments value the tilde modifier, i.e. use %~2 instead of %2.
script:
:mydosfunc - here starts mydosfunc identified by it`s label
echo.
echo. here the mydosfunc function is executing a group of commands
echo. it could do %~1 of things %~2.
goto:eof
example - function with arguments - an example showing how it works
description: the following example demonstrates how to pass arguments into a dos function. the :mydosfunc function is being called multiple times with different arguments.
note: the last call to mydosfunc doesn`t use double quotes for the second argument. subsequently "for" and "me" will be handled as two separate arguments, whereas the third argument "me" is not being used within the function.
script:
@echo off
echo.going to execute mydosfunc with different arguments
call:mydosfunc 100 yeepee
call:mydosfunc 100 "for me"
call:mydosfunc 100,"for me"
call:mydosfunc 100,for me
echo.&pause&goto:eof
::--------------------------------------------------------
::-- function section starts below here
::--------------------------------------------------------
:mydosfunc - here starts my function identified by it's label
echo.
echo. here the mydosfunc function is executing a group of commands
echo. it could do %~1 of things %~2.
goto:eof
script output: script output
going to execute mydosfunc with different arguments
here the mydosfunc function is executing a group of commands
it could do 100 of things yeepee.
here the mydosfunc function is executing a group of commands
it could do 100 of things for me.
here the mydosfunc function is executing a group of commands
it could do 100 of things for me.
here the mydosfunc function is executing a group of commands
it could do 100 of things for.
press any key to continue . . .
returning values the classic way - the classic way of returning values and the limitations
description: the call command doesn`t support return values as known by other programming languages.
the classic walkaround is to have the function store the return value into a environment variable. the calling script can use this variable when the function returns. the :mygetfunc function below demonstrates how the variable var1 gets the "dostips" string assigned which can then be used in the calling function.
note: the var1 variable is reserved for this particular function. any data stored in var1 by the calling function before calling :mygetvar will be overwritten.
usage:
set "var1=some hopefully not important string"
echo.var1 before: %var1%
call:mygetfunc
echo.var1 after : %var1%
script:
:mygetfunc - get a value
set "var1=dostips"
goto:eof
script output: script output
var1 before: some hopefully not important string
var1 after : dostips
returning values via references - let the caller determine how to return the function result and avoid the need of dedicated variables
description: instead of using "global" variables for return value, the function can use one of it`s arguments as variable reference. the caller can then pass a variable name to the function and the function can store the result into this variable making use of the command line expansion of the command processor:
note: the var1 variable is not reserved for this articular function. any variable can be passed to the function the caller has full control.
usage:
call:mygetfunc var1
echo.var1 after : %var1%
script:
:mygetfunc - passing a variable by reference
set "%~1=dostips"
goto:eof
script output: script output
var1 after : dostips
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:
@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: script output
var1 before: cmdtips
var1 after : dostips
press any key to continue . . .
local variables in functions - how to avoid name conflicts and keep variable changes local to the function
description: the setlocal causes the command processor to backup all environment variables. the variables can be restored by calling endlocal. changes made im between are local to the current batch. endlocal is automatically being called when the end of the batch file is reached, i.e. by calling goto:eof.
localizing variables with setlocal allows using variable names within a function freely without worrying about name conflicts with variables used outside the function.
script:
@echo off
set "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 var1
echo.astr after : %astr%
echo.var1 after : %var1%
echo.&pause&goto:eof
::--------------------------------------------------------
::-- function section starts below here
::--------------------------------------------------------
:mygetfunc - passing a variable by reference
setlocal
set "astr=dostips"
set "%~1=%astr%"
endlocal
goto:eof
script output: script output
astr before: expect no changed, even if used in function
var1 before: no change for this one. now what?
astr after : expect no changed, even if used in function
var1 after : no change for this one. now what?
press any key to continue . . .
returning local variables - how to pass return values over the endlocal barrier
description: the question is: when localizing a function via setlocal and endlocal, how to return a value that was calculated before executing endlocal when endlocal restores all variables back to its original state?
the answer comes with "variable expansion". the command processor expands all variables of a command before executing the command. letting the command processor executing endlocal and a set command at once solves the problem. commands can be grouped within brackets.
script:
@echo off
set "astr=expect no changed, even if used in function"
set "var1=expect changed"
echo.astr before: %astr%
echo.var1 before: %var1%
call:mygetfunc var1
echo.astr after : %astr%
echo.var1 after : %var1%
echo.&pause&goto:eof
::--------------------------------------------------------
::-- function section starts below here
::--------------------------------------------------------
:mygetfunc - passing a variable by reference
setlocal
set "astr=dostips"
( endlocal
set "%~1=%astr%"
)
goto:eof
:mygetfunc2 - passing a variable by reference
setlocal
set "astr=dostips"
endlocal&set "%~1=%astr%" &rem this also works fine
goto:eof
script output: script output
astr before: expect no changed, even if used in function
var1 before: expect changed
astr after : expect no changed, even if used in function
var1 after : dostips
press any key to continue . . .
recursive functions - tadaaah!!!
description: being able to completely encapsulate the body of a function by keeping variable changes local to the function and invisible to the caller we are now able to call a function recursively making sure each level of recursion works with its own set of variables even thought variable names are being reused.
example: the next example below shows how to calculate a fibonacci number recursively. the recursion ss when the fibonacci algorism reaches a number greater or equal to a given input number.
the example starts with the numbers 0 and 1 the :myfibo function calls itself recursively to calculate the next fibonacci number until it finds the fibonacci number greater or equal 1000000000.
the first argument of the myfibo function is the name of the variable to store the output in. this variable must be initialized to the fibonacci number to start with and will be used as current fibonacci number when calling the function and will be set to the subsequent fibonacci number when the function returns.
script:
@echo off
set "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: limit
setlocal
set /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
script output: script output
the next fibonacci number greater or equal 1000000000 is 1134903170.
press any key to continue . . .
summary - defining a standard format for a dos batch function
description: with the information learned in this section we can define a standard format for a dos batch functions as shown below.
also check out the rich set of ready to use dos functions provided by the dostips.com function library.
script:
:myfunctionname -- function description here
:: -- %~1: argument description here
setlocal
rem.--function body here
set localvar1=...
set localvar2=...
(endlocal & rem -- return values
if "%~1" neq "" set %~1=%localvar1%
if "%~2" neq "" set %~2=%localvar2%
)
goto:eof
上一篇: 深入理解Vue 单向数据流的原理
下一篇: 批处理(bat)命令学习的一些总结