I want to talk about a very important concept in any programming language, which is the function.
Well, functions are used to encapsulate a chunk of guilt they use to organize your code and save you
from repetitive lines.
So let's say that you have a sequence of lines and you only use it in different places in your code.
Well, maybe you are trying to read the temperature and process it and then do some condition according
to this temperature.
But you want to use it in multiple places in your code?
Well, you could write these five lines every time, but that's really not very efficient.
You need to write it once inside a function which looks like this.
You write your code and then you call this function whenever you want to use it.
Calling a function mean just writing the name of the function and parsing some parameters to it.
So let me break this down to you.
So void right here specifies what will the function return?
So in the case of void, well, we will not be returning anything.
So this statement cannot be written.
What do I mean by return?
Let's say that this function is calculating temperature.
So when this function is over, it should give you a final result, right?
This is what return it after you calculate multiple stuff, you put the result here in return.
So you would say return X, let's say X is the temperature scaled and processed and then you say return
X.
So this function, when it exists, it brings back this X with it to reprogram.
We're going to see examples of that while we go on.
OK, so you need to specify what kind of type it needs to return.
So it could return an entire value.
It would return a boolean, it could return a float.
So it depends on the type of this data that this function would return back with it.
When it's back to the main, you need to specify the typewriter.
So whenever this function is executing and it sees a return, it's going to exit the function and go
back to your, let's say, the loop function, depending on where you have called this function.
And as I said, we will see examples of that as we go on.
No, here is a parameter that you need to pass the function.
So let's say you want to calculate temperature and you call a function to do a single sequence of code.
And, well, this function will take temperature from here.
So you would call the function, you would pass the temperature and the temperature would be processed
here and the result will be returned here.
So here all the parameters that you need in order to process them will be passed using these parameters,
it inside these parentheses.
So you need to specify what is the type of this parameter and you need to write the name of the variable
of that parameter.
And a recap void means returning nothing.
So when you have void, you cannot have any return statement in here.
But I am showing you this return so that if this statement is integer float or whatever, you could
write a return to return evaluable.
So you would ask, why would I need a function that returns nothing?
Well, let's assume that the function will only print stuff on the screen, so it will only print and
then exist.
So there is nothing, no result to return with it.
So let us take a real example.
Void setup and void loop are considered functions, the same thing that we were studying, now an embedded
system will have the main function that will have the whole program running.
So the set up for configuration and loop are both like that.
Now, let's say that we are reading to temperature values from two different sensors on our board.
And let's say that these temperature sensors are attached to a different part of our machine.
So each one is reading a different temperature, but they are all connected to one embedded system,
which is our original.
So let's assume that these two variables are actually what our sensors are reading.
Now, this is not the way that we read an analog input, but let's assume that this are the reading
of our two analog inputs as we are calling them.
Temperature at the picture, too.
OK, so they are from type integer and these are known scaled value to means that we are getting voltage
and we need some formula in order to convert it to temperature.
Now all of these are just imaginary numbers.
None of the formulas are correct.
But I just want to give you this for the sake of argument.
No.
Next, let's say that I have defined an integer called the scale temperature.
OK, so I'm going to store the skill temperature and this valuable.
So I'm saying integer scale temperature equals to a function call.
This function is called calculate temperature from voltage and I am passing a temperature to it.
OK, so I am passing this variable here in order to process it and I'm going to substitute all of this
function with a value that will be calculated from this function.
Now where does this function?
Here it is.
This is calculate them from voltage and it's accepting a temperature.
OK.
Now as you can see, the temperature here is an integer.
We are facing an integer and in our function, a prototype.
We call this the function prototype.
We are calling it a temperature and our function definition.
We are calling this our function definition and its barometer is also an integer.
So the types here are matching.
OK, so here is our function call and this function will be executed when I call it.
So I called it here.
This will be executed with the parameter best, which is this temperature, which is this one.
And now I'm going to return them, which is this value multiplied by 150, divided by 50, and then
we have an eight add to it.
So this will result in a number that will be assigned to scale temperature.
So this will be called this function.
It's here.
Temperature is best, temperature is calculated, and it's to return the back here to be in the scale
temperature.
So this is cold temperature will equal to the result of this return.
OK.
Now, instead of calling this formula again here, we just call the function.
So we're doing the same thing for temperature two and we are passing through it.
Temperature two.
And again, it is jumping through the section of the code.
It's calculating according to temperature two, and it is assigning the result of this return to scale
temperature to that's it.
So let's return here will be back to the main lucrative.
OK, so we can keep our main code clean by defining functions so that we don't have any complicated
processes and complicated calculations.
We put all of these in separate functions and in our main program, we just call those complicated functions
that compress multiple lines of code.
Now, in our case, it's not really complicated, right?
Just doing a simple calculation, but sometimes you will have a bigger function that it's not really
practical to copy pasted all along your loop enough that you call it once, bypassing the right parameters
to it.