Working with Functions in Go

In Go, the function is a set of statements that performs a specific task and can be reused repeatedly in a program. Functions improve the readability and maintainability of a program.

A function can take input parameters and can also return an output. Input parameters and output are optional to a function.

Every Go program will have at least one function main() function which is the starting point.

Syntax:
func functionName ([inputParameters]) [returnType] {
	//function body
} 

A function in Go is declared using the func keyword followed by the function name. Input parameters can be passed to a function which is optional. A function can return one or more values. In the above syntax, returnType is the data type of the return values. It's not mandatory to return a value, in that case, the returnType is not required. The function body is a set of executable statements that performs a specific task.

The following example defines the greet() function:

Example: Function
func greet() {
	fmt.Println ("Hello World") 
}

In the above example, a function greet() is defined using the func keyword. The opening curly brace { indicates the beginning of the function. It should be placed in the same line as the function name declaration and not in a separate line. The closing curly brace indicates the end of the function.

The above greet() function does not take any input parameter and does not return any value. The function body contains single statement.

The function does not get executed automatically. It has to be explicitly called by using the function name followed by parenthesis e.g. greet().

A function can be called one or more from any other functions.

Example: Function Call
package main
import "fmt"

func greet() {
	fmt.Println ("Hello World") 
}

func main() {
	 greet() // calling the greet function
}
Output:
Hello World

Function with Input Parameters and Return Value

In the following example, a function called Sum is declared which accepts two values as input parameters and returns the total of the two values.

Example: Function with Input Parameter and Return Value
package main
import "fmt"

func Sum (x int, y int) int {
	return x + y
}

func main() {
	result := Sum(5, 10)
	fmt.Println (result) 
}
Output:
15

In the above program, the Sum function takes two integers parameters, x and y. It computes the sum of these two integers and returns the result to the calling function.

Multiple Return Values

A function can return multiple values of different data types. For example the following function returns two integer values.

Example: Multiple Return Values
package main
import "fmt"

func SumAndSubtract (x int, y int) (int, int) {
	sum := x + y
	sub:= x – y

	return sum, sub
}

func main() {
	 
	sum,subtract := SumAndSubtract(10, 5)
	
	fmt.Println(sum, subtract) 
}
Output:
15 5

A function can return multiple values of different data types.

Example: Return Multiple Values of Different Data Types
package main
import "fmt"

func GetDefaults() (int, string) {
	return 0, "unknown"
}

func main() {
	id, name := GetDefaults()
	
	fmt.Println(id, name) 
}
Output:
0 unknown

Naming Conventions for Functions

  • A function name must start with a letter.
  • A function name cannot contain spaces.
  • It can contain only alphanumeric characters and underscore.
  • Function names are case sensitive, e.g. Greet() and greet() are two different functions.