# Functions

### Declaration

A function can take 0 or more arguments. The type comes after the variable name, as mentioned before (same here for functions). The reason for that can be found here:

{% embed url="<https://blog.golang.org/declaration-syntax>" %}

To be short, using the same example in the blog,

`func main(argc int, argv []string) int` is a nice order because you can just read it off from this function declaration: function main takes an int and a slice of strings and returns an int.

```go
package main

import "fmt"

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

func main() {
	fmt.Println(add(42, 13))
}
```

```go
func add(x, y int) int { // shorter version if x, y share a type
	return x + y
}
```

```bash
55
```

### Return values

A function can return any number of results. Return values may be named. If so, they are treated as variables defined at the top of the function.

A `return` statement without arguments returns the named return values. This is called "naked" return.

```go
package main

import "fmt"

func split(sum int) (x, y int) {
	x = sum * 4 / 9   // 20 * 4 / 9
	y = sum - x       // 20 - 8 
	return            // x, y
}

func main() {
	fmt.Println(split(20))
}
```

```bash
8 12
```

{% hint style="info" %}
Naked return statements should only be used in short functions. They hard readability in longer functions.
{% endhint %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://blog.yushunchen.com/golang-notes/official-tutorial-notes/more-types/functions.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
