Golang ProgramsGolang Programs

Simple function with return value in Golang

In this example, the add() function takes input of two integer numbers and returns a integer value with a name of total.
package main

import "fmt"

func main() {
	fmt.Println(add(20, 30))
}

func add(x int, y int) int {
	total := 0
	total = x + y
	return total
}
C:\golang\example>go run test1.go
50

C:\golang\example>
The types of input and return value must match with function signature. If we will modify the above program and pass some string value in argument then program will throw an exception "cannot use "test" (type string) as type int in argument to add".