0
0
Goprogramming~10 mins

Pointer behavior in functions in Go - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Pointer behavior in functions
Function called with pointer
Function receives pointer
Function modifies value via pointer
Return to caller
Caller sees updated value
When a function receives a pointer, it can change the original variable's value by accessing it through the pointer.
Execution Sample
Go
package main
import "fmt"
func changeValue(x *int) {
  *x = 10
}
func main() {
  a := 5
  changeValue(&a)
  fmt.Println(a)
}
This code shows how passing a pointer to a function allows the function to modify the original variable.
Execution Table
StepVariableValueActionOutput
1a5Declare a with value 5
2changeValue(&a)pointer to aCall function with address of a
3*x5Inside function, dereference pointer x
4*x10Set value pointed by x to 10
5ReturnFunction returns to main
6a10a is updated because pointer changed it10
💡 Function ends and main prints updated value of a
Variable Tracker
VariableStartAfter changeValue callFinal
a51010
x (pointer)nilpoints to apoints to a
Key Moments - 3 Insights
Why does the value of 'a' change after calling changeValue?
Because the function receives a pointer to 'a' and modifies the value at that address (see execution_table step 4).
What happens if we pass 'a' instead of '&a' to changeValue?
It would cause a compile error because the function expects a pointer (*int), but 'a' is an int.
What does '*x = 10' mean inside the function?
It means 'set the value at the address x points to as 10', changing the original variable (see execution_table step 4).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'a' after step 4?
A0
B5
C10
DUndefined
💡 Hint
Check the 'Value' column for 'a' after step 4 in execution_table.
At which step does the function modify the value pointed by 'x'?
AStep 2
BStep 4
CStep 3
DStep 5
💡 Hint
Look at the 'Action' column describing '*x = 10' in execution_table.
If we passed 'a' instead of '&a' to changeValue, what would happen to 'a'?
AIt would cause a compile error
BIt would change to 10
CIt would remain 5
DIt would become zero
💡 Hint
changeValue expects *int; passing int 'a' causes a type mismatch compile error.
Concept Snapshot
Pointer behavior in functions in Go:
- Pass variable address using &var
- Function receives pointer *type
- Use *pointer to access/change original value
- Changes via pointer affect caller's variable
- Passing value copies data; no effect on original
Full Transcript
This example shows how Go functions can change variables by receiving pointers. The variable 'a' starts at 5. We call changeValue with &a, passing the address of 'a'. Inside changeValue, the pointer 'x' points to 'a'. When we set *x = 10, we change the value at that address. After the function returns, 'a' is updated to 10. Trying to pass 'a' directly would cause a compile error because the function expects a pointer. This demonstrates how pointers allow functions to modify original variables.