How to Define and Call Functions in Go

Define Go functions with the func keyword and parameters, then call them by name with matching arguments.

Define a function using the func keyword followed by the name, parameters, and return types, then call it by writing its name with matching arguments.

func add(a, b int) int {
	return a + b
}

result := add(2, 3)

The func keyword starts the definition, add is the name, (a, b int) declares two integer parameters, and int after the closing parenthesis specifies the return type. The body { return a + b } contains the logic. To call the function, use add(2, 3) and assign the result to a variable.