Description: | The assumption is: A batch script snippet can be named a function when:
|
DOS Batch - Function Tutorial | What it is, why it`s important and how to write your own. |
Call a Function | How to invoke a function? |
Create a Function | What is a function? |
Example - Function with Arguments | An Example showing how it works. |
Example - Calling a Function | An Example showing how it works. |
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? |
Parsing Function Arguments | How to retrieve function arguments within the function? |
Passing Function Arguments | How to pass arguments to the function? |
Recursive Functions | Tadaaah!!! |
Returning Local Variables | How to pass return values over the ENDLOCAL barrier? |
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. |
Summary | Defining a standard format for a DOS batch function |
Description: | A function can be called with the CALL command followed by the function label. | ||
Script: |
|
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: |
|
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: | Download: BatchTutoFunc2.bat
|
||
Script Output: |
|
Description: | The use of batch functions will divide the script into two sections.
|
||
Script: | Download: BatchTutoFunc1.bat
|
||
Script Output: |
|
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:
|
||
Script: | Download: BatchTutoFunc3.bat
|
||
Script Output: |
|
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: | Download: BatchTutoFunc4.bat
|
||
Script Output: |
|
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: |
|
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: |
|
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 stops 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: | Download: BatchTutoFuncRecurs.bat
|
||
Script Output: |
|
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: | Download: BatchTutoFunc5.bat
|
||
Script Output: |
|
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: |
|
||
Script: |
|
||
Script Output: |
|
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: |
|
||
Script: |
|
||
Script Output: |
|
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: | Download: BatchFunctionTmpl.bat
|