Skip to content

Function Declarations, Arguments, Parameters, and Anonymous Functions in Go

Posted on:May 3, 2020 at 10:40 PM

This is the sixth entry of my weekly series Learning Go. Last week I covered the Struct and Interface types. This week I will be talking about Function Declarations, Arguments, Parameters, and Anonymous Functions.

Overview

The role that functions play in Go, or in any programming language, is the same. They exist to perform computation, data fetching, aggregation, and many other utilities. A few notes on functions before we jump into a few examples:

Function Declaration

There are several ways to create a function. The first one I will cover, is probably the most traditional; however, it is not exclusive to how we declare functions.

Before we jump into an example, let me break down the four pieces of a Function Declaration:

Quick note: a receiver is not required. These are used in receiver functions which we will cover later. Another thing to keep in mind, parameters and return types are optional.

If your function does not expect to get any values when it is called, you can leave the parameters empty.

Likewise, if your function does not return a value, you do not need a return type.

Let’s look at a basic example of a function declaration, first, without a receiver type, parameters, or a return type.

package main

import (
	"fmt"
)

func main() {
	sayHello()
	// hello!
}

func sayHello() {
	fmt.Println("hello!")
}

Arguments and Parameters

Why do we need parentheses immediately following our identifier? This is how you would pass values into your function. These values are called arguments. If we do not have any values to pass, we still have to write a set of parentheses, this lets the compiler know that we want to execute this function and that it has no arguments.

Let’s create a function that uses arguments and parameters.

package main

import (
	"fmt"
)

func main() {
	myName("martin")
	// hello martin
}

func myName(s string) {
	fmt.Println("hello", s)
}

Return Values

We have seen a few very basic ideas of the role that functions can play in your programs; however, I am confident that you will not use functions just to print values. Let’s see an example of a function that returns a value:

package main

import (
	"fmt"
)

func main() {
	n := sayHello("martin")
	fmt.Println(n)
	// hello from martin
}

func sayHello(s string) string {
	return fmt.Sprint("hello from ", s)
}

From a code organization standpoint, there will come a time that you need to assign a variable to the returned value of a function in order to do something else useful with it.

Multiple Return Values

In Go, it is possible to have more than one value returned from a function. Let’s see how that works in an example below:

package main

import (
	"fmt"
)

func main() {
	x, y := isAJedi("obi wan", "kenobi")
	fmt.Println(x, y)
	// obi wan kenobi true
}

func isAJedi(s1, s2 string) (string, bool) {
	a := fmt.Sprint(s1, " ", s2)
	b := true
	return a, b
}

Anonymous Functions

As we have already seen, there are many ways you can create and use a function. An anonymous function is used for when you don’t need a function with an identifier:

package main

import (
	"fmt"
)

func main() {
	func() {
		fmt.Println("I'm anonymous!")
	}()
	// I'm anonymous!
}

In Summary

I hope you have enjoyed learning about Function Declarations, Arguments, Parameters, and Anonymous Functions. There is so much more to learn about functions in Go, and I am excited to share more with you in the coming weeks. Next week we will dive into Function Expressions and Closure. Can’t wait, see you then!

Never miss a post, subscribe to my Substack!