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

May 03, 2020

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:

  • keep functions small and modular
  • parameters are the values the function expects
  • arguments are the values that are passed when a function is executed
  • everything in Go is passed by value

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:

  • receiver - specifies the type of parameters that the function receives
  • identifier - the name of the function
  • parameters - the values that the function expects when it is invoked (executed)
  • return types - the type of value that the function will return

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!")
}
  • below the func main we declare a new function using the func keyword
  • next, we give this function the identifier sayHello
  • in between the parentheses () is where your parameters go. We do not have any in this example; however, you still need to have them
  • we execute this function inside of main simply by writing the function identifier with a set of parentheses ()

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)
}
  • much like the first example, we create a new function using the func keyword with the identifier myName
  • immediately following our function identifier myName, you will see we put s string between parentheses
  • the s is the value we will receive from this function and string tells us that s will be of type string
  • inside of mainwe write our function identifier, myName
  • immediately following we put the value "martin" of type string inside of the parentheses, this is the function’s argument
  • myName is then executed and it prints "hello martin"

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.

  • below func main, we declare a function using the func keyword
  • give an identifier of sayHello
  • write a single parameter s of type string between parentheses
  • then write a return type of string
  • using the return keyword, we return the value of s from this function
  • inside of func main we declare a new variable n that is equal to the returned value of the sayHello function
  • after the function executes, we print the value of n

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
}
  • we declare a new function using the func keyword
  • give an identifier of isAJedi
  • between the first set of parentheses, we write two parameters: s1 and s2, both of type string
  • in the next set of parentheses we have our return types: string and bool
  • on the first line, we declare a variable a and assign it to the value of a string that includes the values of s1 and s2
  • next, we declare a variable b and assign it to the value true of type bool
  • after the return keyword we write the variables a and b

    • our return types are string and bool, order matters; therefore, we can not return a bool value and then a string value
  • inside of func main we declare x and y as variables that will be assigned the value of each returned value from isAJedi
  • when we print we see that the value of x is obi wan kenobi and the value of y is true

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!
}
  • inside func main we use the func keyword with
  • since we do not have any parameters, we write empty parentheses ()
  • to let the compiler know to look inside this function, the function body we write an open bracket {
  • inside the function body, using the fmt package, we print the string I'm anonymous!
  • to signify our function body is closed we write a closing bracket } on the next line
  • following the closing bracket } you will notice we have a set of empty parentheses (), as mentioned previously, this is how we tell the compiler to execute this function
  • since we have no arguments these parentheses are empty ()

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!