Software-OK
≡... News | ... Home | ... FAQ | Impressum | Contact | Listed at | Thank you |

  
HOME ► Faq ► FAQ - Command Prompt - Windows 11, 10, etc. ► ««« »»»

Looking for an example of a sub-function in a batch file?


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)!
2.) ... More information about CALL calling sub-functions!



1.) Simple example of a sub-function (sub-routine)!

Here is a simple example in which a sub-function is called twice.

The sub-function multiplies two numbers and outputs the result

Below in the picture you can see:  ►►  the output


sub_function.bat

ECHO off
 
::Call under function 2 times 
 
CALL :sub_function 2 5
CALL :sub_function 5 10
ECHO end
 
::Waiting for keypress
PAUSE
 
::END
GOTO :eof
 
:sub_function
SET /a result=%1*%2
 
ECHO %1 mul %2 is %result%
EXIT /B
 


GOTO
jumps to a specific point or end of the batch file


Example of subfunction output in a batch file





(Image-1) Run the example batch and sub-function on MS Windows all OS!
Run the example batch and sub-function on MS Windows all OS!

-
▲ Back to the top ▲



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!



See also:

►► Using batch file to start multiple programs!
►► How can I run commands in batch files at intervals?
►► Generate random numbers in the command prompt or batch file!
►► How do I create a batch file on / for MS Windows?





 
`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 08:34 Windows
Windows-Console

What are command prompt internal commands?


The Windows CMD has internal commands that are in the command prompt itself, this does not start another exe that is started separately The external commands
Windows-Console

Rename JPEG to JPG via command line, but how?


Sometimes you just want to rename JPEG to JPG to keep things in order, you can do that easily from the command line on MS Windows Desktop and Server OS
Windows-Console

Searching in files by string via command prompt?


The classic search in files for a character string via the command prompt is hardly used anymore, but it is still available on Windows 11, 10, In order
Windows-Console

Deleting files with no content using a batch file, but how?


Deleting files without content with a batch file, command prompt, or via the address bar in File Explorer is quite popular Here you can find out. that
Windows-Console

Is there a list of WMIC commands on MS Windows?


It is quite easy to have the MMIC commands listed on all Microsoft Windows desktop and MS servers and to obtain further information WMIC is deprecated The
Windows-Console

Realize dark mode switch with a batch file?


Why not simply implement the dark mode in Microsoft Windows 11 or 10 with a batch file at the push of a button and without an administrator query Everyone
Windows-Console

How and why do command lines work in CMD.EXE?


The command line in CMD.EXE, often also called console or terminal, is a text-based interface within the operating system that can make things a lot easier

»»

  My question is not there in the FAQ
Asked questions on this answer:
Keywords: windows, console, looking, example, subfunction, batch, file, sometimes, just, want, plain, subroutine, command, prompt, understand, works, call, starts, Questions, Answers, Software




  

  + Freeware
  + Order on the PC
  + File management
  + Automation
  + Office Tools
  + PC testing tools
  + Decoration and fun
  + Desktop-Clocks
  + Security

  + SoftwareOK Pages
  + Micro Staff
  + Freeware-1
  + Freeware-2
  + Freeware-3
  + FAQ
  + Downloads

  + Top
  + Desktop-OK
  + The Quad Explorer
  + Don't Sleep
  + Win-Scan-2-PDF
  + Quick-Text-Past
  + Print Folder Tree
  + Find Same Images
  + Experience-Index-OK
  + Font-View-OK


  + Freeware
  + Bitte.Wenden
  + CoronaKO
  + MatriX.CoronaKO
  + Find.Same.Images.OK
  + 3D.Benchmark.OK
  + PhotoResizerOK
  + Office.Files.Images
  + Film.Strip.Explorer
  + WinScan2PDF
  + PAD-s


Home | Thanks | Contact | Link me | FAQ | Settings | Windows 10 | gc24b | English-AV | Impressum | Translate | PayPal | PAD-s

 © 2025 by Nenad Hrg softwareok.de • softwareok.com • softwareok.com • softwareok.eu


► Desktop shortcut for the calculator on the Windows 10/11 desktop! ◄
► Where is the Rating in Windows 8.1, 10 (Windows Experience Index)? ◄
► Memory is less than the amount of installed memory in Windows 8.1 / 10! ◄
► Anonymous or private browsing on Windows! ◄


This website does not store personal data. However, third-party providers are used to display ads,
which are managed by Google and comply with the IAB Transparency and Consent Framework (IAB-TCF).
The CMP ID is 300 and can be individually customized at the bottom of the page.
more Infos & Privacy Policy

....