Transcript of the tutorial.
TOP
Okay then, my friend.
So it's time to move on to functions, which I think are a really fun part of JavaScript and functions
are probably one of the main building blocks in pretty much any programming language that you come across
and they fall under the object type in our data type list.
Now what functions allow us to do is define a block of code which we can call and execute whenever we
want.
So I guess you could think of functions as a bit like a box with some code inside it that does a particular
thing like this.
Now, when we want to run the code inside that function, all we do is we call the function like this
and it runs the code inside it.
Now, later on, if we want to call it again, we can do and it runs the code again.
And this behaviour is really useful because it means we're only defining the code inside the function
once, but we can run it multiple times whenever we want, whenever we call it.
So imagine we have a web page with a button on it, and every time a user clicks that button, we want
to run the same bit of code.
We could create a function which contains that code that we want to run.
And then whenever a user clicks that button, just call the function to run the code and we can do that.
Whether the user clicks it twice or ten times, it really doesn't matter.
We define the code once in the function and run it or call it as many times as we want in the future.
Now, we could also pass values into functions so that the function could take those values and do something
with it.
And then when it's done, spit out an updated value at the end.
Now, an example of this would be to receive maybe a list of geo coordinates from a database that we
want to convert into some kind of real address.
We could write a function that takes in a latitude and longitude value and it grabs those and converts
them into some kind of real address.
So say we get a list of different coordinates, 50 of them in total.
We're just defining the function once.
Then we could call the function 50 times.
Only three times here because there's not enough room on the screen.
But we could call it as many times as we wanted to, and we just pass in the different coordinates and
for each time we call it, it takes in those coordinates and spits out a real address.
So this is the power of functions to create little blocks of code that become reusable.
So in this chapter, we're going to be looking at functions in a lot of depth.
And we'll also take a sneak peek at how we could potentially use a function to interact with a web page.
We're also going to be discussing what methods are a little more in this chapter, but not in too much
detail just yet.
Remember, a method is just a function which does something, but there is a small distinction between
functions and methods, and we will discuss that as well.