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,
- Define a function named
greetthat takes no parameters and returns no value. - Inside the function, print a greeting message to the console.
- Call the
greetfunction from themainfunction.
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,
- Define a function named
addthat takes two integer parameters and returns their sum. - Inside the function, return the sum of the two parameters.
- Call the
addfunction from themainfunction 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,
- Define a function named
swapthat takes two string parameters and returns them in reversed order. - Inside the function, return the two parameters in reversed order.
- Call the
swapfunction from themainfunction 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,
- Define a function named
factorialthat takes an integer parameter and returns the factorial of that number. - 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
factorialwith the parameter decremented by 1. - Call the
factorialfunction from themainfunction 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