WEBVTT

00:00.240 --> 00:06.300
It's time to refer to another very important aspect in JavaScript, which is called functions, because

00:06.300 --> 00:10.400
of that, this section is about from basic to intermediate JavaScript.

00:10.890 --> 00:14.190
We will discuss what the functions just are and how they work.

00:14.880 --> 00:18.830
But in coming sections, you will see how they act in advanced JavaScript.

00:19.950 --> 00:24.250
Sometimes you may need to write some code and then repeat this code over and over again.

00:24.720 --> 00:26.880
This is the case when we use functions.

00:28.680 --> 00:34.240
They allow us to minimize the amount of our code and they are very flexible to use.

00:34.800 --> 00:36.720
Let's see what the functions really are.

00:37.410 --> 00:43.000
Functions like a group, a series of statements together to perform a specific task.

00:43.740 --> 00:50.730
So as we said, if different parts of a script repeat the same task, you can reuse the function rather

00:50.730 --> 00:52.920
than repeat the same set of statements.

00:54.200 --> 01:01.340
All right, in order to create a function, we need to write a keyword function followed by the function

01:01.340 --> 01:01.680
name.

01:02.990 --> 01:06.260
That name should describe the task function is performing.

01:07.370 --> 01:12.410
Then we have to place parentheses in which we can pass parameters.

01:13.670 --> 01:19.900
We use parameters when functions need to be provided with information in order to achieve a given task.

01:19.940 --> 01:25.340
The steps that the function is to do in order to perform this task are written in a code block.

01:25.550 --> 01:32.000
Inside the process, the code block consists of one or more statements and you do not write the semicolon

01:32.420 --> 01:36.740
after the closing calibrates like you do it after the usual statements.

01:38.200 --> 01:43.180
All right, let's write something inside the code blog, the blog.

01:44.160 --> 01:44.970
Hello, world.

01:48.250 --> 01:51.480
Reload the page and see that we have nothing.

01:51.520 --> 01:56.530
You so the statements in the function are not always executed when the page loads.

01:57.220 --> 02:02.030
In order to do that, you need to call or in other words, invoke the function.

02:02.860 --> 02:07.750
It's very simple to do it just need to write function name with parentheses.

02:09.530 --> 02:11.100
So we've got here.

02:11.150 --> 02:17.450
Hello, World in Council, all right now, right, a bit more complex example.

02:17.690 --> 02:23.780
Suppose that person is passing an exam and trying to enroll in university or at least in college.

02:24.530 --> 02:26.740
For that, the person needs to get the proper score.

02:27.200 --> 02:31.140
So write the function and call it pass exam.

02:32.810 --> 02:38.270
I'm going to use your two parameters, name of the student and score.

02:39.260 --> 02:46.000
Then imagine that in order to passing university, it's required to get at least 71 points.

02:46.670 --> 02:57.800
So create a new variable, call it plus unique and assign to devalue seven to one and after that create

02:57.800 --> 03:01.970
another variable which will be lower limit for passing an exam in college.

03:02.420 --> 03:02.830
Right.

03:02.840 --> 03:06.560
Verbal pass call equals to 51.

03:07.240 --> 03:10.660
OK, now we are going to use each statement.

03:10.700 --> 03:13.400
We are already familiar with that at first.

03:13.400 --> 03:16.900
Let's check if the person could enroll in university.

03:17.420 --> 03:18.080
So right.

03:18.080 --> 03:26.720
If score is greater than or equal to pass uni, then run in Castle.

03:29.180 --> 03:37.340
Place your name Parameter Plus has enrolled in university with.

03:38.970 --> 03:39.420
And.

03:40.510 --> 03:41.730
Here, we need more.

03:43.080 --> 03:44.430
Then plus points.

03:47.140 --> 03:53.530
So if this condition is false, which means that he or she has got lower than the limit in university

03:53.530 --> 04:00.190
was required, then we need to use elective statements which will check if a person could enroll in

04:00.190 --> 04:00.690
college.

04:01.450 --> 04:02.170
So right.

