Batch file functions

In this tutorial, you will learn about batch file functions and how functions are written in batch file programs.

Batch script functions: Introduction
Function definition
Function call
Basic function example
Function with parameters
Function with return values

batch function

Batch file functions: Introduction


A function is a single comprehensive unit (self-contained block) containing a block of code that performs a specific task. The function performs the same task when called which avoids the need of rewriting the same code again and again.

Like in any other programming language, batch file functions also have:

  • function definition
  • function call

NOTE: When defining the main program is to ensure that the statement EXIT /B %ERRORLEVEL% is used in the main program to separate the code of the main program from the function.

Batch File Function Definition

-Syntax

:function_name 
Some_Operational_Code 
EXIT /B 0

As shown in syntax, a function definition in batch file starts with the declaration of a function with a label. Next is the body of function where we write codes to achieve a certain task. At the end  EXIT /B 0 is to ensure successful termination or proper exit of the functions.

Batch File Functions Call


Like in every other programming language, to use the defined functions, we need to call it in the main program. For this, CALL command is used.

Learn more details about all batch file commands here.

-Syntax

:: To call function without parameters 
CALL :function_name
 
:: To call function with parameters
CALL :function_name param1, param2,...,paramN

:: To call function with return values
CALL :function_name return_value1,return_value2,..,return_valueN

So, this is how the function is called in batch files.

In the case of batch file functions with parameters, a tilde(~) sign is used to access them in the main program along with the positional number of parameter.

Similarly, in the case of batch file functions with return values, return values are set in the function using the set command and the tilde(~) character along with the positional number of the return values.

Here is the batch file program to demonstrate the use of the basic function, function with parameters and function with return values.

 [adsense1]

Batch file function example: Program with basic function

@echo OFF
CALL :basic_function 
EXIT /B %ERRORLEVEL% 
:basic_function
SET n=Harry
ECHO My name is %n%
PAUSE
EXIT /B 0

Output

Note that the EXIT /B %ERRORLEVEL% is used to separate the function from the main program and PAUSE is used to hold the screen, else output console will vanish within nanoseconds.

Batch file functions basic output

Batch file function example: Program demonstrating function with parameters

@echo OFF
CALL :param_function 20, 400
EXIT /B %ERRORLEVEL% 
:param_function
ECHO The square of %~1 is %~2
PAUSE
EXIT /B 0

Output

batch file functions with parameter output

Here tilde(~) sign is used to access the parameter’s value followed by the position of the parameter. Hence, ~1 is for accessing first parameter and ~2 for the second.

Batch file function example: Program demonstrating function with return values

@echo OFF
CALL :retun_value_function ret_val1,ret_val2
ECHO The square root of %ret_val1% is %ret_val2%
PAUSE
EXIT /B %ERRORLEVEL% 
:return_value_function
SET %~1=100
SET %~2=10
EXIT /B 0

Output

batch file function return value output

Here SET command along with tilde(~) sign is used to set the return values depending on the position followed by tilde sign.