Concept Flow - Return values
Start function call
Execute function body
Return value(s)
Receive returned value(s)
Continue program
This flow shows how a function runs, sends back value(s), and the program uses them.
package main import "fmt" func add(a, b int) int { return a + b } func main() { fmt.Println(add(3, 4)) }
| Step | Action | Variables | Return Value | Output |
|---|---|---|---|---|
| 1 | Call add(3, 4) | a=3, b=4 | ||
| 2 | Calculate a + b | a=3, b=4 | 7 | |
| 3 | Return 7 to caller | a=3, b=4 | 7 | |
| 4 | Print returned value | 7 | ||
| 5 | Program ends |
| Variable | Start | After Step 1 | After Step 2 | Final |
|---|---|---|---|---|
| a | - | 3 | 3 | 3 |
| b | - | 4 | 4 | 4 |
| return value | - | 7 | 7 |
func functionName(params) returnType {
// code
return value
}
- Function runs code then returns value.
- Caller receives and can use returned value.
- Return ends function execution.