Functions
Welcome to tutorial no. 6 in our Golang tutorial series.
What is a function?
A function is a block of code that performs a specific task. A function takes an input, performs some operations on the input and generates outputs. For example, a function can take the radius as input and calculate the area and circumference as output.
Function declaration
The following is the syntax for declaring a function in Go
1func functionname(parametername datatype) returntype {
2 //function body
3}
The function declaration starts with the func
keyword followed by the functionname
. The parameters are specified between (
and )
followed by the returntype
of the function. The syntax for specifying a parameter is, parameter name followed by the type. Any number of parameters can be specified like (parameter1 datatype, parameter2 datatype)
. Then there is a block of code between {
and }
which is the body of the function.
The parameters and return type are optional in a function. Hence the following is also a valid function declaration.
1func functionname() {
2}
Sample Function
Let’s write a function that takes the price of a single product and the quantity as input parameters and returns the total price by multiplying these two values.
1func calculateBill(price int, quantity int) int {
2 var totalPrice = price * quantity
3 return totalPrice
4}
The above function has two input parameters price
and quantity
of type int
and it returns the totalPrice
which is the product of price
and quantity
. The return value is also of type int
.
If consecutive parameters are of the same type, we can avoid writing the type each time and it is enough to be written once at the end.ie price int, quantity int
can be written as price, quantity int
. The above function can hence be rewritten as,
1func calculateBill(price, quantity int) int {
2 var totalPrice = price * quantity
3 return totalPrice
4}
Now that we have a function ready, let’s call it from somewhere in the code. The syntax for calling a function is functionname(parameters)
. The above function can be called using the code.
calculateBill(10, 5)
Here is the complete program which uses the above function and prints the total price.
1package main
2
3import (
4 "fmt"
5)
6
7func calculateBill(price, quantity int) int {
8 var totalPrice = price * quantity
9 return totalPrice
10}
11
12func main() {
13 price, quantity := 90, 6
14 totalPrice := calculateBill(price, quantity)
15 fmt.Println("Total price is", totalPrice)
16}
The above program will print
Total price is 540
Multiple return values
It is possible to return multiple values from a function. Let’s write a function rectProps
which takes the length
and width
of a rectangle and returns both the area
and perimeter
of the rectangle. The area of the rectangle is the product of length and width and the perimeter is twice the sum of the length and width.
1package main
2
3import (
4 "fmt"
5)
6
7func rectProps(length, width float64)(float64, float64) {
8 var area = length * width
9 var perimeter = (length + width) * 2
10 return area, perimeter
11}
12
13func main() {
14 area, perimeter := rectProps(10.8, 5.6)
15 fmt.Printf("Area %f Perimeter %f", area, perimeter)
16}
If a function returns multiple return values then they must be specified between (
and )
. func rectProps(length, width float64)(float64, float64)
has two float64 parameters length
and width
and also returns two float64
values. The above program prints
Area 60.480000 Perimeter 32.800000
Named return values
It is possible to return named values from a function. If a return value is named, it can be considered as being declared as a variable in the first line of the function.
The above rectProps can be rewritten using named return values as
1func rectProps(length, width float64)(area, perimeter float64) {
2 area = length * width
3 perimeter = (length + width) * 2
4 return //no explicit return value
5}
area and perimeter are the named return values in the above function. Note that the return statement in the function does not explicitly return any value. Since area
and perimeter
are specified in the function declaration as return values, they are automatically returned from the function when a return statement is encountered.
Blank Identifier
_ is known as the blank identifier in Go. It can be used in place of any value of any type. Let’s see what’s the use of this blank identifier.
The rectProps
function returns the area and perimeter of the rectangle. What if we only need the area
and want to discard the perimeter
. This is where _
is of use.
The program below uses only the area
returned from the rectProps
function.
1package main
2
3import (
4 "fmt"
5)
6
7func rectProps(length, width float64) (float64, float64) {
8 var area = length * width
9 var perimeter = (length + width) * 2
10 return area, perimeter
11}
12func main() {
13 area, _ := rectProps(10.8, 5.6) // perimeter is discarded
14 fmt.Printf("Area %f ", area)
15}
In line no. 13 we use only the area
and the _
identifier is used to discard the perimeter
.
This brings us to the end of this tutorial. I hope you liked it. Please leave your feedback and comments. Please consider sharing this tutorial on twitter and LinkedIn. Have a good day.
Next tutorial - Packages