100daysofcode - Day 08
Hello, Hello folks how it’s going on, today marks our DAY
/ 100 . GOLANG is going very smoothly till now 
. I hope you are enjoying my posts, and getting to learn more about this new in demand technology.
In today post, we will talk about functions, their types, and how we can use them in Golang 


What is a function ? 
- In Computer Science , a function is a self contained module of code with an aim to accomplish a specific task
. They usually work by taking data and returning a result. It helps developers write reusable pieces of code, as they can use a defined function in many places.
How to declare a normal function in Golang ?
- In Golang, to declare a new function, we do that by using the
func keyword.
After that we write the function name, the list of parameters and the return type.
- Example:
func ourFirstFunction (parameter1, parameter2 ,parameter3 …) datatype {
// The function logic written here …
}
package main
import "fmt"
// A function that takes 2 integers and return the result as an int
func addition(a int, b int) int {
return a + b
}
// If we have multiple arguments of the same type, we can pass the type
// to the last argument among them, like a,b,c int
func addition2(a, b, c int) int {
return a + b + c
}
// A golang function without any return type
func subtract(num1 int, num2 int) {
result := 0
result = num1 - num2
fmt.Println(result)
}
func main() {
res1 := addition(1, 2)
fmt.Println("1+2 =", res1)
res2 := addition2(1, 2, 3)
fmt.Println("1+2+3 =", res2)
subtract(5, 3)
}
Multiple Return functions in Golang
- Go has built-in support for multiple return values. This feature is used often in idiomatic Go
- If you only want a subset of the returned values, use the blank identifier _.
- Example
package main
import "fmt"
func values() (int, int) {
return 10, 70
}
func main() {
a, b := values()
fmt.Println(a) // Output: 10
fmt.Println(b) // Output: 70
// When we use the blank identifier on the first argument
// only the second one get returned and vice versa
_, c := values()
fmt.Println(c) // Output: 70
}
Variadic Functions in Golang 
-
Coming from its name, a variadic function is simply a one that takes a variable number of args. And each time we call the function we can pass a varying number of arguments of the same type
-
A variadic function can be simply created by adding an ellipsis “…” to the final parameter , as the example shown below
-
Example
package main
import "fmt"
// A function called addition that take a variable number of arguments
func addition(nums ...int) {
// 1st: We are printing the numbers
fmt.Print(nums, " ")
// 2nd: Iterating through the numbers and adding them
total := 0
for _, num := range nums {
total += num
}
// 3rd: printing the total
fmt.Println(total)
}
func main() {
// Calling the function with 2 args
addition(1, 2) // Output: [1 2] 3
// Calling the same function with 3 args
addition(1, 2, 3) // Output: [1 2 3] 6
// And finally here we are passing an array of numbers as an argument to the function
nums := []int{1, 2, 3, 4}
addition(nums...) // Output: [1 2 3 4] 10
}