Batch Beta
Please Login to get full access and to disable ads.

Join the forum, it's quick and easy

Batch Beta
Please Login to get full access and to disable ads.
Batch Beta
Would you like to react to this message? Create an account in a few clicks or log in to continue.

Thanks to awestom for removing those ads!

Staff Selections are closed! Thanks for participating!
Welcome to the new admins and mods!
About the ads, yeah. We reached our 250'th post and the ads kicked on. In order to disable them I need to purchase a ad removal feature. Now, I don't have credits and in order to get one I need donations. Thanks

You are not connected. Please login or register

How to use subroutines

2 posters

Go down  Message [Page 1 of 1]

1How to use subroutines Empty How to use subroutines Thu Jan 13, 2011 4:30 pm

Forum_Reader

Forum_Reader
Jr Admin
Jr Admin

In visual basic programming, you can use subroutines to perform different operations many times, at different times, and to pass arguments to be used in those subs. In batch, this is a little bit different.

Calling a sub is quite easy. It's just like the GOTO command!

Syntax:
Code:
Call :labelname [Parameters]
Unlike the GOTO command, the colon (Smile is not optional in front of the label's name.

In calling a sub, parameters (%1, %2, %3, etc.) are optional, just like a regular batch file, but can be useful (also like a regular batch file). If the batch file already has parameters and you call a sub, the subroutine's parameters will be used instead of the batch file's until the subroutine has completed.

Here is an example of the proper usage of subs with parameters being passed.

Sub-Example.bat
Code:
@echo off
call :label 1
Echo Step 2
call :label 3
pause
goto :eof

:label
Echo Step %1

Subs work on a simple concept: The code is preformed from the subroutine's beginning until it reaches the end of the file by either being sent there with a GOTO command (goto :eof), or it physically reaches the end of the file, then the script continues on at the next command after the call command.

Now, that simple script just says Step 1, Step 2, Step 3, doesn't save very many lines. In fact, I can do it in 5 lines (not counting @echo off) and it's probably possible in less.

Pseudo-Sub-Example.bat
Code:
@echo off
:loop
set /a step+=1
Echo Step %step%
if %step%==3 pause & exit
goto loop

That makes batch subroutines seem pretty inefficient, right? Well, in that sort of situation, I would agree with you. However, if you have multiple parts of your script that would be preforming the EXACT SAME CODE at different points in time, then it can be very useful.

For example: Subs.bat
Code:
@echo off
Rem This will check if a .doc file exists periodically and delete them if they exist.
Echo I like .txt files, but I hate .doc files.
Call :checker
Echo Let's do some simple math...
set /p ans=1+1^=?
if "%ans%"=="2" (echo correct) else (echo YOU FAIL!)
Call :checker
pause > nul
goto :eof

:checker
dir /b *.doc 2> nul
If %errorlevel%==1 (Echo YAY!) else (Del *.doc & echo YAY!)

In that script, it checks if .doc files exist, and if they do, delete them all. Now, if subroutines didn't exist (or you were just using regular GOTOs), it would be pretty hard to get the same spot you left off. Here is an example of the same code, except reworked so it does the exact same thing minus the call commands:

Pseudo-Subs.bat
Code:
@echo off
Rem This will check if a .doc file exists periodically and delete them if they exist.
Echo I like .txt files, but I hate .doc files.
Goto :checker
:First
Echo Let's do some simple math...
set /p ans=1+1^=?
if "%ans%"=="2" (echo correct) else (echo YOU FAIL!)
Goto :checker
:Second
pause > nul
goto :eof

:checker
set /a count+=1
dir /b *.doc 2> nul
If %errorlevel%==1 (Echo YAY!) else (Del *.doc & echo YAY!)
If %count%==1 goto First
If %count%==2 goto Second

Now, that's a difference of 5 lines. But the line difference will increase by 2 each time you go to the subroutine. That can get pretty annoying, right? For each time you want to go to :checker, you have to place another label and if statement. If you're doing it 50+ times, it can be really tedious (doing an extra 100 lines of code).

Now does it seem so inefficient?
Spoiler:

2How to use subroutines Empty Re: How to use subroutines Thu Jan 13, 2011 4:47 pm

-xPloit

-xPloit
Moderators
Moderators

could you provide an example of how to call a sub and give it parameters? I've never seen that done before, and if it's possible, then my guess is that you could create functions with batch...which would make it much more useable in certain situations.

3How to use subroutines Empty Re: How to use subroutines Thu Jan 13, 2011 5:07 pm

Forum_Reader

Forum_Reader
Jr Admin
Jr Admin

-xPloit wrote:could you provide an example of how to call a sub and give it parameters? I've never seen that done before, and if it's possible, then my guess is that you could create functions with batch...which would make it much more useable in certain situations.
Already in the tut, bro.

4How to use subroutines Empty Re: How to use subroutines Thu Jan 13, 2011 5:31 pm

-xPloit

-xPloit
Moderators
Moderators

I looked over it, didn't see. But I found it this time. I'm pretty sure you could use functions in batch then...learn someting new every day! Very Happy

5How to use subroutines Empty Re: How to use subroutines Thu Jan 13, 2011 5:35 pm

Forum_Reader

Forum_Reader
Jr Admin
Jr Admin

-xPloit wrote:I looked over it, didn't see. But I found it this time. I'm pretty sure you could use functions in batch then...learn someting new every day! Very Happy
Subroutines=functions

They're just different words for the same thing.

6How to use subroutines Empty Re: How to use subroutines Thu Jan 13, 2011 5:47 pm

-xPloit

-xPloit
Moderators
Moderators

I disagree. Suroutines are used to do a loop in the middle of other code, usually to cover if statements when they return false. A function is a group of code that, when variable are filled in, can automate similar procedures, but for different things. They usually return a vlaue as well, which can then be used in the subsequent code.

I see a similarity, but they are different. Slightly Wink

7How to use subroutines Empty Re: How to use subroutines Thu Jan 13, 2011 6:42 pm

Forum_Reader

Forum_Reader
Jr Admin
Jr Admin

-xPloit wrote:I disagree. Suroutines are used to do a loop in the middle of other code, usually to cover if statements when they return false. A function is a group of code that, when variable are filled in, can automate similar procedures, but for different things. They usually return a vlaue as well, which can then be used in the subsequent code.

I see a similarity, but they are different. Slightly Wink

I disagree with your statement.

I'm going to break down the word subroutine and make a graphical display based on it.
How to use subroutines Webse

8How to use subroutines Empty Re: How to use subroutines Thu Jan 13, 2011 10:12 pm

-xPloit

-xPloit
Moderators
Moderators

I think of them as different, yet similar, things Razz despite your flow chart

Sponsored content



Back to top  Message [Page 1 of 1]

Permissions in this forum:
You cannot reply to topics in this forum