Variables & Math
Learn local bindings, type inference, integer arithmetic, and string concatenation.
Core concepts
let, type inference, +, -, *, string concatenation
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.
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.
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
}Complete source
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
}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++!
Run the project
vpp run projects/02-variables/main.vppWhat 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.
Common mistakes to teach
- Trying to reassign an immutable let binding
- Mixing numeric addition with string concatenation
- Expecting print to change the value of an expression
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.
Full program
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
}