Welcome to Golang Programs, Get the best new programs in your inbox, daily.

Functions

A function in Go Programming is a reusable piece of code that wrap up a sequence of code statements as a unit, which can be called from within the packages, and also from other packages if the functions are exported to other packages. Because functions are reusable piece of code, you can call this form multiple times. Functions are the basic building blocks of Go programs; all interesting stuff happens in them.

Here is the syntax for writing functions in Go:

func funcname(list of parameters) (list of return types)
{
function body
}

The keyword func is used to declare a function.
funcname is the name of your function.
The parameter list specifies the names and types of the function's parameters, which are the local variables whose values or arguments are supplied by the caller.
The list of return types specifies the types of the values that the function returns.If the function returns one unnamed result or no results at all, parentheses are optional and usually omitted.
The function body contains a collection of statements that define what the function does.

Example of Functions:
package main
import "fmt"
// Funtion name add
func add(a int,b int) int { // int type of function
	var c int
	c = a + b
	return c				// return type	
}
func main(){
	x:= 10
	y:= 20
	z:= add(x,y)
	fmt.Println(z)
}

Functions can be declared in any order you wish. The compiler scans the entire file before execution, so function prototyping is a thing of the past in Go.

Anonymous Functions:

An anonymous function is a function definition without a function name. This is useful when you want to form a function inline without providing a function identifier.

package main
import "fmt"
func cube() func() int {
	var x int
	return func() int{
		x++
		return x*x*x
	}	
}
func main(){
	f := cube()
	fmt.Println(f())
	fmt.Println(f())
	fmt.Println(f())
	fmt.Println(f())
	fmt.Println(f())
}
Output
1
8
27
64
125

The function cube returns another function, of type func() int. A call to cube creates a local variable x and returns an anonymous function that, each time it is called, increments x and returns its cube. A second call to cube would create a second variable x and return a new anonymous function which increments that variable.

Variadic Functions:

A variadic function is one that can be called with varying numbers of arguments. The most familiar examples are fmt.Printf and its variants. Printf requires one fixed argument at the beginning, then accepts any number of subsequent arguments.

package main
import "fmt"
func add(vals ...int) int {
	total := 0
	for _, val := range vals{
		total += val
	}
	return total
}
func main(){
	fmt.Println(add(1,2,3,4)) 
}

In this example, add is allowed to be called with multiple integers. This is known as a variadic parameter. By using an ellipsis (...) before the type name of the last parameter, you can indicate that it takes zero or more of those parameters. In this case, we take zero or more ints.

Closure Functions:

It is possible to create functions inside of functions. Let's move the add function we saw before inside of main:

package main
import "fmt"
func main(){
	cube := func(x int) int{		
		return x*x*x
	}
fmt.Println(cube(5))
}

Recursion Functions:

Recursion is a powerful technique for many problems, and of course it's essential for processing recursive data structures.

package main
import "fmt"
func factorial(x uint) uint {
	if x == 0 {
		return 1
	}
	return x * factorial(x-1)
}
func main(){	
	fmt.Println(factorial(5))
}

Closure and recursion are powerful programming techniques that form the basis of a paradigm known as functional programming. Most people will find functional programming more difficult to understand than an approach based on for loops, if statements, variables, and simple functions.