Golang program to demonstrate passing of pointers to a function


In this Golang article, we will write programs to demonstrate passing of pointers to a function suing increment and Swapping approach. A pointer stores the address of another variable. For ex- var *a = &b. Here a is the pointer that stores the address of b which means a can access the value of b as it stores the address of b.

Using the Increment Approach

In this method, we will write a Golang program to demonstrate the passing of pointers to a function by using an increment function where we use pointers to point to the variable which is to be incremented.

Algorithm

  • Step 1 − Create a package main and declare fmt(format package) package in the program where main produces executable codes and fmt helps in formatting input and Output.

  • Step 2 − Create a function increment and in that function take a pointer pointing to the integer as an input

  • Step 3 − In the function increment the value pointed by pointer

  • Step 4 − In the main initialize a variable val with value 0 and call the function with pointer to the val

  • Step 5 − Print the incremented value on the console using Println function from fmt package where ln means new line

Example

The following example will show how to create a Golang program to demonstrate passing of pointers to a function using the increment approach

package main

import "fmt"

func increment(ptr *int) {
   *ptr = *ptr + 1
}

func main() {	
   val := 0	
   increment(&val)	
   fmt.Println("The incremented val is given below:")
   fmt.Println(val)
}

Output

The incremented val is given below:
1

Using the Swapping Approach

In this method, we will write a Golang program to demonstrate the passing of pointers to a function using a swap function will be used with the two pointers inside it as inputs which point to the values that will be swapped with each other.

Algorithm

  • Step 1 − Create a package main and declare fmt(format package) package in the program where main produces executable codes and fmt helps in formatting input and Output.

  • Step 2 − Create a function swap with two pointers x and y as inputs. In th function swap the values pointers are pointing and use a temp variable additionally to perform swap operation

  • Step 3 − In the main create these two variables x and y and print their initial values using fmt package

  • Step 4 − Then call the swap function with pointers with x and y variable

  • Step 5 − Now, print the swapped values of x and y on the console

Example

The following example illustrates Golang program to demonstrate passing of pointers to a function using the swapping approach

package main

import "fmt"

func swap(x *int, y *int) {
   temp := *x
   *x = *y
   *y = temp
}

func main() {	
   x := 10
   y := 20
   
   fmt.Printf("Before swap the values of x and y are: x = %d, y = %d\n", x, y)	
   swap(&x, &y)	
   fmt.Printf("After swap the values of x and y are: x = %d, y = %d\n", x, y)
}

Output

Before swap the values of x and y are: x = 10, y = 20
After swap the values of x and y are: x = 20, y = 10

Updating the Integer Pointer Value

In this method, we will write a Golang program to demonstrate the passing of pointers to the function by updating the integer pointer value which will be passed as a pointer in the update method.

Algorithm

  • Step 1 − Create a package main and declare fmt(format package) package in the program where main produces executable codes and fmt helps in formatting input and Output.

  • Step 2 − Create a main function in which set the value of a variable named a and print its value on the console

  • Step 3 − Then, pass the address of a to the external function named change_value

  • Step 4 − Call the function in the main and that function takes an integer pointer and updates its value

  • Step 5 − Then, print the value of a after its updated.

  • Step 6 − The print statement is executed using Println function from the fmt package where ln means new line.

Example

The following example explains how to create Golang program to demonstrate the passing of pointers to the function by updating the integer pointer value

package main

import "fmt"

func main() {
   a := 5
   fmt.Println("Before calling the function the value of a =", a)
	
   change_value(&a)
   fmt.Println("After calling the function the value of  a =", a)
}

func change_value(ptr *int) {
   *ptr = 20
}

Output

Before calling the function the value of a = 5
After calling the function the value of  a = 20

Conclusion

We compiled and executed the program of passing pointers to a function using three examples. In the first example we passed the pointer in the function pointing to a value which is incremented and printed on the console. In the second example, swap function is used to show the passing of pointers to function as parameters. In the third example, we updated the value of the integer pointer to execute the program.

Updated on: 04-Apr-2023

150 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements