Passing Arguments and Returning Values in Go Programming Language

Go is a statically typed programming language that supports passing arguments and returning values from functions. In this article, we will explore the various ways to pass arguments and receive return values in Go.

Passing Arguments

Go allows passing arguments to functions in three different ways:

  1. Pass by value: By default, Go uses pass by value. In this method, a copy of the actual argument is passed to the function. Any changes made to the argument within the function do not affect the original value. For example:

    func updateValue(num int) {
        num = 100
        fmt.Println("Inside function:", num)
    }
    
    func main() {
        value := 10
        updateValue(value)
        fmt.Println("Outside function:", value)
    }

    Output: Inside function: 100 Outside function: 10

  2. Pass by pointer: To modify the original value, we can pass the address of the variable using pointers. Changes made to the value pointed to by the pointer will reflect in the original variable. For example:

    func updateValueByPointer(num *int) {
        *num = 100
        fmt.Println("Inside function:", *num)
    }
    
    func main() {
        value := 10
        updateValueByPointer(&value)
        fmt.Println("Outside function:", value)
    }

    Output: Inside function: 100 Outside function: 100

  3. Pass by slice: Go also supports passing arguments by slice. A slice is a reference type that contains a pointer to the underlying array. Changes made to the slice within the function will affect the original slice. For example:

    func updateSlice(slice []int) {
        slice[0] = 100
        fmt.Println("Inside function:", slice)
    }
    
    func main() {
        values := []int{10, 20, 30}
        updateSlice(values)
        fmt.Println("Outside function:", values)
    }

    Output: Inside function: [100 20 30] Outside function: [100 20 30]

Returning Values

In Go, functions can return one or more values. We can declare the return type(s) of a function using the syntax funcName() returnType. For example:

func calculateSum(a int, b int) int {
    return a + b
}

func main() {
    result := calculateSum(5, 3)
    fmt.Println("Sum:", result)
}

Output: Sum: 8

We can also return multiple values from a function. To do this, we need to specify the return types as a comma-separated list. For example:

func calculateSumAndDifference(a int, b int) (int, int) {
    return a + b, a - b
}

func main() {
    sum, difference := calculateSumAndDifference(10, 5)
    fmt.Println("Sum:", sum)
    fmt.Println("Difference:", difference)
}

Output: Sum: 15 Difference: 5

We can assign the return values to variables using the assignment operator (:=) or directly access the return values without assigning them.

Conclusion

Passing arguments and returning values are integral parts of function execution in Go. By understanding how to pass arguments and interpret return values, you can effectively manipulate data and perform computations within your programs. Experiment with different passing techniques and explore the flexibility Go provides in handling function arguments and return values.


noob to master © copyleft