Menu
Coddy logo textTech

Using Pointers in Functions

Part of the Fundamentals section of Coddy's GO journey — lesson 64 of 109.

Functions can accept and modify variables through pointers. This allows functions to change the original value, not just a copy.

Create a function that modifies a value through a pointer:

func main() {
    number := 10
    fmt.Println("Before:", number)
    
    // Pass the address of number
    doubleValue(&number)
    
    fmt.Println("After:", number)
}

func doubleValue(num *int) {
    // Modify the value at the pointer's address
    *num = *num * 2
}

The output shows the value changed by the function:

Before: 10
After: 20

When we pass &number, the function receives a pointer and can modify the original variable.

challenge icon

Challenge

Beginner

In this challenge, you'll practice using pointers in functions. You need to complete a function that takes a pointer to a string and modifies the string value to be uppercase.

The function makeUppercase is already defined for you. Your task is to modify the string that the pointer points to by using the strings.ToUpper() function.

Cheat sheet

Functions can modify variables through pointers by receiving the memory address instead of a copy:

func main() {
    number := 10
    
    // Pass the address of number using &
    doubleValue(&number)
    
    fmt.Println(number) // Output: 20
}

func doubleValue(num *int) {
    // Modify the value at the pointer's address using *
    *num = *num * 2
}

Key pointer syntax:

  • &variable - gets the address of a variable
  • *pointer - dereferences a pointer to access/modify the value
  • *type - declares a pointer parameter type

Try it yourself

package main

import (
	"fmt"
	"strings"
)

// makeUppercase takes a pointer to a string and changes the string to uppercase
func makeUppercase(strPtr *string) {
	// TODO: Use strings.ToUpper() to change the string that strPtr points to
	// Hint: You need to dereference the pointer to access the string value
	? = strings.ToUpper(?)
	
}

func main() {
	// We already have a string variable
	message := "hello, world"
	
	// Print the original message
	fmt.Printf("Original message: %s\n", message)
	
	// Call makeUppercase with the address of message
	makeUppercase(&message)
	
	// Print the modified message
	fmt.Printf("Modified message: %s\n", message)
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals