> For the complete documentation index, see [llms.txt](https://blog.yushunchen.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://blog.yushunchen.com/golang-notes/official-tutorial-notes/more-types/functions.md).

# 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
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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, and the optional `goal` query parameter:

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

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
