Variables & Math

Learn local bindings, type inference, integer arithmetic, and string concatenation.

1

Core concepts

let, type inference, +, -, *, string concatenation

2

How the learner should approach this project

Do not paste the complete program immediately. Create the file, type the first step, run vpp check, and then run the program when a complete entry point exists. Read the explanation before looking at the snippet. After each step, predict what the new code should do. This turns the page into a lesson rather than a code dump.

3

Step 1: Bind values and evaluate expressions

let creates a local binding. V++ looks at the value on the right side and infers the type, so x becomes an int because 10 is an integer literal.

Arithmetic is evaluated before print receives the result. The string binding demonstrates that inference also works for strings. When both sides of + are strings, V++ joins them instead of performing numeric addition.

After typing the snippet, identify the new names introduced by the step. Ask what each name represents, what type the compiler should infer or check, and what value should exist after the code runs.

main.vpp
fn main() -> int {
    let x = 10
    let y = 3
    print(x + y)
    print(x - y)
    print(x * y)
    let name = "v++"
    print("Hello, " + name + "!")
    return 0
}
4

Complete source

main.vpp
fn main() -> int {
    let x = 10
    let y = 3
    print(x + y)
    print(x - y)
    print(x * y)
    let name = "v++"
    print("Hello, " + name + "!")
    return 0
}
5

Expected behavior

The complete program should produce the following output when run with the command shown below. Exact formatting should follow the current V++ runtime.

13
7
30
Hello, v++!
6

Run the project

terminal
vpp run projects/02-variables/main.vpp
7

What the learner should understand after this project

A binding gives a value a name. Type inference removes repetitive annotations without removing static type checking. Expressions can be combined before their result is printed.

8

Common mistakes to teach

  1. Trying to reassign an immutable let binding
  2. Mixing numeric addition with string concatenation
  3. Expecting print to change the value of an expression
9

Practice extension

Change x and y and predict every numeric result before running. Then concatenate two separately defined strings.

A strong learner should be able to explain the program without looking at the code, rebuild the core idea from memory, and make the practice change without copying a solution.

10

Full program

main.vpp
fn main() -> int {
    let x = 10
    let y = 3
    print(x + y)
    print(x - y)
    print(x * y)
    let name = "v++"
    print("Hello, " + name + "!")
    return 0
}
vpp
$ ready. Click Test program.