0
0
Goprogramming~10 mins

Function execution flow in Go - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Function execution flow
Start program
Call function
Enter function
Execute function body
Return value
Resume main program
End program
This flow shows how a Go program calls a function, executes its body, returns a value, and then continues.
Execution Sample
Go
package main
import "fmt"
func add(a int, b int) int {
    return a + b
}
func main() {
    fmt.Println(add(3, 4))
}
This Go program defines a function add that sums two numbers and prints the result of add(3, 4).
Execution Table
StepActionFunctionParametersReturn ValueOutput
1Start mainmain---
2Call addadda=3, b=4--
3Execute add bodyadda=3, b=47-
4Return from addmain-7-
5Print resultmain--7
6End mainmain---
💡 Program ends after main finishes execution.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
a-33--
b-44--
return value--77-
Key Moments - 3 Insights
Why does the program pause at step 2 before continuing?
At step 2, the main function calls add, so execution moves inside add to run its code before returning.
What happens to variables a and b after add returns?
Variables a and b exist only inside add; after returning, main does not have them (see step 4).
Why is the return value 7 at step 3?
Because add sums 3 and 4, returning 7 as the result (step 3 shows this calculation).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of 'a' when executing the add function body at step 3?
A3
B4
C7
D-
💡 Hint
Check the 'Parameters' column at step 3 in the execution table.
At which step does the program print the output?
AStep 2
BStep 3
CStep 5
DStep 6
💡 Hint
Look at the 'Output' column in the execution table.
If add returned a + b + 1 instead, what would be the return value at step 3?
A7
B8
C6
D4
💡 Hint
Add 3 + 4 + 1 to find the new return value.
Concept Snapshot
Function execution flow in Go:
- main calls a function
- execution jumps to function
- function runs with parameters
- function returns a value
- main resumes with return value
- program ends after main finishes
Full Transcript
This example shows how a Go program runs a function. The main function starts and calls add with 3 and 4. Execution moves inside add, where a and b get values 3 and 4. The function adds them and returns 7. Then main receives 7 and prints it. Finally, main ends and the program stops.