1
00:00:00,520 --> 00:00:01,880
Hello again, everyone.

2
00:00:02,500 --> 00:00:09,370
My friends, we have now completed the topic of data types, so in this lesson I want to talk about

3
00:00:09,370 --> 00:00:10,380
functions.

4
00:00:10,840 --> 00:00:15,430
So for this, I will create a new file and continue with it.

5
00:00:16,000 --> 00:00:21,910
And I want different topics to be as tidy as can be.

6
00:00:22,990 --> 00:00:29,950
As you will remember, we were typing functions using the function keyword in JavaScript.

7
00:00:31,700 --> 00:00:34,730
And I'd say this function adds three different numbers and.

8
00:00:44,410 --> 00:00:47,530
Returns this total result to us.

9
00:00:50,100 --> 00:00:52,620
So that's how you write a simple function, right?

10
00:00:53,760 --> 00:01:00,030
In fact, when we look here, we're going to have trouble with the parameters that come in here, have

11
00:01:00,720 --> 00:01:02,100
another type, the number.

12
00:01:02,100 --> 00:01:02,370
Right.

13
00:01:03,060 --> 00:01:09,330
So let's say that we give string values for the first variable here and number of values for the others.

14
00:01:10,060 --> 00:01:14,820
Well, this is going to be a big problem for us down the road while we're working.

15
00:01:15,270 --> 00:01:19,410
So again, here, typescript, offers a great solution.

16
00:01:21,030 --> 00:01:26,370
So likewise, I'll define the second function, of course, with a different function name.

17
00:01:27,460 --> 00:01:32,760
And in the same way will create a function that adds three different numbers and returns a result,

18
00:01:33,490 --> 00:01:38,290
but in order to avoid the previous problem, I can specify that the.

19
00:01:41,730 --> 00:01:44,040
Next parameters will be numbers.

20
00:01:48,370 --> 00:01:55,070
So I specify that the variables will be numbers and the result will be number just like that.

21
00:01:55,750 --> 00:02:00,010
So when I write a different type for the variable to come in here.

22
00:02:02,110 --> 00:02:04,060
The program warns me.

23
00:02:05,030 --> 00:02:06,410
You can use no here.

24
00:02:09,250 --> 00:02:11,500
As if it's not as string or boolean value.

25
00:02:13,330 --> 00:02:19,030
For example, as you can see, when we write the first function here, the variable types appear as

26
00:02:19,030 --> 00:02:19,360
any.

27
00:02:22,730 --> 00:02:27,230
However, it is obvious that these values will be numbers for the second function.

28
00:02:28,240 --> 00:02:32,800
And as you can see, everything is just a little clearer and TypeScript.

29
00:02:34,160 --> 00:02:41,150
And actually, that's what I was trying to tell you in the first lesson, so we can see the error in

30
00:02:41,150 --> 00:02:44,620
our code while writing, not while working.

31
00:02:45,530 --> 00:02:49,460
So it's possible to fix errors quickly on the fly.

32
00:02:51,930 --> 00:02:58,200
So, yes, my friends, we can define these functions like this, so for another way, we can define

33
00:02:58,200 --> 00:03:00,330
them by using the let key word.

34
00:03:01,620 --> 00:03:08,040
So we assign this function to a variable, we have already seen this article before.

35
00:03:12,440 --> 00:03:15,920
And here again, in the same way will add three numbers and.

36
00:03:22,590 --> 00:03:24,990
Return the sum as a result.

37
00:03:28,260 --> 00:03:35,460
So now when defining this function, I stated that the result would be a number, so if I delete the

38
00:03:35,460 --> 00:03:36,420
return here.

39
00:03:37,950 --> 00:03:39,840
TypeScript will return an error.

40
00:03:41,200 --> 00:03:47,610
Because I stated that when I was defining the function, a value would be returned.

41
00:03:50,150 --> 00:03:55,730
All right, so I want to show you one more thing that can be done with functions.

42
00:03:57,180 --> 00:04:03,780
So while we're doing these function, definitions always define three numbers and three values as parameters,

43
00:04:04,590 --> 00:04:07,380
and I want this last number to be optional.

44
00:04:08,350 --> 00:04:14,170
So I'll just send two parameters and normally there's no problems with JavaScript, but.

45
00:04:18,370 --> 00:04:23,770
When you look at how I'm writing like this, of course, it'll give an error with TypeScript.

46
00:04:29,240 --> 00:04:34,970
But if I come in here and put in a question mark, the error goes away, why is that?

47
00:04:36,110 --> 00:04:43,550
Because I've stated that this variable is optional and sometimes only two parameters need to be entered.

48
00:04:45,390 --> 00:04:48,690
So we can reinforce this with a small example.

49
00:04:50,730 --> 00:04:57,720
So now we have an optional parameter within this function, first, we'll add two numbers in total and

50
00:04:57,720 --> 00:05:05,190
I'll put two to the variable count then if the third number is not defined.

51
00:05:15,420 --> 00:05:16,320
We added.

52
00:05:17,500 --> 00:05:20,020
At total and we increase.

