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

7 ChatGPT and AI Apps for Free Travel Planning

7 ChatGPT and AI Apps for Free Travel Planning

These free travel planning apps use AI and Machine Learning tools like ChatGPT to prepare your itinerary in seconds.

How to use Gemini from Google Chrome address bar

How to use Gemini from Google Chrome address bar

Google is integrating AI features into the Google Chrome browser, most recently using Gemini from the Google Chrome address bar.

4 Ways to Change User Account Type in Windows

4 Ways to Change User Account Type in Windows

In addition to creating multiple users, Windows 10 also lets you limit what others can do on a shared PC, by providing multiple types of accounts, namely standard user accounts and admin accounts.

10 useful shortcuts to increase productivity on Microsoft Edge

10 useful shortcuts to increase productivity on Microsoft Edge

Keyboard shortcuts are a great productivity tool, especially for those whose jobs involve a lot of typing.

Serious bug forces Google to pause AI feature that creates images with text

Serious bug forces Google to pause AI feature that creates images with text

Google has announced that it is suspending the feature of generating images of people from text of its Gemini AI model due to a serious historical error.

How to Use Copilot in PowerPoint

How to Use Copilot in PowerPoint

Copilot will help you add creative ideas to your PowerPoint content slides, or fix the bad content in your presentation slides.

How to delete Google Gemini conversation

How to delete Google Gemini conversation

Instead of deleting your entire Google Gemini history, you can choose to delete the conversations you want. This article will guide you through deleting your Google Gemini conversations.

How to Downgrade from iOS 16 Beta 1 to iOS 15

How to Downgrade from iOS 16 Beta 1 to iOS 15

In this article, WebTech360 will guide you how to downgrade from iOS 16 Beta to iOS 15.

How to fix #SPILL! error in Microsoft Excel

How to fix #SPILL! error in Microsoft Excel

The #SPILL! error in Excel is quite common and it is quite simple to fix. Here is how to fix the #SPILL! error easily in Excel.

The best apps to monitor children using phones

The best apps to monitor children using phones

Here's a list of the best parental control apps, to help you compare the different options available.

How to pin media controls to Chromebook Shelf

How to pin media controls to Chromebook Shelf

To use this feature, your Chromebook must be running Chrome OS 89 or later.

How to Stop Sharing Safari Web History Across Multiple Devices

How to Stop Sharing Safari Web History Across Multiple Devices

Safari browser shares web history with other Apple devices that share the same iCloud account. Many people concerned about privacy can refer to the article to stop sharing.

How to Resize Images Properly in Photoshop

How to Resize Images Properly in Photoshop

Many people used to find resizing images very annoying until they learned about Photoshop's simple channel-based protection method.

How to avoid the most common Valentines Day scams

How to avoid the most common Valentines Day scams

Valentine's Day is prime time for scammers. However, you can ensure your February 14th goes smoothly by avoiding these scams.

The cost of replacing the screen of the Galaxy S25 series will be cheaper than the Galaxy S24

The cost of replacing the screen of the Galaxy S25 series will be cheaper than the Galaxy S24

Samsung launched the Galaxy S25 Ultra with Corning Gorilla Armor 2 glass, which is said to be more resistant to drops than the Armor glass on the S24 Ultra. But that's not all: replacement parts for repairing the screen are also significantly cheaper than previous generations.