Functions in Go

In Go, functions are blocks of code that perform specific tasks, which can be reused throughout the program to save memory, improve readability, and save time. Golang functions may or may not return a value to the caller.

Functions in Go

Things to know about functions in Golang

For example:

package main
import "fmt"

// multiply() nhân 2 số nguyên và trả về kết quả
func multiply(a, b int) int {
    return a * b
}

func main() {
    result := multiply(5, 10)
    fmt.Printf("multiplication: %d", result)
}

Result:

multiplication: 50

Function syntax in Golang

func function_name(Parameter-list)(Return_type) {
    // function body...
}

Function declaration

In Go, a function is declared with the func keyword, followed by a name, parameters, and optional return type.

Syntax

func function_name(Parameter-list)(Return_type) {
    // function body...
}

Multiply function example

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

func: Keyword to declare a function.
function_name: Name of the function, for example: multiply.
Parameter-list: a, b int—the parameters along with their types.
Return_type: int specifies the return type.

Call function

To use a function, simply call it by its name with any necessary arguments. Here, multiply(5, 10) calls the function with 5 and 10 as arguments.

For example

result := multiply(5, 10)
fmt.Printf("Result of multiplication: %d", result)

Function arguments

Go supports two ways to pass parameters to a function: Call by value and Call by reference. By default, Go uses call by value, which means that values ​​are copied and changes inside the function do not affect the caller's variables.

Call by value

In call by value, the values ​​of the arguments are copied into the function parameters, so changes in the function do not affect the original variables.

For example:

package main
import "fmt"

func multiply(a, b int) int {
    a = a * 2 // chỉnh sửa bên trong hàm này
    return a * b
}

func main() {
    x := 5
    y := 10
    fmt.Printf("Before: x = %d, y = %d\n", x, y)
    result := multiply(x, y)
    fmt.Printf("multiplication: %d\n", result)
    fmt.Printf("After: x = %d, y = %d\n", x, y)
}

Result:

Before: x = 5, y = 10
multiplication: 100
After: x = 5, y = 10

Call by reference

In call by reference, pointers are used so that changes inside the function are reflected in the caller's variables.

For example:

package main
import "fmt"

func multiply(a, b *int) int {
    *a = *a * 2 // chỉnh sửa giá trị của a ở địa chỉ bộ nhớ của nó
    return *a * *b
}

func main() {
    x := 5
    y := 10
    fmt.Printf("Before: x = %d, y = %d\n", x, y)
    result := multiply(&x, &y)
    fmt.Printf("multiplication: %d\n", result)
    fmt.Printf("After: x = %d, y = %d\n", x, y)
}

Result

Before: x = 5, y = 10
multiplication: 100
After: x = 10, y = 10
Sign up and earn $1000 a day ⋙

Leave a Comment

The Best Diets for Heart Health

The Best Diets for Heart Health

In addition to regular exercise and not smoking, diet is one of the best ways to protect your heart. Here are the best diets for heart health.

How to cure insomnia for pregnant women in the last 3 months

How to cure insomnia for pregnant women in the last 3 months

The third trimester is often the most difficult time to sleep during pregnancy. Here are some ways to treat insomnia in the third trimester.

Scientifically Proven Ways to Automatically Burn Calories

Scientifically Proven Ways to Automatically Burn Calories

There are many ways to lose weight without changing anything in your diet. Here are some scientifically proven automatic weight loss or calorie-burning methods that anyone can use.

All about iOS 26

All about iOS 26

Apple has introduced iOS 26 – a major update with a brand new frosted glass design, smarter experiences, and improvements to familiar apps.

Yoga exercises to treat insomnia

Yoga exercises to treat insomnia

Yoga can provide many health benefits, including better sleep. Because yoga can be relaxing and restorative, its a great way to beat insomnia after a busy day.

What is the flower of the other shore? Meaning and legend of the flower of the other shore

What is the flower of the other shore? Meaning and legend of the flower of the other shore

The flower of the other shore is a unique flower, carrying many unique meanings. So what is the flower of the other shore, is the flower of the other shore real, what is the meaning and legend of the flower of the other shore?

Healthy snacks that help you lose weight

Healthy snacks that help you lose weight

Craving for snacks but afraid of gaining weight? Dont worry, lets explore together many types of weight loss snacks that are high in fiber, low in calories without making you try to starve yourself.

What to do when you have trouble sleeping?

What to do when you have trouble sleeping?

Prioritizing a consistent sleep schedule and evening routine can help improve the quality of your sleep. Heres what you need to know to stop tossing and turning at night.

How to add a printer to Windows 10

How to add a printer to Windows 10

Adding a printer to Windows 10 is simple, although the process for wired devices will be different than for wireless devices.

The most commonly deficient nutrients in the diet

The most commonly deficient nutrients in the diet

Diet is important to our health. Yet most of our meals are lacking in these six important nutrients.

How to get beautiful nails quickly

How to get beautiful nails quickly

You want to have a beautiful, shiny, healthy nail quickly. The simple tips for beautiful nails below will be useful for you.

The best laptops for students in 2025

The best laptops for students in 2025

Students need a specific type of laptop for their studies. It should not only be powerful enough to perform well in their chosen major, but also compact and light enough to carry around all day.

Ways to reduce the risk of birth defects in the fetus

Ways to reduce the risk of birth defects in the fetus

Birth defects are something no one wants. Although they cannot be completely prevented, you can take the following steps to reduce the risk of birth defects in your baby.

How to check RAM and check RAM errors on your computer with the highest accuracy rate

How to check RAM and check RAM errors on your computer with the highest accuracy rate

As you know, RAM is a very important hardware part in a computer, acting as memory to process data and is the factor that determines the speed of a laptop or PC. In the article below, WebTech360 will introduce you to some ways to check for RAM errors using software on Windows.

Top 5 best automatic home coffee makers

Top 5 best automatic home coffee makers

The automatic home coffee maker is a modern and professional product, bringing you and your family delicious cups of coffee with just a few quick steps.