# Go Functions

The amount of code in the `main` function has grown a lot during the last few chapters. It is time to start refactoring the code into smaller functions. This will make the code easier to read and understand.

## What is a function?

A function is a block of code that takes zero or more arguments, performs specific tasks, may return one or more values, and possibly has side effects (such as printing to the screen or writing to a file).

A function enhances the readability of the code and provides the ability to reuse the code multiple time by calling an existing function.

It is a generalization of the mathematical concept of a function.

## Defining a function

The Go syntax for defining a function is:

``````func name(arg1 type1, arg2 type2, ...) (returntype1, returntype1, ...) {
// body
}``````

But often there will be only one value returned:

``````func name(arg1 type1, arg2 type2, ...) returntype1 {
// body
}``````

The declaration of the function contains:

• func: A reserved keyword in Go language to create functions.
• name: Name of the function
• paramters: Parameters are like variables used in functions. It has a name and data type associated. We pass a value to these parameters when the function is invoked.
• Return Type: Indicates the data type of the value that is returned by the function. It is not mandatory to return a value always, the function can exists and perform tasks without returning a value. In this case, return type is not required.
• Function Body: A set of statements that performs a specific task.

## Simple examples

When computing an approximation of the square root of a number, we used the `math.Abs` function. Let's define our own absolute value as an exercise:

``````func abs(x float64) float64 {
if x < 0 {
return -x
} else {
return x
}
}``````

The keyword `return` tells the function that what follows is the result of the execution.

Similarly, a common function in mathematics is the `sign` function:

``````func sign(x float64) int {
if x < 0 {
return -1
} else if x > 0 {
return 1
} else {
return 0
}
}``````

## How to call a function?

To call a function, we write the name of the function followed by the arguments in parentheses:

``````package main

import "fmt"

func abs(x float64) float64 {
if x < 0 {
return -x
} else {
return x
}
}

func main() {

// call the abs function
result := abs(-5.0)
fmt.Println(result)
}
``````

Output

``5``

Actually, you already know that since you have been calling the `fmt.Println` function a lot!

The above function call can be simplified further inside the `main` function:

``````func main() {
fmt.Println(abs(-1.0))
}``````

Indeed, since the result is never used again, there is no need to store it in a variable.

## Benefits of using Functions

• Reduces the complexity of the code
• Enhances the readability of the code
• Avoids rewriting of the same code and provides re-usability of the code
• Makes testing easy and ease to write unit test cases
• Provides abstraction and extensibility

## Naming Conventions for Go Functions

• A function name can contain only alphanumeric characters and underscores, i.e., `[a-z, A-Z, 0-9 and _ ]`
• It is recommended to use a camel case and recommended to have `MixedCaps` for exported functions. For Eg: the `Print()`, `ToLower()`, `Scan()`, `ToUpper()` methods are example of exported functions where the convention is`MixedCaps`.