Functions in Go



In this tutorial, we will learn about functions in Go. We will cover the basics of defining and using functions, including how to return values, use multiple return values, and create recursive functions.


Understanding Functions in Go

A function in Go is a group of statements that perform a specific task. Functions help to divide the code into smaller and modular chunks, making it more organized and manageable.


Defining a Function

Functions in Go are defined using the func keyword, followed by the name of the function, parameters, and the return type.

func functionName(parameters) returnType {
    // function body
}

Function Returning a Value

A function can return a value by specifying the return type in the function signature and using the return statement inside the function.

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

Function with Multiple Return Values

Go functions can return multiple values by specifying multiple return types in the function signature.

func swap(x, y string) (string, string) {
    return y, x
}

Recursive Function

A function can call itself, which is known as recursion. Recursive functions are used to solve problems that can be broken down into smaller, repetitive problems.

func factorial(n int) int {
    if n == 0 {
        return 1
    }
    return n * factorial(n-1)
}


Example 1: Basic Function Definition

We can define and call a basic function in Go.

For example,

  1. Define a function named greet that takes no parameters and returns no value.
  2. Inside the function, print a greeting message to the console.
  3. Call the greet function from the main function.

Go Program

package main
import "fmt"
func greet() {
    fmt.Println("Hello, Go!")
}
func main() {
    greet()
}

Output

Hello, Go!


Example 2: Function Returning a Value

We can define a function in Go that returns a value.

For example,

  1. Define a function named add that takes two integer parameters and returns their sum.
  2. Inside the function, return the sum of the two parameters.
  3. Call the add function from the main function and print the result.

Go Program

package main
import "fmt"
func add(a int, b int) int {
    return a + b
}
func main() {
    result := add(3, 4)
    fmt.Println(result)
}

Output

7


Example 3: Function with Multiple Return Values

We can define a function in Go that returns multiple values.

For example,

  1. Define a function named swap that takes two string parameters and returns them in reversed order.
  2. Inside the function, return the two parameters in reversed order.
  3. Call the swap function from the main function and print the results.

Go Program

package main
import "fmt"
func swap(x, y string) (string, string) {
    return y, x
}
func main() {
    a, b := swap("hello", "world")
    fmt.Println(a, b)
}

Output

world hello


Example 4: Recursive Function

We can define a recursive function in Go to solve problems that can be broken down into smaller, repetitive problems.

For example,

  1. Define a function named factorial that takes an integer parameter and returns the factorial of that number.
  2. Inside the function, use a base case to return 1 if the parameter is 0. Otherwise, return the product of the parameter and a recursive call to factorial with the parameter decremented by 1.
  3. Call the factorial function from the main function and print the result.

Go Program

package main
import "fmt"
func factorial(n int) int {
    if n == 0 {
        return 1
    }
    return n * factorial(n-1)
}
func main() {
    fmt.Println(factorial(5))
}

Output

120