How do method calls work using stack in Go
A method call is a transfer of control of a program. When a method is called, the execution of the current program stops, and control is transferred to the method. The address of the previous program’s next instruction is stored to return once the execution of the method completes. A system stack is used to keep track of each return address.
A stack follows the LIFO or last-in-first-out order. Each subsequent return address is placed on top of the stack. When its relevant method call finishes execution, the return address is popped from the stack, and the program jumps onto that address to continue execution.
The illustration below shows how a stack works:
Explanation
Step 1: The stack is initially empty.
Step 2: A push operation adds an item to the top of the stack. The top shifts one place above its previous position.
Step 3: Each subsequent push adds an item to the top of the stack.
Step 4: A pop command removes an item from the top-1 position of the stack. The top shifts one place below its previous position.
Example
Consider the program below:
package mainimport "fmt"func function_one() {fmt.Println("Inside Function One")function_two()fmt.Println("Back in Function One")}func function_two() {fmt.Println("Inside Function Two")}func main() {fmt.Println("First Line in Main")function_one()fmt.Println("Second Line in Main")}
Explanation
- The program starts execution from the main function in
Line 14. - It outputs the statement in
Line 15. - A method call occurs in
Line 16. - At this instance, the next instruction address in
Line 17is placed in thestack. The program keeps track of this address so that it returns to it once the method call finishes its execution.
The illustration below shows the state of the stack and program control at this instance:
We will use a random value to represent the address of each instruction in the
stack.
- Program control is transferred to
function_one, which begins execution fromLine 4. - It outputs the statement in
Line 5. - Another method call occurs in
Line 6. - The next instruction address in
Line 7is placed in thestack. - Program control is transferred to
function_two.
The illustration below shows the updated state of the stack and program control:
function_twobegins execution fromLine 10.- It outputs the statement in
Line 11. - At this point, the execution of this method completes. It must now jump to the return address.
- The system
stackpops the return address to indicate completion of this method’s execution and transfer control to the previous method.
The illustration below shows the updated state of the stack and program control:
- Control is transferred back to
function_one. - Execution continues from
Line 7(the return address). It outputs the statement. - At this point, the execution of this method completes. It must now jump to the return address.
- The system
stackpops the return address to indicate completion of this method’s execution and transfer control to the previous method.
The illustration below shows the updated state of the stack and program control:
- Control is transferred back to the
main. - It continues execution from
Line 17, and normal flow resumes.
Summary
To conclude:
The next instruction address is placed in the
stackwhen a method is called. This serves as the return address.
When a method finishes execution, the return address is popped from the
stack.
Free Resources