Sometimes you just want a plain example of a subfunction ergo Sub-Routine in a command prompt batch file to understand how it works!The CALL command starts a new batch file context along with any parameters specified. When the end of the second batch file is reached or when EXIT is used, control returns immediately after the first CALL statement. Contents: 1.) ... Simple example of a sub-function (sub-routine)!
|
(Image-1) Run the example batch and sub-function on MS Windows all OS! |
2.) More information about CALL calling sub-functions!
When command extensions are enabled, the CALL command is modified as follows:
The CALL command now accepts markers as jump targets. The syntax is: CALL :marker arguments
With:
call /?
Get more help on the CALL command, just practice, because practice makes perfect!
`CALL` is used in batch files to call another batch file or a sub-function within the same batch file. Here is some more information about using `CALL` to call subfunctions:
1. Syntax:
The syntax for calling a subfunction is `call :label`, where `label` is the name of the subfunction. The `:` before the label name identifies it as a subfunction within the same batch file.
2. Parameter Passing:
You can pass parameters to subfunctions by specifying them after the label name, e.g. `call :add 5 3`. Within the subfunction, these parameters can then be accessed via `%1`, `%2`, `%3`, etc.
3. Return Values:
There is no explicit way to get return values from subfunctions in batch files. Typically, values are passed to the call site via environment variables or file operations.
4. End of a subfunction:
The end of a subfunction is marked by the keyword `goto :eof`. `:eof` is a special label that represents the end of a file in a batch file. After calling a subfunction with `CALL`, execution continues at the point immediately after the `CALL`.
5. Local Variables:
Variables defined within a subfunction are local by default and only apply within that function. To make a variable global and visible outside the subfunction, it must be edited with `setlocal enabledelayedexpansion` and `endlocal`.
6. Multiple levels of subfunctions:
You can call subfunctions within subfunctions, and the same principles apply to parameter passing and calling.
Using subfunctions with `CALL` allows batch scripts to be structured and modularized, improving readability and maintainability.
FAQ 140: Updated on: 13 April 2024 10:34