Swiftorial Logo
Home
Swift Lessons
AI Tools
Learn More
Career
Resources

Function Parameters and Return Values in Go Programming

Introduction

In Go, functions are first-class citizens. They can take zero or more parameters and return zero or more values. Understanding how to work with function parameters and return values is crucial for writing efficient and effective Go programs.

Function Parameters

Function parameters allow you to pass data into functions. In Go, you define function parameters within the parentheses following the function name. Each parameter is defined with a name followed by its type.

Example:

package main

import "fmt"

func greet(name string) {
    fmt.Println("Hello,", name)
}

func main() {
    greet("Alice")
}

Output:

Hello, Alice

Multiple Parameters

You can define multiple parameters in a function by separating them with commas.

Example:

package main

import "fmt"

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

func main() {
    sum := add(3, 4)
    fmt.Println("Sum:", sum)
}

Output:

Sum: 7

Return Values

Functions in Go can return values. The return type is specified after the parameter list. You can return a value using the return statement.

Example:

package main

import "fmt"

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

func main() {
    product := multiply(3, 4)
    fmt.Println("Product:", product)
}

Output:

Product: 12

Multiple Return Values

Go functions can return multiple values. This is useful for returning error values along with actual data.

Example:

package main

import "fmt"

func divide(a int, b int) (int, error) {
    if b == 0 {
        return 0, fmt.Errorf("division by zero")
    }
    return a / b, nil
}

func main() {
    result, err := divide(10, 2)
    if err != nil {
        fmt.Println("Error:", err)
    } else {
        fmt.Println("Result:", result)
    }
}

Output:

Result: 5

Named Return Values

You can name the return values of a function. This can make the code more readable and allow the use of the return statement without arguments.

Example:

package main

import "fmt"

func swap(a, b int) (x int, y int) {
    x = b
    y = a
    return
}

func main() {
    x, y := swap(1, 2)
    fmt.Println("Swapped values:", x, y)
}

Output:

Swapped values: 2 1

Variadic Functions

Variadic functions can accept a variable number of arguments. The parameter is defined with an ellipsis before the type.

Example:

package main

import "fmt"

func sum(numbers ...int) int {
    total := 0
    for _, number := range numbers {
        total += number
    }
    return total
}

func main() {
    total := sum(1, 2, 3, 4, 5)
    fmt.Println("Total:", total)
}

Output:

Total: 15

Conclusion

Understanding function parameters and return values is fundamental to writing effective Go programs. Whether you're passing simple data types, handling errors with multiple return values, or working with variadic functions, mastering these concepts will greatly enhance your ability to write robust and maintainable code.