Download our all in one automation tool for various social media websites.
In Golang, functions can take zero or many arguments. Functions are a group of statement that perform a group of tasks. A function can be used for diving a program into multiple groups, each group of function perform a certain task.
Refer to the example given
below that makes use of function that takes no arguments prints a message
hello
to the console.
// program to demonstrate basic use of functions in Golang
package main
import "fmt"
func hello() {
fmt.Println("hello")
}
func main() {
hello()
}
Above program produces following output:
hello
Program given below demonstrates use of functions with parameters.
Functions given below accept two parameter a
and b
, both of them are
integers.
// functions with args
package main
import "fmt"
func add(a int, b int) int {
return a + b
}
func subtract(a int, b int) int {
return a - b
}
func multiply(a int, b int) int {
return a * b
}
func divide(a int, b int) int {
return a / b
}
func main() {
var a=1
var b=2
fmt.Println(add(a,b))
fmt.Println(subtract(a,b))
fmt.Println(multiply(a,b))
fmt.Println(divide(a,b))
}
Above program produces following output:
3
-1
2
0
As demonstrated in above program, above function takes arguments a
and b
both are integers. Above function return value of type integer
.
If two different parameters in a function share a type then they can be grouped together as shown in the example given below
// functions with args
package main
import "fmt"
// notice that parameter a and b both are integers
// however their type is not defined separately instead it is grouped together
func add(a, b int) int {
return a + b
}
func main() {
var a=1
var b=2
fmt.Println(add(a,b))
}
Above program produces following output:
3
In above program, type of parameters a
and b
is grouped together.
Program given below demonstrates function that return multiple values.
// functions with multiple return values
package main
import "fmt"
// function given below returns multiple values
func calculate(a, b int) (int, int, int, int) {
return a + b, a - b, a * b, a / b
}
func main() {
fmt.Println(calculate(1, 2))
}
Above program produces following output:
3 -1 2 0
Program given above demonstrates a function calculate
that returns multiple
values for calculating addition, subtraction, multiplication and division of
argument a
and b
.
In Golang, return values of a program can be named. If a return value is named then it is treated as a value that is already defined at the top of the function.
Also notice that if return values are named then empty return
statements are
used for returning the value of the defined variables.
Functions with named return values are only useful for short functions, in longer functions they can cause confusion and readability issues.
// named return values
package main
import "fmt"
// this function makes use of named return values
func calculate(a, b int) (addition, subtraction, multiplication, division int) {
addition = a + b
subtraction = a - b
multiplication = a * b
division = a / b
return
}
func main() {
fmt.Println(calculate(5, 5))
}
Above program produces following output:
10 0 25 1