17

I understand both link to labels in the code, but what is the difference?

@echo off
:top
echo I love StackOverflow.com
goto :top

@echo off
:top
echo I love StackOverflow.com
call :top

Thank you in advance!

Crazy Redd
  • 435
  • 1
  • 5
  • 18

3 Answers3

27

The example you gave won't really show the difference between the two.

  • goto - goes to the label.
  • call - goes to the label and then returns to the caller when the code is complete.

In your example, as your code is never complete, it never returns to the caller.

The only difference you may see, is that the call version would eventually crash when the list of "where to return to" will become so big until it "fills up" the memory.

To see how the call command can be used properly: http://ss64.com/nt/call.html

Olivia Stork
  • 4,660
  • 5
  • 27
  • 40
D G
  • 699
  • 7
  • 7
4

In your example, very little - except the call version will eventually crash.

goto transfers execution to the label specified; execution continues from that point.

call also transfers execution to the label but when the processing reaches an exit or end-of-physical-file, execution is transferred back to the instruction directly after the call instruction.

call also allows parameters to be passed. As far as the subroutine that is the target of the call, its %1... are the parameters provided by the call, not as provided as command-line parameters to the batch procedure.

You can call an external batch or executable, and at the end of that called routine, execution will resume with the instruction after the call. goto will simply execute the target, and completely forget where it was in the original batch

Magoo
  • 77,302
  • 8
  • 62
  • 84
  • 2
    CALL also establishes a new scope for SETLOCAL / ENDLOCAL commands. ENDLOCAL cannot release variables that were defined before the CALL. Also, there is an implicit ENDLOCAL when returning from a CALL. – dbenham Dec 22 '15 at 17:15
0

with call there is a return:

for /l %%i in (1,2,10) do call :process %%i
pause
Exit /b

:process
echo subroutine - %1
Stephan
  • 53,940
  • 10
  • 58
  • 91