Skip to main content

Functions

Updated Jul 19, 2023 ·

Overview

Functions are reusable blocks of code that run when they are called.

In Go, the main() function is special because Go calls it automatically when the program starts. Other functions run only when your code calls them.

func main() {
fmt.Println("Hello")
}

The fmt.Println() call is also a function call. It uses a function from the fmt package.

Define and Call a Function

Use the func keyword to define a function.

func outputText() {
fmt.Print("Hello")
}

A function definition has these parts:

PartPurpose
funcStarts a function definition.
outputTextNames the function.
()Holds any parameters the function accepts.
{ ... }Holds the function body that should run.
fmt.Print(...)Runs code when the function is called.

Note: Function names should clearly describe what the function does.

To run a function, call it by writing its name followed by parentheses.

func main() {
outputText()
}

func outputText() {
fmt.Print("Hello")
}

In this example, main() calls outputText(). The code inside outputText() runs at that point.

Passing Parameters

Parameters let a function receive input values.

To pass a parameter, pass them inside the parentheses in the function definition, followed by the parameter type.

For example, text with type string:

func outputText(text string) {
fmt.Print(text)
}

The text parameter then becomes available inside the function body.

Call the function outputText and pass an argument:

func main() {
outputText("Investment Amount: ")
}

func outputText(text string) {
fmt.Print(text)
}

The argument "Investment Amount: " is stored in the text parameter while the function runs.

info

The parameter is the name in the function definition. The argument is the value passed when the function is called.

Using Multiple Parameters

A function can accept more than one parameter.

func printUser(name string, age int) {
fmt.Println(name, age)
}

If multiple parameters use the same type, you can write the type once at the end.

func outputTwoTexts(firstText, secondText string) {
fmt.Print(firstText, secondText)
}

Both firstText and secondText are string values.

Returning a Value

A function can return a value to the place where it was called.

See sample code here: Investment Calculator using Functions

Here, the investmentAmount, returnRate, and years parameters are passed info the function. The function calculates the future value and returns it to the caller.

func calculateFutureValue(investmentAmount, returnRate, years float64) float64 {
futureValue := investmentAmount * math.Pow(1+returnRate, years)
return futureValue
}

When the value is returned, it is formatted as a float64 because that is the return type specified in the function definition.

The return keyword sends the value back to the caller.

func calculateFutureValue(...) float64 {
...
return futureValue
}

The returned value can be stored in a variable when the function is called.

futureValue := calculateFutureValue(investmentAmount, returnRate, years)

Returning Multiple Values

Go functions can return more than one value.

In this example, the calculateFutureValues function contains two calculations. It then returns both the futureValue and the adjustedFutureValue.

func calculateFutureValues(investmentAmount, returnRate, years float64) (float64, float64) {
const inflationRate = 0.02

futureValue := investmentAmount * math.Pow(1+returnRate, years)
adjustedFutureValue := futureValue / math.Pow(1+inflationRate, years)

return futureValue, adjustedFutureValue
}

To store the returned values, use multiple variables.

futureValue, adjustedFutureValue := calculateFutureValues(investmentAmount, returnRate, years)

The first returned value goes into the first variable (futureValue)

The second returned value goes into the second variable (adjustedFutureValue)

Named Return Values

Go also allows named return values. This means that the variable names are specified in the function definition, not inside the function body. This creates the variables automatically when the function runs.

func calculateFutureValues(investmentAmount, returnRate, years float64) (futureValue, adjustedFutureValue float64) {
const inflationRate = 0.02

futureValue = investmentAmount * math.Pow(1+returnRate, years)
adjustedFutureValue = futureValue / math.Pow(1+inflationRate, years)

return futureValue, adjustedFutureValue
}

Since variables are created automacally, we don't need to use the := operator. Instead, we can use the = operator to assign values to the variables.

futureValue = investmentAmount * math.Pow(1 + returnRate, years)
adjustedFutureValue = futureValue / math.Pow(1 + inflationRate, years)

You can also use a plain return when return values are named.

func calculateFutureValues(...) (futureValue, adjustedFutureValue float64) {
...
return
}

Note: Explicitly returning the values is often easier to read, especially when a function becomes longer.

Function Scope

Variables and constants created inside a function only exist inside that function.

func main() {
investmentAmount := 1000.0
}

func calculateFutureValue() {
fmt.Println(investmentAmount) // This will not work
}

The investmentAmount variable is scoped to main(), so calculateFutureValue() cannot use it directly.

In the example below, the calculateFutureValue is called inside the main() function so it can use the investmentAmount, returnRate, and years variables.

func main() {
investmentAmount := 1000.0
returnRate := 0.05
years := 10.0

futureValue := calculateFutureValue(investmentAmount, returnRate, years)
fmt.Println(futureValue)
}

func calculateFutureValue(investmentAmount, returnRate, years float64) float64 {
return investmentAmount * math.Pow(1 + returnRate, years)
}

Note that constants and variables can also be declared outside functions.

const inflationRate = 0.02

func main() {
fmt.Println(inflationRate)
}

A package-level value can be used by all functions in the same package.

warning

Keep values inside functions when they only belong to that function.

Use package-level values only when multiple functions need the same value.

Example: Investment Calculator With Functions

In this example, the main() function collects user input and then calls the calculateFutureValues() function to calculate the future value and adjusted future value of an investment.

See sample code here: Investment Calculator using Functions

Run the program:

go run .

Sample input with expected output:

## Input
Enter the investment amount: 1000
Enter the number of years to invest: 100

## Output
Future Value: 131501.26
Adjusted Future Value: 18151.51