Functions in Go Language

Functions contain a set of statements bundled together to achieve a particular outcome. Now, a function may call other functions based on its requirements. But, why do we need functions? There are many reasons to use functions in a program.

  • It helps in the reusability of the code because now we don’t have to rewrite the code wherever particular processing needs to be done again and again. And if any code change is required, then it’d be easier to update the code in a function, rather than changing the code at every place where we wrote that piece of code.
  • It improves the readability of the code.
  • Breaking bigger functions into smaller ones is a good practice while writing code.

Naming conventions for functions In GO

The naming convention for a function in GO is pretty much similar to naming a variable.

  • A name must begin with a letter or an underscore ( _ ) followed by any number of additional letters or numbers.
  • Functions whose name starts with a capital letter are meant to be exported, meaning they will have visibility outside of the current package ( Now, you know why the Println function starts with a capital letter ). If we only meant to use the function within the current package only, then we should start the function name with a lowercase letter.
  • Function names with multiple words should use the camelCase. ( like multiplyTwoNumnbers ).

Declaring functions

Declaring a simple function with no arguments or return types is very easy. Below is the syntax for the same

func func_name() {
  // code inside the function 
}

A declaration starts with the func keyword, followed by the function’s name and a pair of parentheses, and then a block containing the code within the function.

function declaration in golang
Code example
package main

import (
	"fmt"
)

func displayCodekruName() {
	fmt.Println("Hi Codekru")
}

func main() {

	displayCodekruName()
}

Output –

Hi Codekru
Declaring functions with parameters or arguments

We can declare one or more parameters within the parentheses of the function. A parameter is a variable whose scope is within the function’s only and its value is set up when the function is called.

The syntax for writing a function with parameters

fun func_name( param1 param1_type, param2 param2_type ){
  // code inside the function
}

Code example

package main

import (
	"fmt"
)

func multiplyTwoNUmbers(a int, b int) {
	var c int = a * b
	fmt.Println("Result: ", c)
}

func main() {

	multiplyTwoNUmbers(4, 5)
}

Output –

Result:  20
Functions with return value

What we have done in our previous code is just passed the two parameters, multiplied them, and printed the result within that function only. But what if we want to return the result to the caller? This is where we use the function with a return value.

Syntax

func func_name(parameter_name parameter_type) return_value_type{
  // code inside the function 
}

Code example

package main

import (
	"fmt"
)

func multiplyTwoNUmbers(a int, b int) int {
	var c int = a * b
	return c
}

func main() {

	var c int = multiplyTwoNUmbers(4, 5)
	fmt.Println("value returned from function:", c)
}

Output –

value returned from function: 20
Functions with multiple return values

In Go, we can return multiple values from the function unlike in many programming languages where we can return only a single value. To declare multiple return values for a function, we can place the return value types in the second set of parentheses, where the return value types will be separated by the comma

Syntax

func func_name(parameter_name paramter_type) (return_type_value1, return_type_value_2){
   // code inside the function
}

Code example

package main

import (
	"fmt"
)

func multiplyTwoNUmbers(a int, b int) (int, bool) {
	var c int = a * b
	return c, true
}

func main() {

	c, booleanValue := multiplyTwoNUmbers(4, 5)
	fmt.Println("int value from function is:", c)
	fmt.Println("bool value from function is:", booleanValue)

}

Output –

int value from function is: 20
bool value from function is: true

We can even name the return values just like the parameters to help us understand the purpose of that return value. It also helps in increasing the readability of the code.

package main

import (
	"fmt"
)

func multiplyTwoNUmbers(a int, b int) (intValue int, booleanValue bool) {
	var c int = a * b
	return c, true
}

func main() {

	c, booleanValue := multiplyTwoNUmbers(4, 5)
	fmt.Println("int value from function is:", c)
	fmt.Println("bool value from function is:", booleanValue)

}

Output –

int value from function is: 20
bool value from function is: true

Pass by Value and Pass by reference

We can pass an argument to a function using two ways

  • Pass by Value
  • And second is, Pass by Reference
Pass by Value

When we call a function that has declared parameters, then we need to provide the arguments to call that function. The value in each argument is copied to the corresponding parameter variable. This is what we call “pass-by-value“.

Go language is a pass-by-value language where function parameters receive a copy of the arguments when a function is called. So, if you want to change a variable value by passing it into a function, then it won’t work because you will only be changing the copy of the variable and not the original.

package main

import (
	"fmt"
)

func multiplyTwoNUmbers(a int, b int) {
	b = b * a
	fmt.Println("value of b inside the function:", b)
}

func main() {

	var a int = 5
	var b int = 6
	multiplyTwoNUmbers(a, b)
	fmt.Println("value of b outside the function:", b)

}

Output –

value of b inside the function: 30
value of b outside the function: 6

Here we changed the value of “c” to 30 inside the function but after the function got executed and returned back to the caller where we passed “c” as an argument, the value of “c” remain same as it was before.

Pass by Reference

We can pass an argument to the function using its address or we can say as a pointer and this is what we call “pass-by-reference“. This will help us overcoming pass-by-value shortcoming where the value of the variable is lost after a function is executed but we also have to be bit more caredul not to change the variable’s value unintentionally.

Syntax to pass a variable by reference

 // function declaration
fun func_name(pointerName pointer_data_type){
  // code inside the function's body
}
// calling the function
func_name(&variable)

Code example

package main

import (
	"fmt"
)

func multiplyTwoNUmbers(a int, pointerToB *int) {
	*pointerToB = *pointerToB * a
	fmt.Println("value of b inside the function:", *pointerToB)
}

func main() {

	var a int = 5
	var b int = 6
	multiplyTwoNUmbers(a, &b)
	fmt.Println("value of b outside the function:", b)

}

Output –

value of b inside the function: 30
value of b outside the function: 30

So, here we can see that the variable “b” value was retained even after the function’s execution. This is because we directly changed the value present at an address and hence the variable pointing to that address now will see the updated value.

Hope you have liked the article. If you have any doubts or concerns, please feel free to write us in comments or mail us at [email protected]

Liked the article? Share this on

Leave a Comment

Your email address will not be published.