Go Functions (Part 1) — The anatomy of a regular function
By: Sher Chowdhury
Tags: Go Tutorial, Go, Golang, Golang Tutorial

Functions are used for organising your code into reusable blocks of code, and these code blocks in turn, performs a specific task.

This article is part of the Functions in Go series

Photo by Sven Brandsma on Unsplash

There are different types of functions, but in this post we’re just going to look at a regular function. Functions are declared using the func keyword.

func functionName() {

...

}

Here’s an example:

Line 5: We declared a function called greetings. This is a really simple function that just prints “Hello World” to the standard output.

Line 10: Here we call our greetings function. This ends up executing the greetings function’s code block. We can call our function as many times as we like.

Function Parameters

The greeting() function is a really basic function. However in a lot of cases you may want to feed data into a function and/or get data out of it. In Go, that’s often done using function parameters. There are two types of function parameters:

func functionName(input-parameters) output-parameters {

...

}

Input parameters are used for passing one or more arguments into a function. Whereas output parameters are used for returning values from a function.

Input Parameters

Input parameters are defined inside round brackets:

Line 5: We have specified two input parameters, num1 and num2. You have to specify its data type for each input parameter. In this example they’re both integer based input parameters.

Line 6: Input parameters are used like ordinary variables inside the function. The function automatically declares these variables for use behind the scenes, so don’t need to do explicitly declare them like this:

func add(num1 int, num2 int) {

var num1 int

var num2 int

result := num1 + num2

fmt.Println(result)

}

Tip: If all the input parameters are of the same data type, then we can write it in a more shorthand form:

func add(num1, num2 int) {

result := num1 + num2

fmt.Println(result)

}

Line 11: We pass in two integer arguments when calling the add function. If you fail to provide the right arguments then you’ll get an error message.

Arguments verses Parameters

There’s a common misconception that “parameters” are “arguments” are names for the same thing. In actual fact, there is a subtle difference between them:

parameters: these are your function’s settings, e.g. num1 and num2.

arguments: these are actual values that you pass into a function via the function’s input parameters, e.g. 2 and 6.

Output Parameters

Output parameters are used for getting a function to return some data.

Line 5: We’ve specified one output parameter after the round brackets, by just specifying its data type, int . We don’t need to name our output parameters (although you can, we’ll cover that a bit later).

Line 7: We used the return statement to specify what data we want to send back out via the output parameter. The function ends as soon as return is executed.

Line 11: This time we’re using := to capture the output parameter’s data into a variable called totalsum.

Functions with multiple return values

You can specify more than one output parameters for a function in order to get multiple return values from your function.

Line 5: The (int, string) means that this function has 2 output parameters. The function returns an int value followed by string value. You also have to use round brackets when specifying more than one output parameters.

Lines 10–14: This works out what the remainder is when dividing result by 2, and then applies an if-else statement based on whether the remainder is equal to 0.

Line 16: The return statement now needs return two values, a string, and an integer, in the order that’s dictated by the output parameters defined in line 5. That means return oddOrEven, result

Line 20: Our function is designed to return 2 values, That’s why we’ve specified 2 new variables to capture those values, totalsum, and numberType. Once again the ordering is mirrored. So totalsum is assigned with result, and NumberType gets assigned with oddOrEven.

Functions with “named” return values

Like input parameters, we can name our output parameters.

Line 5: This time we’ve named our output parameters as result and oddOrEven respectively. Our function declares these variables behind the scenes as part of its internal initialisation process. That is, it runs var result int and var oddOrEven string .

Line 7: This time we’ve replaced := with = . since the function already declared result for us behind the scenes.

Line 10 and 12: Similarly we don’t have to do var oddOrEven string beforehand, since that’s already done for us behind the scenes.

Line 15: We no longer need to explicitly specify the return values in our return statement. Instead the function will just return the values of result and oddOrEven.

Ignoring Returned Values

In some cases we might not be interested in what a return value is. In which case we can use underscore, _ , to discard it.

Line 5: This function doesn’t have any input parameters. But it does have 2 named output parameters. We’ve written it using a short-hand form since all the output parameters are of the same data type, but we could have used the longhand form if we wanted to (favourite int, leastLiked int)

Line 11: We’re using _ to ignore and discard the value sent back to us via the function’s second output parameter, leastliked

Summary

A function is a discrete block of code that performs a specific task. A function can:

accept 0 or more inputs (aka arguments) with the help of input parameters run a block of code using those inputs return 0 or more outputs (aka return values) with the help of output parameters

So while the package declaration let’s you organise your code into smaller .go files, functions let’s you organise your code at a lower-level within those .go files, turning them into discrete blocks and also making them reusable.

Further reading