Sunday, 23 June 2019

Batch Script: Functions


Function is a group of statements used to perform a task.

How to define a function?
A function is defined by using the label statement.

Syntax
:function_name
    Do_something
GOTO :EOF

GOTO :EOF
Transfers the control to the end of batch file.

Hello.bat
@ECHO OFF

REM Call main function
GOTO :main

REM Defining a function that prints "Hello World"
:sayHello
	ECHO Hello World
GOTO :EOF

REM Defining a function that prints "Good Morning"
:sayGoodMorning
	ECHO Good Morning
GOTO :EOF

:main
	CALL :sayHello
	CALL :sayGoodMorning
	ECHO Done....
GOTO :EOF

C:\Users\Public>Hello
Hello World
Good Morning
Done....

As you see Hello.bat file, I defined 3 functions.
a.   sayHello
b.   sayGoodMorning
c.    main


CALL command is used to execute the function.


Previous                                                 Next                                                 Home

No comments:

Post a Comment