[No Audio]
This tutorial is about Function Pointer types,
which are used to refer to a function
whose identity is not necessarily known at
compile time. As opposed to referencing a
data value, a function pointer points to
executable code within a memory. The function
type allows us to define a variable which
will be of type function which means that, it
will be pointing to some function and then we
can change the pointer in runtime to change
it to, point to some other function. The topic
is not much complex, and simple and therefore,
this tutorial will be a bit short. With this,
let's start to learn about Function Types.
We will first define a couple of functions, I
will define a max and min function, which will
compute, and return the maximum and minimum of
the two input values.
First, we will define the max function.
[No Audio]
Next, I will define our main function
in the very same way.
[No Audio]
Let us use these functions now
using function pointers.
[No Audio]
I will first define
a variable of type function, and we'll
initialize it from the function of main.
You may note that, the type of the variable is
being automatically set by the Rust compiler
to that of the function signature, containing
the information of the inputs and outputs.
We can now use the same variable to call a
function, I will call the function in the
print statement with some inputs.
[No Audio]
The Rust will interpret the variable of f as
a function and the value inside the
parentheses as input to the function.
Let us execute the code.
[No Audio]
It displays correctly the
minimum of the two values, now let us change
the value of the variable to
that of max and execute again.
[No Audio]
You may note that the function of maximum is
now being executed at the maximum of the two
values is being displayed to us. This way, we
can change the value of the function variable
to refer to different functions using a
single variable. Okay now let us see some of
the uses of the function types. We will first
look at the case, where we can use the
function pointer as an argument or parameter
to another function. I will first create a
simple function for printing an input string.
[No Audio]
Next, I will define another function called
fn print_full_info.
[No Audio]
The first parameter of this function will be a function type.
[No Audio]
The second input will be a string
and the last input will be the age.
[No Audio]
Inside the function, we will first call the
fn prints_name using the
function pointer which is f, and we'll pass
the input string of some_one to it.
[No Audio]
Next, I will add an additional print statement
for printing the age of the person.
[No Audio]
I will now call these functions from the main.
I will first define a couple of variables
containing the name and age.
[No Audio]
Next, I will call the fn prints_full_info with suitable inputs.
[No Audio]
Let us cargo run this to see the outputs.
[No Audio]
The fn print_full_info first calls the function of
prints_name using the functional pointer of f n
and then it next prints my name.
Let us cover some more examples and details
about function pointers. In this example we
will define a function called add_one and
we'll call it from another function called twice.
First, I will define the add_one
function, which will do nothing but add one to the
value that is being passed to it as an input.
[No Audio]
Next, I will define another function
which will call this function twice. So I
will call this function as do_twice.
[No Audio]
The input to the function will be a
functional pointer in i32 value and the
output is i32 value.
[No Audio]
Inside the body of the function, I will call
the fn add_one twice, and
we'll add the values together using the
function pointer.
[No Audio]
Now, any value that is
being passed to this function, will be added
one and then the values will be added.
For instance, if I pass a value of let's say 10.
And then a value of 1 will be added which
will make its value 11. And then 11 will be
added to itself, which will make the value equal to 22.
Let us now use these functions
in the main program now, I will use a
variable which will hold the output value
from the function do_twice.
[No Audio]
The function name add_one will
serve as a type of functional pointer for the
variable f inside the parameter list, of the
do_twice function. Next I will
add a simple print statement, which will
print and have the value of the answer.
[No Audio]
The answer in this case will be having a
value of 12. Because a value of 1 will be
added to the value of 5, which will make
its value equal to 6 and then 6 will be
added to itself. So the final answer will be 12.
Let us cargo run this.
[No Audio]
We obtained the required answer. The function pointers
are very useful in some advanced topics such
as communicating with other languages and
external API's. Okay, that brings us to the
end of this tutorial, we have learned about
the functional pointers which provides a nice
way for using the functions inside other
functions are having a pointer to a function code.
In the upcoming tutorial, we will be
looking at the concept of iterator, so do
come back for learning that and until next
tutorial, happy Rust programming.
[No Audio]