04:02.190 --> 04:02.860
Else if.

04:04.000 --> 04:08.950
And the condition score is greater than or equal to Pascal.

04:11.850 --> 04:15.630
Then inside the Brace's right Coslett log.

04:18.940 --> 04:23.560
Then copy it from if statement and just change.

04:26.040 --> 04:31.320
University into college and the next possible case is to fail.

04:31.830 --> 04:34.200
For that, let's use our statement.

04:35.830 --> 04:37.180
And then running castle.

04:38.400 --> 04:41.640
Name plus has failed.

04:44.700 --> 04:51.060
All right, function is ready, but as we said, in order to execute all those codes, we need to call

04:51.060 --> 04:51.930
it so.

04:51.930 --> 04:54.460
Right, the name of the function pass exam.

04:55.720 --> 05:05.020
And pursue the relevant arguments, right, John, as the name of the student and score 75.

05:06.710 --> 05:16.280
Reload the page and you see that we have here, John, has enrolled in university with 75 points, that's

05:16.280 --> 05:19.360
the power of functions for every next student.

05:19.370 --> 05:21.230
You don't need to write this code again.

05:21.590 --> 05:26.710
You just need to call this function over and over again and just change the arguments.

05:27.560 --> 05:29.170
So let's do it right.

05:29.180 --> 05:38.270
Pass exam, then place your as the name Mary and as score 65.

05:40.520 --> 05:45.230
Then to the same again and pursue your arguments as Bob.

05:46.420 --> 05:48.070
And that 45.

05:49.990 --> 05:53.000
Reload and we have the proper results.

05:53.620 --> 05:56.860
John has enrolled in university with seven to five points.

05:57.430 --> 06:03.140
Mary has enrolled in college with 65 points, and Bob has failed.

06:04.330 --> 06:04.870
All right.

06:05.540 --> 06:09.450
You may notice that sometimes I set parameters and arguments.

06:10.240 --> 06:15.000
So let me explain what is the difference between them, what the function was declared.

06:15.010 --> 06:21.150
You saw the words name and score used these words acting the same way as variables.

06:21.700 --> 06:24.280
So they are called parameters.

06:24.880 --> 06:32.230
But what the function was called, we specified real names and actual scores of the students that were

06:32.260 --> 06:33.510
used to get the results.

06:33.970 --> 06:36.430
These values are called arguments.

06:37.890 --> 06:38.540
Makes sense.

06:40.470 --> 06:42.690
So those are parameters.

06:43.930 --> 06:46.120
And those are arguments.

06:47.620 --> 06:57.640
All right, suppose that core consists of quiz score and s.A score points, we can create a function

06:57.640 --> 06:59.670
which will calculate the total score.

07:00.660 --> 07:11.970
Right, function and call it calculates court that insert here two parameters for Questcor and for s.A

07:11.970 --> 07:12.420
score.

07:15.100 --> 07:22.330
Then inside codebook create a variable score, which equals to Questcor, plus a set score.

07:26.700 --> 07:31.950
So in order to get this core value, we need to use special keyword return.

07:33.050 --> 07:39.620
The return statement stops the execution of a function and returns the value from that function.

07:40.680 --> 07:47.490
In this case, we want to get a core value, therefore we have to right return its core.

07:49.170 --> 07:56.250
Now, let's call this function as the arguments inside the past example function and set the arguments

07:56.250 --> 08:03.990
for that function, for example, as forty four Questcor and 34 essay's court.

08:04.410 --> 08:09.300
So this function will calculate core value and return it as an argument.

08:09.300 --> 08:14.100
For example, function, reload the page and see.

08:14.460 --> 08:17.850
John has enrolled in college with 70 points.

08:18.860 --> 08:24.230
Seventh is the sum of 30 and 40, which are the arguments of recalculates court function.

08:25.780 --> 08:26.440
Makes sense.

08:27.880 --> 08:34.690
OK, so in this lecture, we have discussed what the functions are in JavaScript, how they work and

08:35.080 --> 08:37.900
what are they use for that's what.
