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

Devin, the AI ​​tool that can automatically write code

Devin, the AI ​​tool that can automatically write code

Engineers at startup Cognition Labs have just launched Devin, an AI tool that can automatically write code and take on the work of software engineers on recruiting sites like Upwork without human intervention.

Tien Linh DTCL: How to play, squad, build Tien Linh TFT season 12

Tien Linh DTCL: How to play, squad, build Tien Linh TFT season 12

DTCL Fairy Spirits gain more health and create unique different equipment to rank Fairy Spirit generals in the team.

Latest Last Pirates Code 04/25/2025 and how to enter the code

Latest Last Pirates Code 04/25/2025 and how to enter the code

If you need Last Pirates codes, check out the list of Last Pirates codes below. They will have time and entry limits, so you should regularly update the latest Last Pirates codes.

Garen DTCL season 9 equipment, build Garen Dung Si

Garen DTCL season 9 equipment, build Garen Dung Si

Garen DTCL season 9 has just appeared but is already in the hot meta even though it only costs 3 gold next to Katarina DTCL season 9.

How to Compare Antivirus Software Before Downloading

How to Compare Antivirus Software Before Downloading

Choosing the right antivirus software is important, but figuring out which antivirus to use is not easy. There are many options, each claiming to be the best.

How to clean your phone cleanly and safely to avoid Covid 19

How to clean your phone cleanly and safely to avoid Covid 19

To clean your phone against Corona properly and easily, you can follow the instructions in this article.

Meet Opera Air: Operas New Browser

Meet Opera Air: Operas New Browser

Many people are skeptical when a web browser claims to help relax the mind. But Opera's new Air browser is surprisingly useful, with handy tools for focusing, meditating, exercising, and more.

Listen to the creepy sound of a black hole 250 million light years from Earth, announced by NASA

Listen to the creepy sound of a black hole 250 million light years from Earth, announced by NASA

Sound waves cannot travel through a vacuum, so there can be no sound in space. However, science can help us hear the universe in many ways.

How to measure body temperature accurately

How to measure body temperature accurately

To measure body temperature we will use a thermometer. So what types of thermometers are there and what is the correct way to measure temperature?

6 Easiest Ways to Add Music to Videos on Your Computer

6 Easiest Ways to Add Music to Videos on Your Computer

Windows 10 has built-in video and image editor right on your computer with simple customizations so you can process videos without needing video editing software.

Instructions for creating mind maps in Word

Instructions for creating mind maps in Word

Mind maps are extremely useful when we need to remember information because the content is designed in the most visual style. Below are instructions for creating a mind profile in Word.

9 Apps to Add to Control Center iOS 18

9 Apps to Add to Control Center iOS 18

With iOS 18, Apple has made it even easier to customize Control Center and even your lock screen with support for widgets from third-party apps.

5 unique features of the Pixel 9 Pro Fold

5 unique features of the Pixel 9 Pro Fold

Google launched the Pixel 9 Pro Fold at its Made by Google event in August 2024. While it has a few improvements over its predecessor to compete with the Galaxy Z Fold 6 and OnePlus Open, these are the features that many people are most excited to try.

All the latest codes for Otherworld: Light and Magic

All the latest codes for Otherworld: Light and Magic

Different World: Light and Magic gives players a series of attractive rewards including Star Diamonds, Sparkling Stardust, EXP, Soul Stones, and Summon Cards in the giftcode.

Latest Ninja Legend Code 4/2025

Latest Ninja Legend Code 4/2025

Get the Ninja Legend giftcode now to receive attractive rewards.