Call Subroutine from another Subroutine

6 views
Skip to first unread message

Zaidy036

unread,
Jun 29, 2022, 2:27:53 PMJun 29
to
Is it possible to call a Subroutine from inside another Subroutine?

Example:
:: Main batch
CALL :_One
some commands
CALL :_TWO
some commands
EXIT or CMD /K

:_One
some commands
GOTO :EOF

:_Two
some commands
CALL :_One <------ Will not work so how can this be done?
GOTO :EOF

Kenny McCormack

unread,
Jun 29, 2022, 3:29:07 PMJun 29
to
In article <t9i5j6$1gn46$1...@dont-email.me>,
I think it should work. What happens when you try it?

--
The randomly chosen signature file that would have appeared here is more than 4
lines long. As such, it violates one or more Usenet RFCs. In order to remain
in compliance with said RFCs, the actual sig can be found at the following URL:
http://user.xmission.com/~gazelle/Sigs/ForFoxViewers

Zaidy036

unread,
Jun 29, 2022, 3:36:20 PMJun 29
to
On 6/29/2022 3:29 PM, Kenny McCormack wrote:
> In article <t9i5j6$1gn46$1...@dont-email.me>,
> Zaidy036 <Zaid...@air.isp.spam> wrote:
>> Is it possible to call a Subroutine from inside another Subroutine?
>>
>> Example:
>> :: Main batch
>> CALL :_One
>> some commands
>> CALL :_TWO
>> some commands
>> EXIT or CMD /K
>>
>> :_One
>> some commands
>> GOTO :EOF
>>
>> :_Two
>> some commands
>> CALL :_One <------ Will not work so how can this be done?
>> GOTO :EOF
>
> I think it should work. What happens when you try it?
>
CALL :_TWO does not return to original batch. I think getting confused
by the return to :Two required after the CALL :_One. I tried variations
of POPD and PUSHD which did not work. Batch must keep return some place
for GOTO :EOF from each subroutine but only "room" for one return pointer.

Tim Rude

unread,
Jun 29, 2022, 4:11:04 PMJun 29
to
Works for me like this:

---

@echo off
echo Start of main batch file
call :_One
echo End of main batch file
goto :EOF

:_One
echo Stuff from :_One
call :_Two
echo Back in :_One again
goto :EOF

:_Two
echo Stuff from :_Two
call :_Three
echo Back in :_Two again
goto :EOF

:_Three
echo Stuff from :_Three
goto :EOF

---

Output generated is:

Start of main batch file
Stuff from :_One
Stuff from :_Two
Stuff from :_Three
Back in :_Two again
Back in :_One again
End of main batch file

So I nested 3 levels deep and came back out of each level as expected.

Dallas

unread,
Jun 29, 2022, 4:32:23 PMJun 29
to
On 6/29/2022 3:10 PM, Tim Rude wrote:
> Works for me like this:
I had no idea you could have internal subroutines like that.

You can even use recursion.

Thanks for the example!


Zaidy036

unread,
Jun 29, 2022, 5:27:15 PMJun 29
to
Thanks for the example.
My problem may have to do with what program was actually being run in
the subroutine which was Macrium Reflect (free) generating an image with
a time recorder subroutine before and after. Will try again.

Kenny McCormack

unread,
Jun 30, 2022, 8:11:07 AMJun 30
to
In article <t9icsl$1hk4t$1...@dont-email.me>, Dallas <dal...@texas.usa> wrote:
...
> > So I nested 3 levels deep and came back out of each level as expected.
>
>
>I had no idea you could have internal subroutines like that.
>
>You can even use recursion.
>
>Thanks for the example!

Note that, in the olden days (COMMAND.COM), you had to do this yourself -
brute force. I often coded it like:

if %1X==L10X goto L10
echo main routine - Now calling L10 routine...
%0 L10
goto the_end
:L10
echo here we are in L10 subroutinue...
:the_end

I assume that the powers that be at MS noticed that people were doing this
sort of thing, so they codified it into the language (in CMD.EXE versions
of the batch language).

--
"He is exactly as they taught in KGB school: an egoist, a liar, but talented - he
knows the mind of the wrestling-loving, under-educated, authoritarian-admiring
white male populous."
- Malcolm Nance, p59. -

Dallas

unread,
Jun 30, 2022, 9:57:21 AMJun 30
to
On 6/30/2022 7:11 AM, Kenny McCormack wrote:
> In article <t9icsl$1hk4t$1...@dont-email.me>, Dallas <dal...@texas.usa> wrote:
>> I had no idea you could have internal subroutines like that.
>> You can even use recursion.
>> Thanks for the example!
>
> Note that, in the olden days (COMMAND.COM), you had to do this yourself -
> brute force. I often coded it like:
>
> if %1X==L10X goto L10
> echo main routine - Now calling L10 routine...
> %0 L10
> goto the_end
> :L10
> echo here we are in L10 subroutinue...
> :the_end
>
> I assume that the powers that be at MS noticed that people were doing this
> sort of thing, so they codified it into the language (in CMD.EXE versions
> of the batch language).
>

That is how I have been doing it too.
But now I will use the CALL variation as it self-documents so much better.

https://ss64.com/nt/call.html

Kenny McCormack

unread,
Jun 30, 2022, 10:15:01 AMJun 30
to
Actually, there is a typo in my previous post.

>> %0 L10

should, of course, be:

call %0 L10

(since batch files don't, by default, return; hence the invention of "call")

--
"The party of Lincoln has become the party of John Wilkes Booth."

- Carlos Alazraqui -

Reply all
Reply to author
Forward
0 new messages