After variables.
Let's see what functions are.
And as we did for valuables, let's first understand why you need functions.
And I'm going to go quite quick here.
Okay?
The goal is to give you an overview of functions, not to go into much details about everything you
can do about functions, so you will learn more about functions as you progress in the course through
how the different activities and practice exercises.
So if it doesn't make complete sense for now, well, don't worry too much.
It will by the end of the course.
So what do we have here?
What?
I have created two variables here.
You can see number one.
And number two, those variables are integer numbers three and five.
And what I do in the void setup is I just well, I just create a new variable A that is the double of
number one, and then B, that is the double of number two.
So let's say that what you want to do is simply to multiply the number by two.
And so every time you're going to need to do the variable multiplied by two.
Thing is this would be very inconvenient to always need to repeat the same code every time you need
to use this functionality.
And here it's a very small functionality, but the functionality can be much bigger with many lines
of code.
So what we need here is a function.
A function is a reusable block of code that you can call from different parts of your program.
And let's just create one to see what it is.
So I'm just going to create a function here.
Actually, void setup is already a function and void loop is a function.
So let's create a new one here on top and let's call it double number like this, because we want to
double a number and I'm going to make sure that I don't add any spaces for any new world.
I just added new uppercase and you can see that at the beginning of the function.
So that will be the name.
But at the beginning of the function, we have something and we need to give the data type that we want
to return because the function will accept some parameters as an input and we'll return something as
an output.
And so here I'm just going to put INT and then space double number.
That means that this function will return an integer number.
So I put the data type the name, and then I open and close the parentheses and I have the definition
of my function.
You can see just like the void set up and the void loop.
Now what I can also do is when I have a function, as I told you, we can give it an input.
And so the input here, I'm just going to put int number, which means that the function can receive
an integer value here that's going to be named number inside the function.
And once I have this, I can just go back to a new line and open and close curly brackets.
Okay.
Very similar to what we did here with void, setup and void.
But in this case you can see I have INT and I also have one parameter.
You could have as many parameters as you want.
You just separate them with a comma.
But let's just keep things simple for now with just one parameter.
So this what is this function doing?
Well, this function will need to actually multiply the number here by two.
So let's just create a variable.
INT result, for example, is equal to number multiplied by two.
The number is the variable.
As you can see that we get here in the parameter of the function.
So that's what we do in the function.
And then there is a keyword that you need to use in a function.
It's called return can see turns green return result.
So what do I return?
I return this variable and this variable is an integer number.
So that match is the type that I want to return.
So now I have this block of code.
You can see this block of code that's going to take as an input a number here, an integer number,
and that's going to return another integer.
But you can see this time the returned number is the double of the number that we pass as an input.
And so once I have created a function, I can actually use it.
And so here, instead of doing number one multiplied by two, I can just what we call calling the function.
Actually, I can call the function.
So double number and I pass number one.
Double number, I call this function and I pass what I pass the number one variable here that I will
pass to the input of the function.
And so the number one will go here.
It will be multiplied by two, and then we return the result, which means that it will go back here
and then a will be equal to the result of this function, which is the double of the number.
And I can do the same thing here, double number with number two, for example, I can give a different
number every time.
Okay, so that's a reusable block of code that I can use with a different number every time.
Great.
And that's a first example of a.
Function and well, you can combine functions as much as you want.
For example, you could call a function inside another function, and that's what we are going to do
here.
I'm going to create a function and I'm going to name it Void, and I'm going to explain to you void
print double number and this one gets also int number.
So what is this?
Well, you can see that with a function you need to return data type.
So you need to return, for example, an int or a float number or any other kind of data.
But if you put void, void means that you will not return anything.
And you can see that's actually what's happening in the void setup.
This is a function and the void loop also is a function, but you don't have any return keyword because
this is void.
So we don't have any return keyword and that's what we do here.
What we want to do is just to print the double of the number that we get from the parameter.
So I put the void and then I put my opening and closing curly brackets.
And what I can do here, let's say I want to so I want to print the double of this number.
But instead of doing number multiplied by two, I can just call this function so I could do serial dot
print ln to print something.
And what I can print is I can just do double number and I pass the number that I receive.
I pass it here and I close all the parentheses.
So what's going to happen here is that if I call this function from here, let's say call it here,
print double number with number one, what's going to happen is so we have created number one here and
then we call the function named print double number with number one.
So we go here.
The number one is going to be passed here.
So the int number in this function is going to become the value of number one.
So it's going to be three.
So here we have three.
And then what we do is we do select println with double number.
So here we are calling another function.
So you see from one function we call another function.
And so I call the double number and I pass what I get here as an input.
I pass it here as an input for this function.
So you can see I pass the values from function to function.
And then this function is going to compute the double of the number, return the double of the number.
So return the result.
And so basically this here is going to be evaluated to the result of the function.
So this will become the double of the number.
And then what we do is we print the double of the number.
All right?
And so we can we can actually call it once I can call it twice with different numbers, etc., Actually,
if we want to test, I would also need to initialize sales.
So begin.
Let's put this.
Moderate.
And let's actually run this.
Lets name it functions.
Okay.
Don't uploading.
If I open the sale monitor, you will see that we have six and ten.
So six is the double of the number one.
Three and ten is the double of the number two five here.
So what I do, I just print double number.
So I pass the three is going to go here to double number is going to go here is going to be return and
we print it here.
And then number two is going to be five.
So we call this function with five.
It goes here on line eight and then we call the one number on line two.
So the number five is passed until here and then we reach on ten and then here we can print ten.
And one thing we can maybe do also here as an improvement is that, well, in the double number function,
what we do is we create a viable that we just return.
So we don't use this variable for anything else than to return it.
So instead of just creating that, I can just actually do return number multiplied by two.
So if it's the last thing to do is you don't need to use the variable that you create, then you just
can return whatever that you compute here directly.
And this is going to work the same.
All right.
So that was a very quick introduction about functions.
I know this is a quite complex topic when you begin, but don't be afraid if you don't understand everything,
it will all make sense while we go through how the course with all of the different exercises.
Okay.
And later on, you can come back to this lesson and eventually it will make more sense.