A new product!

Social Media Toolkit

Download our all in one automation tool for various social media websites.

Gopher

Functions in Golang

Published on 10 May 2020
Last Updated on 10 May 2020

In Golang, functions can take zero or many arguments. Functions are a group of statement that perform a group of tasks. A function can be used for diving a program into multiple groups, each group of function perform a certain task.

Program to demonstrate functions with no parameters

Refer to the example given below that makes use of function that takes no arguments prints a message hello to the console.

// program to demonstrate basic use of functions in Golang
package main

import "fmt"

func hello()  {
	fmt.Println("hello")
}

func main()  {
	hello()
}

Program output

Above program produces following output:

hello

Program to demonstrate function with parameters

Program given below demonstrates use of functions with parameters.

Functions given below accept two parameter a and b, both of them are integers.

// functions with args
package main

import "fmt"

func add(a int, b int) int {
	return a + b
}

func subtract(a int, b int) int {
	return a - b
}

func multiply(a int, b int) int {
	return a * b
}

func divide(a int, b int) int {
	return a / b
}

func main() {
	var a=1
	var b=2
	fmt.Println(add(a,b))
	fmt.Println(subtract(a,b))
	fmt.Println(multiply(a,b))
	fmt.Println(divide(a,b))
}

Program output

Above program produces following output:

3
-1
2
0

Program Description

As demonstrated in above program, above function takes arguments a and b both are integers. Above function return value of type integer.

Program to demonstrate functions with parameter types grouped together

If two different parameters in a function share a type then they can be grouped together as shown in the example given below

// functions with args
package main

import "fmt"

// notice that parameter a and b both are integers
// however their type is not defined separately instead it is grouped together
func add(a, b int) int {
	return a + b
}

func main() {
	var a=1
	var b=2
	fmt.Println(add(a,b))
}

Program output

Above program produces following output:

3

Program Description

In above program, type of parameters a and b is grouped together.

Program to demonstrate function that returns multiple values

Program given below demonstrates function that return multiple values.

// functions with multiple return values
package main

import "fmt"

// function given below returns multiple values
func calculate(a, b int) (int, int, int, int) {
	return a + b, a - b, a * b, a / b
}

func main() {
	fmt.Println(calculate(1, 2))
}

Program output

Above program produces following output:

3 -1 2 0

Program Description

Program given above demonstrates a function calculate that returns multiple values for calculating addition, subtraction, multiplication and division of argument a and b.

Program to demonstrate functions with named return values

In Golang, return values of a program can be named. If a return value is named then it is treated as a value that is already defined at the top of the function.

Also notice that if return values are named then empty return statements are used for returning the value of the defined variables.

Functions with named return values are only useful for short functions, in longer functions they can cause confusion and readability issues.

// named return values
package main

import "fmt"

// this function makes use of named return values
func calculate(a, b int) (addition, subtraction, multiplication, division int) {
	addition = a + b
	subtraction = a - b
	multiplication = a * b
	division = a / b
	return
}

func main() {
	fmt.Println(calculate(5, 5))
}

Program output

Above program produces following output:

10 0 25 1