Click to subscribe to my newsletter
Click to follow me on twitter
Click to view my Github
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 thefunc
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 identifiermyName
- immediately following our function identifier
myName
, you will see we puts string
between parentheses - the
s
is the value we will receive from this function andstring
tells us thats
will be of typestring
- inside of
main
we write our function identifier,myName
- immediately following we put the value
"martin"
of typestring
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 thefunc
keyword - give an identifier of
sayHello
- write a single parameter
s
of typestring
between parentheses - then write a return type of
string
- using the
return
keyword, wereturn
the value ofs
from this function - inside of
func
main
we declare a new variablen
that is equal to the returned value of thesayHello
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
ands2
, both of typestring
- in the next set of parentheses we have our return types:
string
andbool
- on the first line, we declare a variable
a
and assign it to the value of astring
that includes the values ofs1
ands2
- next, we declare a variable
b
and assign it to the valuetrue
of typebool
- after the
return
keyword we write the variablesa
andb
- our return types are
string
andbool
, order matters; therefore, we can not return abool
value and then astring
value
- our return types are
- inside of
func
main
we declarex
andy
as variables that will be assigned the value of each returned value fromisAJedi
- when we print we see that the value of
x
isobi wan kenobi
and the value ofy
istrue
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 thefunc
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 thestring
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, Callbacks, and Recursive Functions. Can't wait, see you then!
Top comments (0)