Functions

Define reusable functions with explicit parameter types and return types.

1

Core concepts

fn, typed parameters, return values, function signatures

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: Create add

A function signature tells the compiler the function name, its parameters, and its return type. V++ requires explicit parameter and return types for function declarations.

The body can use the parameters just like local values. return sends the computed value back to the caller.

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 add(a: int, b: int) -> int {
    return a + b
}
4

Step 2: Create greet

Functions can return strings as well as numbers. The parameter name is local to the function. The compiler checks that the returned expression is compatible with string.

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 greet(name: string) -> string {
    return "Hello, " + name
}
5

Step 3: Call both functions from main

main calls add and greet. Each call produces a value which is passed to print. This separates reusable logic from the entry point.

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 {
    print(add(10, 20))
    print(greet("Shaurya"))
    return 0
}
6

Complete source

main.vpp
fn add(a: int, b: int) -> int {
    return a + b
}
fn greet(name: string) -> string {
    return "Hello, " + name
}
fn main() -> int {
    print(add(10, 20))
    print(greet("Shaurya"))
    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.

30
Hello, Shaurya
8

Run the project

terminal
vpp run projects/03-functions/main.vpp
9

What the learner should understand after this project

Functions let a program separate reusable logic from the entry point. Explicit signatures make the intended input and output types visible.

10

Common mistakes to teach

  1. Missing parameter types
  2. Returning a value with the wrong type
  3. Calling a function with the wrong number of arguments
11

Practice extension

Write a multiply function and a welcome function. Call both from main.

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 add(a: int, b: int) -> int {
    return a + b
}
fn greet(name: string) -> string {
    return "Hello, " + name
}
fn main() -> int {
    print(add(10, 20))
    print(greet("Shaurya"))
    return 0
}
vpp
$ ready. Click Test program.