0
0
Goprogramming~10 mins

Passing values vs pointers in Go - Visual Side-by-Side Comparison

Choose your learning style9 modes available
Concept Flow - Passing values vs pointers
Start
Call function with value
Function gets a copy
Modify copy inside function
Return from function
Original variable unchanged
Call function with pointer
Function gets address
Modify value via pointer
Return from function
Original variable changed
End
Shows how passing a value copies data, so original stays same, but passing a pointer lets function change original variable.
Execution Sample
Go
package main
import "fmt"
func incVal(x int) { x = x + 1 }
func incPtr(x *int) { *x = *x + 1 }
func main() {
  a := 5
  incVal(a)
  fmt.Println(a)
  incPtr(&a)
  fmt.Println(a)
}
This code shows incrementing an integer by value (no change) and by pointer (changes original).
Execution Table
StepVariable 'a'Function CalledParameter PassedInside FunctionEffect on 'a'Output
15incVal(a)Value 5x = 5 + 1 = 6No change to 'a'
25Return from incVal--'a' still 5
35fmt.Println(a)---5
45incPtr(&a)Pointer to 'a'*x = 5 + 1 = 6'a' changed to 6
56Return from incPtr--'a' now 6
66fmt.Println(a)---6
💡 'main' ends after printing updated 'a' value
Variable Tracker
VariableStartAfter incValAfter incPtrFinal
a5566
x (incVal)-6--
x (incPtr)--6-
Key Moments - 3 Insights
Why does 'a' not change after calling incVal?
Because incVal receives a copy of 'a' (see execution_table step 1), so changes inside do not affect original 'a'.
How does incPtr change the original 'a'?
incPtr receives a pointer to 'a' (step 4), so modifying *x changes the actual 'a' variable.
What does the & operator do in incPtr(&a)?
It gives the address of 'a', so the function can access and modify the original variable.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 2, what is the value of 'a' after incVal returns?
A5
B6
CUndefined
D0
💡 Hint
Check the 'Effect on a' column at step 2 in execution_table.
At which step does 'a' first change its value?
AStep 1
BStep 3
CStep 4
DStep 6
💡 Hint
Look for when 'Effect on a' shows a change in execution_table.
If incVal was changed to accept a pointer instead, what would happen to 'a' after calling incVal(&a)?
A'a' would remain unchanged
B'a' would change like in incPtr
CCompilation error
D'a' would become zero
💡 Hint
Compare incVal and incPtr parameter types and effects in variable_tracker.
Concept Snapshot
Passing by value copies data to function, so original variable stays same.
Passing by pointer sends address, so function can modify original.
Use & to get address, * to access value via pointer.
Changes via pointer affect original variable.
Value passing is safe but no side effects.
Pointer passing allows modification but needs care.
Full Transcript
This visual trace shows how Go functions receive arguments either by value or by pointer. When a value is passed, the function works on a copy, so changes inside do not affect the original variable. When a pointer is passed, the function gets the address and can modify the original variable's value. The example increments an integer first by value (no change) and then by pointer (changes original). The execution table tracks each step, showing variable values and effects. Key moments clarify why the original variable changes only with pointers. The quiz tests understanding of these steps and effects.