53
00:05:23,330 --> 00:05:24,530
The count by one.

54
00:05:34,450 --> 00:05:36,760
Then we can go and print these values on a console.

55
00:05:45,960 --> 00:05:49,500
So are you ready for another of usage of this function?

56
00:05:51,530 --> 00:05:52,640
So as we're.

57
00:05:53,780 --> 00:05:59,480
Giving the parameters here later, we can also give default values when defining the function.

58
00:06:01,610 --> 00:06:04,760
For example, as you see, I'll define the last number here.

59
00:06:05,850 --> 00:06:08,130
I'm sending the other numbers later.

60
00:06:08,280 --> 00:06:13,200
So in total, these two numbers and this number will be.

61
00:06:14,130 --> 00:06:17,340
And if I come in here and send three numbers again.

62
00:06:18,570 --> 00:06:20,490
This third number will receive.

63
00:06:21,760 --> 00:06:23,110
The last number that we sent.

64
00:06:24,690 --> 00:06:34,320
We don't get any errors both the first time and this time, so here the values we determine in this

65
00:06:34,320 --> 00:06:36,990
way are default values.

66
00:06:40,230 --> 00:06:41,310
Terrific.

67
00:06:41,340 --> 00:06:48,180
So now there's another feature that I want to tell you about that we can use and functions, rest parameters.

68
00:06:49,590 --> 00:06:56,040
I don't know if you're aware of it, but we've talked over a number of parameters so far.

69
00:06:56,760 --> 00:07:02,760
But sometimes we might not know the exact number of parameters while preparing our projects.

70
00:07:03,480 --> 00:07:10,500
For example, the number of customers visiting a site or the number of invitees participating, you

71
00:07:10,500 --> 00:07:15,120
know, in an invitation and event invitation.

72
00:07:17,390 --> 00:07:22,460
You get the idea, but in such cases, we can use rest parameters.

73
00:07:23,420 --> 00:07:27,410
So now let's see how to use rest parameters just by defining a new function.

74
00:07:28,700 --> 00:07:33,680
For example, I want to find the average age of people who log in to any site in this function.

75
00:07:34,010 --> 00:07:40,970
So here, while defining the parameter that will go to the function, I'll just put three dots in parentheses

76
00:07:40,970 --> 00:07:41,320
first.

77
00:07:42,860 --> 00:07:51,680
Now, specify the variable name and what type it is, so I'm using a number array now here it is important

78
00:07:51,680 --> 00:07:54,650
that all the following parameters are of the same type.

79
00:07:55,800 --> 00:08:05,280
So the values will be numbers also, if the first value is certain, then we can define it here so we

80
00:08:05,280 --> 00:08:09,870
can find the rest parameter as the second parameter.

81
00:08:11,630 --> 00:08:19,220
So let's calculate the average operation right now, so for this, I define two variables named total

82
00:08:19,430 --> 00:08:20,270
and count.

83
00:08:21,190 --> 00:08:24,580
I'll just make zero as the default.

84
00:08:30,120 --> 00:08:31,530
Then I'll just create a for loop.

85
00:08:33,050 --> 00:08:39,950
The first value starts and zero and continues until the last element of the number, right, and we'll

86
00:08:39,950 --> 00:08:42,260
add the next variable with the total.

87
00:08:49,000 --> 00:08:51,370
And then the count increases by one.

88
00:08:55,590 --> 00:09:00,990
So as a result, I can be dividing the total by count and get.

89
00:09:08,290 --> 00:09:10,480
All right, so now that defines our function.

90
00:09:11,940 --> 00:09:15,480
And now we can send as many parameters as we want to hear.

91
00:09:18,450 --> 00:09:23,190
So if we send two, three or four parameters to this function, it's not going to have a Brahma.

92
00:09:32,270 --> 00:09:39,590
So do you see that then we can easily perform operations in uncertain situations just by using the rest

93
00:09:39,590 --> 00:09:40,310
parameter?

94
00:09:42,680 --> 00:09:49,220
Now, I want to show you one more thing, so the function definition here is the same as JavaScript

95
00:09:49,850 --> 00:09:51,980
and there's not really a problem with that.

96
00:09:52,100 --> 00:09:59,330
But in order to be more professional, we can use the typescript const keyword.

97
00:10:00,600 --> 00:10:03,930
So we can define the function in this way.

98
00:10:05,740 --> 00:10:07,750
All right, my friend, so that's the end of the lesson.

99
00:10:08,080 --> 00:10:15,850
As you can see, we're making faster expressions, OK, especially now that you're more familiar with

100
00:10:15,850 --> 00:10:18,280
some of these topics because of JavaScript.

101
00:10:20,480 --> 00:10:26,490
So here we examined some of the differences in TypeScript, because the logic is pretty much the same.

102
00:10:26,900 --> 00:10:29,150
We can clarify the parts that are different.

103
00:10:29,540 --> 00:10:29,960
All right.

104
00:10:29,970 --> 00:10:31,580
So I want to see in the next lesson.
