Discussion:
Call Subroutine from another Subroutine
(too old to reply)
Zaidy036
2022-06-29 18:27:49 UTC
Permalink
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
2022-06-29 19:29:06 UTC
Permalink
Post by Zaidy036
Is it possible to call a Subroutine from inside another Subroutine?
:: 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?
--
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
2022-06-29 19:36:17 UTC
Permalink
Post by Kenny McCormack
Post by Zaidy036
Is it possible to call a Subroutine from inside another Subroutine?
:: 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
2022-06-29 20:10:59 UTC
Permalink
Post by Zaidy036
Post by Zaidy036
Is it possible to call a Subroutine from inside another Subroutine?
:: 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.
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
2022-06-29 20:32:19 UTC
Permalink
Post by Tim Rude
@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
---
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.
I had no idea you could have internal subroutines like that.

You can even use recursion.

Thanks for the example!
Kenny McCormack
2022-06-30 12:11:05 UTC
Permalink
In article <t9icsl$1hk4t$***@dont-email.me>, Dallas <***@texas.usa> wrote:
...
Post by Dallas
Post by Tim Rude
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
2022-06-30 13:57:15 UTC
Permalink
Post by Kenny McCormack
Post by Dallas
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 -
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
2022-06-30 14:14:59 UTC
Permalink
Post by Dallas
Post by Kenny McCormack
Post by Dallas
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 -
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.
Actually, there is a typo in my previous post.
Post by Dallas
Post by Kenny McCormack
%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 -
Zaidy036
2022-06-29 21:27:12 UTC
Permalink
Post by Tim Rude
Post by Zaidy036
Post by Zaidy036
Is it possible to call a Subroutine from inside another Subroutine?
:: 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.
---
@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
---
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.
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.
Loading...