Golang Functions

Learn about the Go programming language functions, types of the functions, call by values, call by references, syntaxes, examples, etc.
Submitted by IncludeHelp, on January 04, 2022

Functions

A function is a block of code that can perform a specific task. It provides the program the functionality to reuse the code multiple times in the program.

Golang programming language also allows its programmer to create functions in order to make their code more readable. The function in Golang can take input values from calling code or i/o devices and return values back to either calling code or output devices after performing a set of instructions written.

Declaring / Defining a function

To use a function in our program, we need to declare the function in our program. The below syntax is used to declare a function in Golang:

func function_name(parameter_list)(return_type){
    // function body 
}

Parts of a function declaration

  • func keyword is the reserved word in go for declaring a function.
  • function_name is the name literal provided to the function which is used while calling it in the code.
  • parameter_list is the list of parameters that are passed to the function. It contains the name as well as the data type of the arguments.
  • return_type is the type of values that the function will return.
  • Function body is the block of code enclosed by braces '{ }', this contains the code that will be executed when the function is called.

Example: Function to take a value as input and return its square.

func calcSquare (a int)( int ){
	sq := a*a
	return sq
}

Calling a function

After declaring the function in our code. We need to call the function to execute the given lines of code. We can call the function multiple times in our code and even no call to a declared function is syntactically correct. 

The syntax for calling a function:

function_name(paratmers)

Example: Simple program to illustrate the working of functions in Golang

// Go program to illustrate the working 
// of a function

package main

import "fmt"

// Declaring the calcSquare() function
func area(a int) int {
	sq := a * a
	return sq
}

func main() {
	// Calling the function
	fmt.Printf("The square of the %d is %d \n", 10, area(10))
	fmt.Printf("The square of the %d is %d", 43, area(43))
}

Output:

The square of the 10 is 100 
The square of the 43 is 1849

Function Arguments

The parameters that are passed to a function can act differently based on the methods by which they are passed. Go language supports two types of parameter passing methods:

  1. Call by Value
  2. Call by Reference

(i) Call by Value

In this method, the function contains a formal copy (formal parameter) of the actual parameters passed to the function. These formal parameters have different memory locations, thus the changes made on these parameters will not be reflected in the actual parameters.

Example: Golang program to illustrate call by value parameter passing

// Golang program to swap two values
// using call by value
// (but values will not be swapped)

package main

import "fmt"

// Declaring the function
func swap(a, b int) {
	temp := a
	a = b
	b = temp
}

func main() {
	x, y := 10, 20
	fmt.Printf("Before swapping: x= %d, y= %d\n", x, y)
	// Calling the function
	swap(x, y)
	fmt.Printf("After swapping: x= %d, y= %d\n", x, y)
}

Output:

Before swapping: x= 10, y= 20
After swapping: x= 10, y= 20

See the output – In the calling of the function, we passed the parameters (x, y) not the references. Thus, whatever changes will be done with the formal parameter (a, b) will not be reflected x, y.

(ii) Call by Reference

In this method, the references of actual parameters are passed to the function. Thus, the change made inside the function definition will be reflected in the actual parameters.

Example: Golang program to illustrate call by reference parameter passing

// Golang program to swap two values
// using call by reference

package main

import "fmt"

// Declaring the function
func swap(a, b *int) {
	temp := *a
	*a = *b
	*b = temp
}

func main() {
	x, y := 10, 20
	fmt.Printf("Before swapping: x= %d, y= %d\n", x, y)
	// Calling the function
	swap(&x, &y)
	fmt.Printf("After swapping: x= %d, y= %d\n", x, y)
}

Output:

Before swapping: x= 10, y= 20
After swapping: x= 20, y= 10

Returning value(s) from a function

We can return values from a function back to the calling function. These values are the resultant value of the operation performed by the function. A function can return no values, single value, or multiple values. Let's see the program for each type of return.

Program to illustrate no return value from a function in Go

package main

import "fmt"

// Function declaration without
// return type
func Msg() {
	fmt.Printf("Hello World!")
}

func main() {
	// Calling the function
	Msg()
}

Output:

Hello World!

Program to illustrate single return value in Go

package main

import "fmt"

// Declaration the function
// with single return value
func calcProd(a, b int) int {
	prod := a * b
	return prod
}

func main() {
	var a int = 5
	var b int = 53

	// Calling the function
	fmt.Printf("The product of the two values is %d", calcProd(a, b))
}

Output:

The product of the two values is 265

Program to illustrate returning of multiple values in Go

package main

import "fmt"

// Declaration of the function
func swapValues(a, b int) (int, int) {
	return b, a
}

func main() {
	var a int = 5
	var b int = 53

	fmt.Printf("Values before Swapping a = %d, b = %d \n", a, b)

	// Calling the function
	x, y := swapValues(a, b)

	fmt.Printf("Values after Swapping a = %d, b = %d \n", x, y)
}

Output:

Values before Swapping a = 5, b = 53 
Values after Swapping a = 53, b = 5 



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.