Arrays

Create arrays, iterate over them, index elements, and pass arrays to typed functions.

1

Core concepts

array[int], indexing, for, let mut, function parameters

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: Sum an array

An array type describes the element type inside square brackets. The sum function accepts array[int], so the compiler knows every element is an integer.

total must be mutable because each loop iteration updates it. The for loop gives n one element at a time.

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 sum(nums: array[int]) -> int {
    let mut total = 0
    for n in nums {
        total = total + n
    }
    return total
}
4

Step 2: Find the largest value

Indexing uses square brackets. nums[0] reads the first element and gives best its inferred integer type.

Each element is compared with best. When a larger value is found, best is replaced. This demonstrates how arrays and mutable local state work together.

Before moving forward, explain how this step connects to the previous one. In particular, identify which values cross a function boundary, which values change, and which values are guaranteed by the type system.

main.vpp
fn max_of(nums: array[int]) -> int {
    let mut best = nums[0]
    for n in nums {
        if n > best {
            best = n
        }
    }
    return best
}
5

Step 3: Call the array functions

The array literal gives nums the type array[int]. The same array can be passed to both functions because their signatures expect the same element type.

Before moving forward, explain how this step connects to the previous one. In particular, identify which values cross a function boundary, which values change, and which values are guaranteed by the type system.

main.vpp
fn main() -> int {
    let nums = [3, 7, 2, 9, 4]
    print(sum(nums))
    print(max_of(nums))
    return 0
}
6

Complete source

main.vpp
fn sum(nums: array[int]) -> int {
    let mut total = 0
    for n in nums {
        total = total + n
    }
    return total
}
fn max_of(nums: array[int]) -> int {
    let mut best = nums[0]
    for n in nums {
        if n > best {
            best = n
        }
    }
    return best
}
fn main() -> int {
    let nums = [3, 7, 2, 9, 4]
    print(sum(nums))
    print(max_of(nums))
    return 0
}
7

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.

25
9
8

Run the project

terminal
vpp run projects/05-arrays/main.vpp
9

What the learner should understand after this project

Arrays provide ordered collections of values with one element type. Iteration and indexing are two different ways to access that collection.

10

Common mistakes to teach

  1. Indexing beyond the available elements
  2. Passing an array with the wrong element type
  3. Trying to update a non mutable accumulator
11

Practice extension

Write a function that returns the first array element and another that counts how many values are greater than 5.

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.

12

Full program

main.vpp
fn sum(nums: array[int]) -> int {
    let mut total = 0
    for n in nums {
        total = total + n
    }
    return total
}
fn max_of(nums: array[int]) -> int {
    let mut best = nums[0]
    for n in nums {
        if n > best {
            best = n
        }
    }
    return best
}
fn main() -> int {
    let nums = [3, 7, 2, 9, 4]
    print(sum(nums))
    print(max_of(nums))
    return 0
}
vpp
$ ready. Click Test program.