How to Create a Function in Batch Script

MD Aminul Islam Feb 02, 2024
How to Create a Function in Batch Script

You need not write a code for the same task again and again if you create a function for that specific task. This article will discuss how we can create a function in Batch Script.

Create a Function in Batch Script

The general format for creating a function is shown below.

:function_name

And the general format to call this function is by using a CALL keyword with :function_name, like the one below.

CALL :function_name

Now let’s see some examples with proper explanations to make this easier to understand.

In our below example, we just created a function that prints the message Hello! It's the function.... Our code for this example will look like this:

@echo off
CALL :showmessage
EXIT /B %ERRORLEVEL%

:showmessage
ECHO Hello! It's the function...
EXIT /B 0

You can notice that we wrote the line EXIT /B %ERRORLEVEL%. This line aims to exit the program after the function has done its job; otherwise, the program will continue executing the function.

And the line EXIT /B 0 was created to exit the function.

After running the code, you will get the below output.

Output:

Hello! It's the function...

Call a Function With Parameters

In our next example, we will call a function with parameters.

The general format for calling a function is shown below:

CALL :function_name %parameter%

And the parameter can be accessed inside the function by using the index of parameters like %~1.

Our code for the example will look like this:

@echo off
SET FuncVar=10
CALL :showvar %FuncVar%
EXIT /B %ERRORLEVEL%

:showvar
ECHO Your passed variable is %~1
EXIT /B 0

In the example, we declared a variable named FuncVar with the value 10. After that, we called the function and passed this variable as a parameter.

In the function, we just printed the parameter passed to the function. Now the output will be like the following.

Output:

Your passed variable is 10

Call a Function With Multiple Parameters

When working with multiple parameters, you have to follow the format given below:

CALL :function_name %parameter1% %parameter2% %parameter3%

In our next example, we passed multiple variables as parameters in the function. The example code will be,

@echo off
SET FuncVar1=10
SET FuncVar2=50
CALL :showvar %FuncVar1% %FuncVar2%
EXIT /B %ERRORLEVEL%

:showvar
ECHO Your 1st parameter is %~1
ECHO Your 2nd parameter is %~2
EXIT /B 0

You can notice that we accessed two parameters depending on the index of the parameters like %~1 and %~2. After running the code, you will get the following output.

Output:

Your 1st parameter is 10
Your 2nd parameter is 50
Note
The codes we shared in this article are written in Batch and will only work for the Windows CMD.
MD Aminul Islam avatar MD Aminul Islam avatar

Aminul Is an Expert Technical Writer and Full-Stack Developer. He has hands-on working experience on numerous Developer Platforms and SAAS startups. He is highly skilled in numerous Programming languages and Frameworks. He can write professional technical articles like Reviews, Programming, Documentation, SOP, User manual, Whitepaper, etc.

LinkedIn