Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Batch - What is the difference between CALL and GOTO?

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!

like image 427
Crazy Redd Avatar asked Dec 21 '15 18:12

Crazy Redd


2 Answers

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

like image 123
D G Avatar answered Nov 12 '22 08:11

D G


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

like image 40
Magoo Avatar answered Nov 12 '22 09:11

Magoo