Parameters

Parameters allow us to provide information to a function at the time when we ask it to run.

Asking a function to run is called calling it.

When we define a function, we can tell it the variable names and types we want it to use for receiving parameters, as in the following example:

pub fn set(&mut self, value: i32) {
self.current = value;
}

We'll talk about self in the Implementing behavior for types section of this chapter. For now, ignore it and take a look at value. Here, we've provided a name and data type, just as we would if we were using let to create a new variable. What we have not done is provide a value for the value variable, because that happens when the function is called.

We've seen function calls all along, but for the sake of clarity, they look like this:

some_function(2 + 2, false)

In that example, some_function is the name of a function, and the values that are assigned to its parameters are the results of the expressions 2 + 2 and false. The parameter expressions are evaluated before the function is called, so the actual values of the parameters are the number 4 and the Boolean, false.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.0.145