Yes. Subroutines typically call other subroutines which call other subroutines.... and so on.
But, even if it calls another subroutine, it eventually gets control back again. Then, after it finishes, it returns to its caller.
The problem is illustrated at right.
The main
routine is written
to call a useful subroutine
sub
at several locations
in the code.
But sub
is written to return
to only one location.
Usually this will not work.
In the past, before the concept was completely understood, hardware support for subroutines was missing. Various nasty tricks were used to implement the idea.
What is needed is a method that sends the return address to the subroutine. When the subroutine finishes, it passes control to that return address.
Of course, "passing control to a return address" means to load the PC (program counter) with the return address. The next instruction fetch of the machine cycle will get the instruction from that address.