# Variables

In V variables can be declared and initialized with the `:=` operator. Variables can only be declared this way in V, this means all variables have an initial value. The type of a variable is inferred from the value on the right hand side. By default variables in V are immutable.

```go
age := 23               // int
name := 'Alice'         // string
is_adult := age > 21    // bool

println(age_str)        // 23
println(name)           // Alice
println(is_adult)       // true
```

> Note: Variables can only be defined in a function. There are no global variables and no global state in V.

To change the value of a variable, it needs to be mutable. This can be done using the `mut` keyword when declaring the variable. To assign a new value to a variable use `=`.

```go
mut age := 20       // declare the mutable variable age and assign it to the value 20.
println(age)        // 20
age = 21            // assign a new value to age
println(age)        // 21
```

Leaving out the `mut` keyword here would result in an error because the value of an immutable variable cannot be changed.

```go
fn main() {
    age = 20
    println(age)
}
```

The code above would result in an error during compilation because the variable `age` is not declared,

```go
fn main() {
    mut age := 20       // we declare the mutable variable age and assign it to the value 20.
    println(age)        // 20
    age := 21           // ERROR
}
```

here `age := 21` will result in another error while compiling because the variable `age` is already defined in the scope. It's very simple to remember, just declare value with `:=` and assign value with `=`.

Like Go, You can also use `_` for ignoring values when it is not needed. Usually used in multi return functions.

```go
_ := "I don't need this value"
println(_) // ERROR: Cannot use `_` as value
```

## Naming Rules

The following are the rules which should be kept in mind while naming variables.

* Name should not contain Uppercase letters like `AlphaTest`
* Use underscores as separators like `hello_world`
* Name should be descriptive as possible
* Name should not contain `__`
* Name should not contain any space
* If the name is longer than 11 then it must use `_` as separator

These rules are from [`Snake_Case`](https://en.wikipedia.org/wiki/Snake_case). V uses Snake Case and prefers it because it is more easy to read, write and understand.

### Valid Names

```go
boby
john_dads
myfamily_number
```

### Invalid Names

```go
IamNotValid
new Make
```


---

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