> For the complete documentation index, see [llms.txt](https://v-community.gitbook.io/v-by-example/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://v-community.gitbook.io/v-by-example/en/examples/section_3/functions.md).

# Functions

A function is a block of organized, reusable code that is used to perform a single, related action. Functions provide better modularity for your application and a high degree of code reusing.

Ideally, you should consider using the [single responsibility principle](https://en.wikipedia.org/wiki/Single_responsibility_principle) (SOLID) which states that every module or function should have responsibility for a single part of the functionality provided by the software to keep your code maintainable.

Like C and Go, functions cannot be overloaded.

```go
fn sum(x, y int) int {
    return x + y
}

println(sum(77, 33))
```

> Note: The type comes after the argument's name.

```go
fn full_name(first_name, last_name string) string {
    return first_name + ' ' + last_name
}

println(full_name("Vitor", "Oliveira"))
```

## Variadic Functions

Functions can also be variadic i.e. accept an infinite number of arguments. They are not arrays and cannot be returned.

```go
fn foo(test ...string) {
    for txt in test {
        println(txt)
    }
}

foo("V", "is", "the", "best", "lang" , "ever")
```

Output

```
V
is
the
best
lang
ever
```

## Multi-Return Functions

Similar to Go, functions in V can also return multiple and with a different type.

```go
fn student(name string, age int) (string, int) {
    return name, age
}

name1, age1 := student("Tom", 15)
println(name1)
println(age1)
```

Output

```
Tom, 15
```

## High Order Functions

Functions in V can also take in another function as a parameter which is usually needed for something like sort, map, filter, etc.

```go
fn square(num int) int {
    return num * num
}

fn run(value int, op fn(int) int) int {
    return op(value)
}

println(run(10, square))
```

Output

```
100
```

## Exercises

1. Write a V program to find the square of any number using the function.
2. Write a V program to check a given number is even or odd using the function.
3. Write a V program to convert decimal number to binary number using the function.
4. Write a V program to check whether a number is a prime number or not using the function.
5. Write a V program to get the largest element of an array using the function.


---

# 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://v-community.gitbook.io/v-by-example/en/examples/section_3/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.
