Functions In Go Programming Language
Function
A function is a block of code that takes some input(s), does some processing on the input(s) and produces some output(s). In Golang, a function can be called multiple times within the same package, and also from other packages if the functions are exported to other packages.
Declaring Functions
In Golang, we declare a function using the func keyword. A function has a name, a list of comma-separated input parameters along with their types, the result type(s), and a body.
Here is the syntax for writing the functions in Golang
Syntax
func [functionName] (param1 type, param2 type, ...) (returned type1 , returned type2 , .....){
// Executable statements
}
Function with a return value
In the below example program add() function takes input of two integer numbers and returns the sum of two integer numbers with the name of the total.
The types of input and return type must match with the function signature otherwise it will throw an Exception.
You can run this program live here https://onecompiler.com/go/3utcj6m4d
package main
import "fmt"
func main() {
var a , b = 10 , 20
result := add(a,b)
fmt.Println("Sum of", a, "and", b ,"is:" , result)
}
// creating add function
func add (a int , b int) int {
total := 0
total = a + b
return total
}
Output
Sum of 10 and 20 is: 30
Functions with named Return values
We can also assign a name to the return value by defining variables. Following is an example of a simple function called caluculateAverage that takes two input parameters of type float64 and returns the average of the inputs. The result is also of type float64.
You can run this program live here https://onecompiler.com/go/3utckd3wn
package main
import "fmt"
func main() {
//var a , b = 10.25 , 20.36
// or
a := 10.25
b := 20.36
result := caluculateAverage(a,b)
fmt.Println("Average of", a, "and", b ,"is:" , result)
}
// create the function taht calculates average of two input numbers
func caluculateAverage(a float64 , b float64) (result float64) {
result = (a + b) / 2
return result
}
Output
Average of 10.25 and 20.36 is: 15.305
Function with different number of parameters of the same type
If a function has two or more consecutive parameters of the same type, then it suffices to specify the type only once for the last parameter of that type.
Example
func printPersonDetails(firtsName , lastName string, age int){
}
Function with multiple return values
Go functions are capable of returning multiple values. This is something that most programming languages don’t support natively. But Go is different.
Example
In the below program, the function getStockChangePercentage takes the previous price and current price of stock and returns the amount by which the price has changed and the percentage of what.
package main
import "fmt"
func main() {
var currentPrice , previousPrice = 185000.0 , 95000.0
change , percentChange := getStockChangePercentage(currentPrice , previousPrice)
if change < 0 {
fmt.Printf ("The Stock Price decreased by %.2f which is %.2f%% of the prev price\n", change, percentChange)
}else{
fmt.Printf ("The Stock Price increased by %.2f which is %.2f%% of the prev price\n", change, percentChange)
}
}
func getStockChangePercentage(currentPrice , previousPrice float64) (change float64 , percentChange float64){
change = currentPrice - previousPrice
percentChange = (change / previousPrice) / 100
return change , percentChange
}
Output
The Stock Price increased by 90000.00 which is 0.01% of the prev price