Function arguments in Golang

In Golang , a function is a group of statements used to perform tasks, with an optional return value . Go supports two main ways to pass arguments: Pass by value and Pass by reference. Go uses pass by value by default.

Function arguments in Golang

Basic terms in passing parameters to functions:

  • Actual parameters: The arguments passed to the function.
  • Formal parameters: The parameters received by the function.

For example

package main
import "fmt"

// Cố gắng sửa đổi giá trị của num
func modify(num int) {
    num = 50
}

func main() {
    num := 20
    fmt.Printf("Before, num = %d\n", num)
    modify(num)
    fmt.Printf("After, num = %d\n", num)
}

In this example, numremains unchanged after the call modifybecause it is passed by value.

Syntax

func functionName(param Type) {
    // function body                                        # Gọi theo giá trị
}

func functionName(param *Type) {
    // function body                                       # Gọi theo tham chiếu
}

Call by value

In call-by-value, a copy of the actual parameter value is passed. Changes made in the function do not affect the original variable.

Syntax

func functionName(param Type) {
    // function body
}

For example:

package main
import "fmt"

// Chỉnh sửa giá trị của num
func modify(num int) {
    num = 50
}

func main() {
    num := 20
    fmt.Printf("Before, num = %d\n", num)
    modify(num)
    fmt.Printf("After, num = %d\n", num)
}

Result:

Before, num = 20
After, num = 20

The value remains the same, as changes inside modifydo not affect numthe outside main.

Function arguments in Golang

Call by reference

In call-by-reference, a pointer to the actual parameter is passed, so any changes inside the function are reflected on the original variable.

Syntax

func functionName(param *Type) {
    // function body
}

For example:

package main
import "fmt"

// Chỉnh sửa giá trị của num qua tham chiếu
func modify(num *int) {
    *num = 50
}

func main() {
    num := 20
    fmt.Printf("Before, num = %d\n", num)
    modify(&num)
    fmt.Printf("After, num = %d\n", num)
}

Result

Before, num = 20
After, num = 50

Since numit is passed by reference, the command modifywill change its value, which is reflected in the command main.

golang,function arguments,function arguments in golang,how to use golang function arguments,guide to use golang function arguments,go,go programming

Leave a Comment

What Young Riders Should Know About Moving Their Motorcycles Across Cities

What Young Riders Should Know About Moving Their Motorcycles Across Cities

Long-distance travel can involve heavy traffic, changing weather conditions, and rider fatigue. If you are also dealing with the responsibilities of moving home, such as packing belongings or coordinating accommodation, a long ride may add unnecessary pressure to an already busy schedule.

Solving Microsoft Teams Shortcut Error Not Opening

Solving Microsoft Teams Shortcut Error Not Opening

Tired of Microsoft Teams shortcut error preventing you from opening the app? Follow our expert, step-by-step guide with the latest fixes for instant resolution. Works on Windows, Mac & web – no tech skills needed!

Solving Microsoft Teams Task Management Sync Error

Solving Microsoft Teams Task Management Sync Error

Tired of Microsoft Teams Task Management Sync Error halting your workflow? Follow our proven, step-by-step fixes to resolve sync issues fast and restore seamless task collaboration. No tech expertise needed!

Troubleshooting Microsoft Teams Wiki Error Formatting

Troubleshooting Microsoft Teams Wiki Error Formatting

Struggling with Microsoft Teams Wiki Error Formatting? This step-by-step guide reveals proven fixes for common wiki tab issues, ensuring smooth editing and collaboration in Teams. Get back to productive wikis fast!

How to Fix Microsoft Teams Installation Error for Linux

How to Fix Microsoft Teams Installation Error for Linux

Struggling with Microsoft Teams installation error on Linux? Discover step-by-step fixes for Ubuntu, Fedora & more. Resolve dependency issues, crashes, and errors quickly with our ultimate guide. Get Teams running smoothly today!

Solving Microsoft Teams Error Page Not Loading

Solving Microsoft Teams Error Page Not Loading

Struggling with Microsoft Teams "Error Page" not loading? Get step-by-step fixes for desktop, web, and mobile. Solve Microsoft Teams Error Page issues quickly and resume seamless teamwork today.

Solving Microsoft Teams Error Screenshot Issues

Solving Microsoft Teams Error Screenshot Issues

Tired of Microsoft Teams "Error Screenshot" blocking your workflow? Get proven, step-by-step solutions to resolve screenshot errors in Teams instantly and boost productivity. No tech skills needed!

How to Fix Microsoft Teams Error U User

How to Fix Microsoft Teams Error U User

Tired of Microsoft Teams "Error U" User blocking your chats? Get proven, step-by-step fixes to clear cache, reset, and restore seamless collaboration instantly.

Where are Microsoft Teams Registry Keys Located on Windows 11?

Where are Microsoft Teams Registry Keys Located on Windows 11?

Unlock the precise locations of Microsoft Teams registry keys on Windows 11. Step-by-step guide to find, access, and safely tweak them for optimal performance and troubleshooting. Essential for IT pros and Teams enthusiasts.

How to Fix Microsoft Teams Training Error Video Lag

How to Fix Microsoft Teams Training Error Video Lag

Tired of Microsoft Teams "Training Error" Video Lag ruining your meetings? Follow our step-by-step guide with the latest fixes for smooth video calls—no more frustration!