It's time to refer to another very important aspect in JavaScript, which is called functions, because
of that, this section is about from basic to intermediate JavaScript.
We will discuss what the functions just are and how they work.
But in coming sections, you will see how they act in advanced JavaScript.
Sometimes you may need to write some code and then repeat this code over and over again.
This is the case when we use functions.
They allow us to minimize the amount of our code and they are very flexible to use.
Let's see what the functions really are.
Functions like a group, a series of statements together to perform a specific task.
So as we said, if different parts of a script repeat the same task, you can reuse the function rather
than repeat the same set of statements.
All right, in order to create a function, we need to write a keyword function followed by the function
name.
That name should describe the task function is performing.
Then we have to place parentheses in which we can pass parameters.
We use parameters when functions need to be provided with information in order to achieve a given task.
The steps that the function is to do in order to perform this task are written in a code block.
Inside the process, the code block consists of one or more statements and you do not write the semicolon
after the closing calibrates like you do it after the usual statements.
All right, let's write something inside the code blog, the blog.
Hello, world.
Reload the page and see that we have nothing.
You so the statements in the function are not always executed when the page loads.
In order to do that, you need to call or in other words, invoke the function.
It's very simple to do it just need to write function name with parentheses.
So we've got here.
Hello, World in Council, all right now, right, a bit more complex example.
Suppose that person is passing an exam and trying to enroll in university or at least in college.
For that, the person needs to get the proper score.
So write the function and call it pass exam.
I'm going to use your two parameters, name of the student and score.
Then imagine that in order to passing university, it's required to get at least 71 points.
So create a new variable, call it plus unique and assign to devalue seven to one and after that create
another variable which will be lower limit for passing an exam in college.
Right.
Verbal pass call equals to 51.
OK, now we are going to use each statement.
We are already familiar with that at first.
Let's check if the person could enroll in university.
So right.
If score is greater than or equal to pass uni, then run in Castle.
Place your name Parameter Plus has enrolled in university with.
And.
Here, we need more.
Then plus points.
So if this condition is false, which means that he or she has got lower than the limit in university
was required, then we need to use elective statements which will check if a person could enroll in
college.
So right.
Else if.
And the condition score is greater than or equal to Pascal.
Then inside the Brace's right Coslett log.
Then copy it from if statement and just change.
University into college and the next possible case is to fail.
For that, let's use our statement.
And then running castle.
Name plus has failed.
All right, function is ready, but as we said, in order to execute all those codes, we need to call
it so.
Right, the name of the function pass exam.
And pursue the relevant arguments, right, John, as the name of the student and score 75.
Reload the page and you see that we have here, John, has enrolled in university with 75 points, that's
the power of functions for every next student.
You don't need to write this code again.
You just need to call this function over and over again and just change the arguments.
So let's do it right.
Pass exam, then place your as the name Mary and as score 65.
Then to the same again and pursue your arguments as Bob.
And that 45.
Reload and we have the proper results.
John has enrolled in university with seven to five points.
Mary has enrolled in college with 65 points, and Bob has failed.
All right.
You may notice that sometimes I set parameters and arguments.
So let me explain what is the difference between them, what the function was declared.
You saw the words name and score used these words acting the same way as variables.
So they are called parameters.
But what the function was called, we specified real names and actual scores of the students that were
used to get the results.
These values are called arguments.
Makes sense.
So those are parameters.
And those are arguments.
All right, suppose that core consists of quiz score and s.A score points, we can create a function
which will calculate the total score.
Right, function and call it calculates court that insert here two parameters for Questcor and for s.A
score.
Then inside codebook create a variable score, which equals to Questcor, plus a set score.
So in order to get this core value, we need to use special keyword return.
The return statement stops the execution of a function and returns the value from that function.
In this case, we want to get a core value, therefore we have to right return its core.
Now, let's call this function as the arguments inside the past example function and set the arguments
for that function, for example, as forty four Questcor and 34 essay's court.
So this function will calculate core value and return it as an argument.
For example, function, reload the page and see.
John has enrolled in college with 70 points.
Seventh is the sum of 30 and 40, which are the arguments of recalculates court function.
Makes sense.
OK, so in this lecture, we have discussed what the functions are in JavaScript, how they work and
what are they use for that's what.