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
funcmainwe declare a new function using thefunckeyword - 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
mainsimply 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
funckeyword with the identifiermyName - immediately following our function identifier
myName, you will see we puts stringbetween parentheses - the
sis the value we will receive from this function andstringtells us thatswill be of typestring - inside of
mainwe write our function identifier,myName - immediately following we put the value
"martin"of typestringinside of the parentheses, this is the function's argument -
myNameis 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
funcmain, we declare a function using thefunckeyword - give an identifier of
sayHello - write a single parameter
sof typestringbetween parentheses - then write a return type of
string - using the
returnkeyword, wereturnthe value ofsfrom this function - inside of
funcmainwe declare a new variablenthat is equal to the returned value of thesayHellofunction - 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
funckeyword - give an identifier of
isAJedi - between the first set of parentheses, we write two parameters:
s1ands2, both of typestring - in the next set of parentheses we have our return types:
stringandbool - on the first line, we declare a variable
aand assign it to the value of astringthat includes the values ofs1ands2 - next, we declare a variable
band assign it to the valuetrueof typebool - after the
returnkeyword we write the variablesaandb- our return types are
stringandbool, order matters; therefore, we can not return aboolvalue and then astringvalue
- our return types are
- inside of
funcmainwe declarexandyas variables that will be assigned the value of each returned value fromisAJedi - when we print we see that the value of
xisobi wan kenobiand the value ofyistrue
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
funcmainwe use thefunckeyword 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
fmtpackage, we print thestringI'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)