Functions¶
Functions are prevalent in Peel code. You use the fn keyword to declare a new function. Functions in Peel use snake_case as the conventional style for function and variable names.
Defining a Function¶
Here is a basic function that prints a greeting:
fn main() {
println!("Hello, world!");
another_function();
}
fn another_function() {
println!("Another function.");
}
Peel execution begins in the main function. Other functions can be defined anywhere; as long as they are accessible in the same file or imported, they can be called.
Parameters¶
We can define functions to have parameters, which are special variables that are part of a function's signature. When you define parameters, you must declare their type.
fn print_labeled_measurement(value: int, unit_label: string) {
fmt.println("The measurement is:", value, unit_label);
}
fn main() {
print_labeled_measurement(5, "h");
}
Return Values¶
Functions can return values to the code that calls them. We don't name return values, but we must declare their type after an arrow (->).
Statements vs. Expressions¶
- Statements are instructions that perform some action and do not return a value. For example,
let y = 6;is a statement. - Expressions evaluate to a resultant value. Adding
5 + 6is an expression that evaluates to11. Calling a function is an expression. Calling a macro is an expression. A new scope block created with curly brackets{}is an expression.