Functions
Define reusable functions with explicit parameter types and return types.
Core concepts
fn, typed parameters, return values, function signatures
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: 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.
fn add(a: int, b: int) -> int {
return a + b
}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.
fn greet(name: string) -> string {
return "Hello, " + name
}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.
fn main() -> int {
print(add(10, 20))
print(greet("Shaurya"))
return 0
}Complete source
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
}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
Run the project
vpp run projects/03-functions/main.vppWhat 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.
Common mistakes to teach
- Missing parameter types
- Returning a value with the wrong type
- Calling a function with the wrong number of arguments
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.
Full program
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
}