Functions
Now that we've covered blocks and scope, let's talk about functions. Think of functions as named blocks that can take input (parameters) and produce output (return values). They allow you to encapsulate logic and reuse code without copying and pasting like it's 1999.
Here's how you define a simple function in Zig:
fn add(a: i32, b: i32) i32 {
return a + b;
}
This add function takes two i32 parameters and returns their sum. It's a block of code that you can call whenever you need to add two numbers, saving you from rewriting a + b every time—as exhilarating as that might be.
Parameters – accepting inputs
Function parameters are the inputs your function needs to perform its task. In Zig, parameters are immutable by default. This means you can't modify them inside the function unless you explicitly make a copy. It keeps your functions pure and your intentions...