Hello again, everyone.
My friends, we have now completed the topic of data types, so in this lesson I want to talk about
functions.
So for this, I will create a new file and continue with it.
And I want different topics to be as tidy as can be.
As you will remember, we were typing functions using the function keyword in JavaScript.
And I'd say this function adds three different numbers and.
Returns this total result to us.
So that's how you write a simple function, right?
In fact, when we look here, we're going to have trouble with the parameters that come in here, have
another type, the number.
Right.
So let's say that we give string values for the first variable here and number of values for the others.
Well, this is going to be a big problem for us down the road while we're working.
So again, here, typescript, offers a great solution.
So likewise, I'll define the second function, of course, with a different function name.
And in the same way will create a function that adds three different numbers and returns a result,
but in order to avoid the previous problem, I can specify that the.
Next parameters will be numbers.
So I specify that the variables will be numbers and the result will be number just like that.
So when I write a different type for the variable to come in here.
The program warns me.
You can use no here.
As if it's not as string or boolean value.
For example, as you can see, when we write the first function here, the variable types appear as
any.
However, it is obvious that these values will be numbers for the second function.
And as you can see, everything is just a little clearer and TypeScript.
And actually, that's what I was trying to tell you in the first lesson, so we can see the error in
our code while writing, not while working.
So it's possible to fix errors quickly on the fly.
So, yes, my friends, we can define these functions like this, so for another way, we can define
them by using the let key word.
So we assign this function to a variable, we have already seen this article before.
And here again, in the same way will add three numbers and.
Return the sum as a result.
So now when defining this function, I stated that the result would be a number, so if I delete the
return here.
TypeScript will return an error.
Because I stated that when I was defining the function, a value would be returned.
All right, so I want to show you one more thing that can be done with functions.
So while we're doing these function, definitions always define three numbers and three values as parameters,
and I want this last number to be optional.
So I'll just send two parameters and normally there's no problems with JavaScript, but.
When you look at how I'm writing like this, of course, it'll give an error with TypeScript.
But if I come in here and put in a question mark, the error goes away, why is that?
Because I've stated that this variable is optional and sometimes only two parameters need to be entered.
So we can reinforce this with a small example.
So now we have an optional parameter within this function, first, we'll add two numbers in total and
I'll put two to the variable count then if the third number is not defined.
We added.
At total and we increase.
The count by one.
Then we can go and print these values on a console.
So are you ready for another of usage of this function?
So as we're.
Giving the parameters here later, we can also give default values when defining the function.
For example, as you see, I'll define the last number here.
I'm sending the other numbers later.
So in total, these two numbers and this number will be.
And if I come in here and send three numbers again.
This third number will receive.
The last number that we sent.
We don't get any errors both the first time and this time, so here the values we determine in this
way are default values.
Terrific.
So now there's another feature that I want to tell you about that we can use and functions, rest parameters.
I don't know if you're aware of it, but we've talked over a number of parameters so far.
But sometimes we might not know the exact number of parameters while preparing our projects.
For example, the number of customers visiting a site or the number of invitees participating, you
know, in an invitation and event invitation.
You get the idea, but in such cases, we can use rest parameters.
So now let's see how to use rest parameters just by defining a new function.
For example, I want to find the average age of people who log in to any site in this function.
So here, while defining the parameter that will go to the function, I'll just put three dots in parentheses
first.
Now, specify the variable name and what type it is, so I'm using a number array now here it is important
that all the following parameters are of the same type.
So the values will be numbers also, if the first value is certain, then we can define it here so we
can find the rest parameter as the second parameter.
So let's calculate the average operation right now, so for this, I define two variables named total
and count.
I'll just make zero as the default.
Then I'll just create a for loop.
The first value starts and zero and continues until the last element of the number, right, and we'll
add the next variable with the total.
And then the count increases by one.
So as a result, I can be dividing the total by count and get.
All right, so now that defines our function.
And now we can send as many parameters as we want to hear.
So if we send two, three or four parameters to this function, it's not going to have a Brahma.
So do you see that then we can easily perform operations in uncertain situations just by using the rest
parameter?
Now, I want to show you one more thing, so the function definition here is the same as JavaScript
and there's not really a problem with that.
But in order to be more professional, we can use the typescript const keyword.
So we can define the function in this way.
All right, my friend, so that's the end of the lesson.
As you can see, we're making faster expressions, OK, especially now that you're more familiar with
some of these topics because of JavaScript.
So here we examined some of the differences in TypeScript, because the logic is pretty much the same.
We can clarify the parts that are different.
All right.
So I want to see in the next lesson.