Rust Programming: Parameters

0
A parameter is a special kind of variable used in a function to refer to one of the pieces of data provided as input to the function. For example, the following function has two parameters:

fn print_labeled_measurement(value: i32, unit_label: char) {
    println!("The measurement is: {value}{unit_label}");
}

The first parameter, `value`, is of type `i32`. This means that the function expects to be passed an integer value as input. The second parameter, `unit_label`, is of type `char`. This means that the function expects to be passed a single character as input. When we call the `print_labeled_measurement` function, we can pass it two values:

print_labeled_measurement(5, 'h');

The first value, 5, is the value for the `value` parameter. The second value, 'h', is the value for the `unit_label` parameter. The `print_labeled_measurement` function will then print the following output:

The measurement is: 5h

As you can see, the function has used the values we passed in to fill in the `{value}` and `{unit_label}` placeholders in the format string.

Parameters are a powerful way to pass data into functions. They allow us to write functions that are more flexible and reusable.

Post a Comment

0Comments
Post a Comment (0)