Functions in golang

Udit Saurabh
4 min readNov 9, 2022

--

Functions in Golang

Functions are a set or a group of lines of code to represent certain functionality. In Go, we create a function with the func keyword. The most important purpose of using function is the reusability of code. We can use the piece of functionality more than once. We can write a function once reuse it inside the package or export it outside the package based on our requirements. A function has name input parameters with and return type

In The below example, we can see that there is a function defined with the keyword func. After that, there is a function name, the parameter it takes and the return type. Here the name of the function is “Give_Greetings”, the function parameter is the name the of type string and the return type a is string.

func Give_Greetings(name string) string {

return “Hi” + “ “ + name

}

O/P

C:\Users\lenovo\OneDrive\Desktop\Projects\go-projects\go-book>go run .

Hi Raju

The function provides abstraction. Abstraction is a way to hide the inner details from the presentation. This means that the user does not necessarily need to know the inside working of a function code, He only needs to understand the name its parameter and the return type to use it.

There are two ways to pass parameters into a function in Golang.

  1. Pass by Value
  2. Pass by Reference

Pass By Value

Golang follows pass by value method to pass data to a function. In this method, the data is copied and then passed to a particular function. A copy of the data is passed and the original data is often preserved. You can see in the below example that the data in variable res is preserved even after passing it to the Add_One function.

func Add_One(val int) int {

return val + 1

}

func main() {

res := 1

new_res := func_test.Add_One(res)

fmt.Println(“The value of res is preserved after function execution”, res)

fmt.Println(“The new value is returned”, new_res)

}

Output

C:\Users\lenovo\OneDrive\Desktop\Projects\go-projects\go-book>go run .

The value of res is preserved after function execution 1

The new value is returned 2

The call-by-value method provides data encapsulation. This means that data which goes inside a function is a copy that is sent to a function and is different for the rest of the application. It saves the data in the called environment.

The disadvantage of using the call-by-value method

The main disadvantage of using the call-by-value is that if we have large objects then creating a copy of the data will take time.

Pass By Reference

In this method, we pass a pointer to the caller function and change the data in the function. This is a great approach for passing large data structures but for primitive data structures call by value are very efficient. Here in the example below, we are not passing actual data but passing the address of the original data and making the change in the original data rather than in the copy. The advantage is while copying large data we will save the copying time and memory.

func Add_One_By_Refrence(ptr *int) {

*ptr = *ptr + 1

}

func main() {

res := 1

func_test.Add_One_By_Refrence(&res)

fmt.Println(“The value of res is not preserved after function execution”, res)

}

O/P

C:\Users\lenovo\OneDrive\Desktop\Projects\go-projects\go-book>go run .

The value of res is preserved after function execution 2

Passing Array By Value

Array are passed by values when we directly pass them in the function

func Pass_Array_Example(arr [3]int) {

arr[0] = 2000

}

func main() {

res := […]int{10, 20, 30}

func_test.Pass_Array_Example(res)

fmt.Println(res)

}

O/P

C:\Users\lenovo\OneDrive\Desktop\Projects\go-projects\go-book>go run .

[10 20 30]

Pass By Reference Example in Array

Use it when the size of the array is unknown at compile time or we are expecting a variable size array input or it’s very large and we are using it for lookup or special use.

func Pass_By_Ref_Array_Example(arr *[3]int) {

(*arr)[0] = 5000

}

func main() {

res := […]int{10, 20, 30}

func_test.Pass_By_Ref_Array_Example(&res)

fmt.Println(res)

}

O/P

C:\Users\lenovo\OneDrive\Desktop\Projects\go-projects\go-book>go run .

[5000 20 30]

Passing data in functions using slices

We can pass data using slices rather than using the pointer to the array because slices are themselves references to the underlying array. Since the change to slice is changing the original array so we don’t need to pass a reference of the array.

func Pass_Data_With_Slice(arr []int) {

arr[0] = 20000

fmt.Println(“the slice is as follows”, arr)

}

func main() {

res := []int{10, 20, 30}

func_test.Pass_Data_With_Slice(res)

fmt.Println(res)

}

O/P:

C:\Users\lenovo\OneDrive\Desktop\Projects\go-projects\go-book>go run .

the slice is as follows [20000 20 30]

[20000 20 30]

Passing a slice copy a pointer. Always prefer to use slices more as compared to an array as it is more memory efficient.

--

--

Udit Saurabh

full-stack developer, problem solver, technical writer