Bird
Raised Fist0
Goprogramming~10 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What does passing a pointer to a function in Go allow you to do?
easy
A. Automatically convert the variable to a string
B. Create a copy of the variable that cannot be changed
C. Modify the original variable's value inside the function
D. Prevent the function from accessing the variable

Solution

  1. Step 1: Understand pointer passing

    Passing a pointer sends the address of the variable, not a copy.
  2. Step 2: Effect on original variable

    Inside the function, using the pointer lets you change the original variable's value.
  3. Final Answer:

    Modify the original variable's value inside the function -> Option C
  4. Quick Check:

    Pointer lets function change original variable [OK]
Hint: Pointers let functions change original variables directly [OK]
Common Mistakes:
  • Thinking pointers create copies instead of referencing original
  • Confusing pointer with value passing
  • Assuming pointers prevent modification
2. Which of the following is the correct way to declare a function that takes a pointer to an int in Go?
easy
A. func updateValue(val *int) {}
B. func updateValue(val int) {}
C. func updateValue(*val int) {}
D. func updateValue(&val int) {}

Solution

  1. Step 1: Recall pointer syntax in function parameters

    In Go, a pointer parameter is declared with * before the type, like *int.
  2. Step 2: Check each option

    func updateValue(val *int) {} correctly uses *int as the parameter type.
  3. Final Answer:

    func updateValue(val *int) {} -> Option A
  4. Quick Check:

    Pointer parameter syntax = *type [OK]
Hint: Pointer parameters use * before type, not variable name [OK]
Common Mistakes:
  • Placing * before variable name instead of type
  • Using & in parameter declaration
  • Omitting * for pointer type
3. What will be the output of this Go program?
package main
import "fmt"
func change(val *int) {
    *val = 10
}
func main() {
    x := 5
    change(&x)
    fmt.Println(x)
}
medium
A. 5
B. 10
C. 0
D. Compilation error

Solution

  1. Step 1: Understand pointer usage in change function

    The function receives a pointer to x and sets the value at that address to 10.
  2. Step 2: Trace main function execution

    x starts as 5, change(&x) updates x to 10, then fmt.Println prints x.
  3. Final Answer:

    10 -> Option B
  4. Quick Check:

    Pointer lets function update x to 10 [OK]
Hint: Pointer lets function change original variable value [OK]
Common Mistakes:
  • Expecting original value 5 to print
  • Confusing pointer dereference syntax
  • Thinking function works without & operator
4. Identify the error in this Go code snippet:
func update(val *int) {
    val = 20
}

func main() {
    x := 5
    update(&x)
    fmt.Println(x)
}
medium
A. Cannot assign int to pointer variable directly
B. Missing & operator when calling update
C. Function update should return int
D. x is not initialized

Solution

  1. Step 1: Analyze assignment inside update function

    val is a pointer to int, but val = 20 tries to assign int to pointer variable, which is invalid.
  2. Step 2: Correct way to change value via pointer

    Use *val = 20 to assign 20 to the value pointed by val.
  3. Final Answer:

    Cannot assign int to pointer variable directly -> Option A
  4. Quick Check:

    Assign to *val, not val [OK]
Hint: Assign to *pointer, not pointer variable itself [OK]
Common Mistakes:
  • Assigning value directly to pointer variable
  • Forgetting to dereference pointer
  • Confusing pointer and value types
5. Given this Go code, what will be the output?
package main
import "fmt"
func swap(a, b *int) {
    *a, *b = *b, *a
}
func main() {
    x, y := 1, 2
    swap(&x, &y)
    fmt.Println(x, y)
}
hard
A. Compilation error
B. 1 2
C. 0 0
D. 2 1

Solution

  1. Step 1: Understand swap function behavior

    swap takes pointers to two ints and swaps their values using pointer dereferencing.
  2. Step 2: Trace main function execution

    x=1, y=2 initially; swap(&x, &y) swaps values, so x becomes 2 and y becomes 1.
  3. Final Answer:

    2 1 -> Option D
  4. Quick Check:

    Pointer swap exchanges values correctly [OK]
Hint: Swap values by dereferencing pointers [OK]
Common Mistakes:
  • Expecting original order after swap
  • Confusing pointer addresses with values
  • Not using * to dereference pointers
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard