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: 20When we pass &number, the function receives a pointer and can modify the original variable.
Challenge
BeginnerIn 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)
}This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Comparison & Logical Operators
Comparison Operators - Part 1Comparison Operators - Part 2Logical AND OperatorLogical OR OperatorLogical NOT OperatorOperator Precedence BasicsRecap - Making Comparisons7Control Flow: Loops
What The `for` Loop ExplainedFor Loop - BasicFor Loop - Condition OnlyThe `break` KeywordThe `continue` KeywordNested LoopsRecap - Repeating Actions2Variables and Basic Data Types
What is a variableType Inference with `:=`Integers (int)Floating-Point NumbersBooleansStringsZero ValuesConstantsNaming ConventionsRecap - Variables and Types5Basic Input/Output
Formatted OutputFormat VerbsPrinting TypesGetting Basic User InputRecap - Input and Output8Functions
Understanding FunctionsDeclaring a FunctionCalling FunctionsFunction ParametersReturning a Single ValueReturning Multiple ValuesNamed Return ValuesFunction Scope BasicsRecap - Creating Reusable Code3Basic Operators
Arithmetic OperatorsDivision OperatorThe Modulo OperatorAssignment OperatorAugmented Assignment OperatorsIncrement and DecrementRecap - Calculations6Control Flow: Conditionals
The `if` StatementThe `else` KeywordThe `else if` KeywordVariable Shadowing in `if`Initializing VariablesThe `switch` StatementSwitch with ExpressionsSwitch without ExpressionThe `fallthrough` KeywordRecap - Making Decisions9Pointers
What is a Pointer?Declaring Pointer VariablesThe Address-Of OperatorDereferencing PointersUsing Pointers in FunctionsNil PointersRecap - Understanding Pointers