Jonas: Welcome back
to one of the most fundamental concepts in Javascript,
and that is functions.
So, if we have a piece of code
that we want to run a lot of times,
we can put that into a function
instead of writing it over and over again, okay?
So functions are like containers
that hold some lines of code
and we can pass arguments into them
and they can then return us our result.
And instead of talking more about this,
let me actually show you
a very small and very simple example.
So this is how we write functions.
We simply write function
and then we give the function a name.
So just like a variable,
we have to give a function also a name.
And in this case what we want to do
is to calculate an age.
So calculate age.
And as I mentioned before,
we can pass something called arguments into a function.
And so we open up parenthesis,
and then we say the arguments that we want to pass in.
And what we want to pass is called birth year, okay?
And so this will kind of be like
a variable inside of the function, alright?
But more on that a bit later.
So, we say function, then the name of the function,
then the argument that we want to pass into the function,
and then finally the curly braces
in order to write the function block.
So all the code that's in here will be the function block.
Now we can of course pass zero arguments into this
or we can also pass multiple.
So we could pass more like x and y,
separated by commas like this,
but in this case, we only want one argument
which is the birth year, alright?
So we pass in the birth year,
and then we can return something from the function.
So in this case, what we want to return
is the age of the person.
So we can calculate that by doing
2018 minus the year in which the person was born.
And so we can now basically use
this variable inside of the function, okay?
So this birth year parameter that we defined here
in this function definition up here.
So I like to think of a function as
like a machine that receives some input,
then it does some stuff with it,
and finally returns something else.
So something you created from the data that we put in.
So that is our function definition.
Now let's actually go ahead and use this function, okay?
So to call this.
So we can say now calculate age,
then we open the parentheses,
and now's the time where we pass in that argument.
So let's say that we want to pass in 1990,
and so like this we will call the function.
And the function will then take this value,
this 1990, as the birth year.
So this is now 1990,
and then we will do this calculation,
2018 minus that birth year,
so in this case, minus 1990,
and will then return a result, okay?
So that result we have to store it somewhere.
So let's create a new variable for that age John.
And so in age John here will be stored
the result of calling this calculate age function.
Alright, so test that out and log age John to console.
And here we go.
So, 28 years old.
And that makes total sense because it's 2018 minus 1990.
So, again, we define this parameter here or this argument
and you will hear me saying these two words.
And they're not exactly the same, okay?
But for now let's just keep it simple
and assume that arguments and parameters
are the same thing, okay?
So we define this argument here of birth year,
and so when we call the function calculate age down here,
and we pass in the value,
then this birth year variable here or argument
will be assigned that value that we pass in.
So, 1990 in this case.
And then inside the function we can use that.
Alright, so let's now calculate some more ages.
So let me copy this here.
And say we want age Mike.
Let's say 1948.
And age, let's say Jane now,
so a new person here. 1969.
Alright, and now we can log all of these to the console.
And so let's see.
So 28, 70, and 49.
So now you start seeing the power of the function
because instead of having to write
that same code here over and over again,
we can simply always call the same function.
Now in this case,
it would probably not be so problematic to write this code.
It's just one line,
but of course functions can be huge.
They can be much bigger than this,
and so then it of course becomes a lot more useful.
And so functions are very, very important
for one of the most programming principles,
which is called don't repeat yourself, or DRY.
Okay, so the DRY principle is something
really important in programming as general.
So you should never repeat yourself.
Never write the same code,
and instead put it into a function.
And so that's why we have functions
in Javascript and in all programming languages in general.
So, that was just a very simple example
of a very, very simple function.
So let's now add some more complexity to it
and let's create a new function,
which will calculate the years until retirement.
So based on our birth year,
we will be able to calculate
how many years we have left until we retire.
So that's a little bit more complex,
and so it's a good use case for a function.
So years until retirement.
And now let's pass in the birth year
just as before and also the first name.
So as I mentioned,
we can pass multiple things into a function,
not just one argument.
We can pass two arguments or zero or three, four,
and as many as we want, really.
So how are we going to calculate the years until retirement?
Well first of all, we need to calculate the age.
And so now let's actually call the other function
that we already coded before.
And so what this means is that
functions can actually also call other functions, alright?
And so let's calculate the age
based on the birth year again, okay?
And maybe let's give it here another name
so that it's not so confusing.
So I'm just calling this year now here, okay?
And if I change it up here, then
of course I have to also have to change it here, alright?
And so when we now call this function,
this years until retirement,
then we pass in an argument as the year, let's say 1990,
and that will then be passed into calculate age,
so 1990 again,
and that in turn will be passed
into the calculate age function here
and can be used to return the age, alright?
And so that age will then be stored in this variable,
and then we can calculate the retirement.
So the retirement is
the age at which we retire minus our current age.
So let's say that we retire at 65 years,
so that's kind of a standard, I guess at this time,
and then minus the age.
And that gives us how many years
do we have left until retirement?
Okay, now functions don't always have to return something.
So up here we returned something
so that we can then use the value later.
But in this case we don't want to return anything.
We can simply log something to the console.
So we wanna say first name retires in
and then retirement plus years.
Okay, and so that's our function.
And let's now call it.
So, years until retirement.
Let's again use the year of 1990,
and then the first name is John.
Okay, and this time we are not saving the result anywhere
because there is not really a result.
So that's what I just mentioned before.
There is no return here in this function.
And so there's no result,
and so we don't have to store any result anywhere.
So, if we just now call this function like this,
then all that's gonna happen is
that these calculations will be done,
and then we will log something to the console.
So, let's check that out.
And so John retires in 37 years.
So let's quickly recap what happened here.
We passed in two arguments.
1990, which is now called year inside of this function,
and we also passed in this string called John,
which will be called first name inside of that function.
And so in our console dot log,
we use that first name here, which is John,
and then also the year is used to calculate the age.
And that age calculation is, as I mentioned before,
done using the calculate age function
that we coded up before up here, okay?
So functions can call other functions.
They also do not have to return anything.
It's quite usual that they do,
but of course they don't always have to, alright?
So, let's now do this with some other years.
And we can actually use the same as before.
And probably it's easier to just write it out.
So, 1969, and here we have Mike,
and then also Jane.
And so, now you have
John retires 37, minus five, and 16,
and so hopefully by now you see even better
how handy it is to have all this code here in one function.
'Cause imagine how repetitive it would be
to have to write this for each of these persons, right?
Instead, we just put all of this code in one function
and therefore keep our code dry.
So, DRY remember, means don't repeat yourself, okay?
So just one thing that I see here is
that we have minus five years here,
and what that means is that,
this person Mike, in this case, is already retired, okay?
So let's add a simple if else statement here.
So if, the retirement is greater than 0,
well in this case, it means that we still have
some years left to retire.
And then we want to log this to the console.
But else, so basically if the retirement is zero or less,
It means that people are already retired.
So let's log that to the console
First name is already retired.
So, take that out,
and so Mike is already retired it says now
instead of what it had before.
Alright, great.
So, these are the fundamentals of functions
which again, are one of the most fundamentals principles
in all programming and of course,
also in Javascript, alright?
So, what you have to keep in mind here,
just to recap, is that
we can pass in arguments into a function,
like this and like this, like we see here.
Then we can return results from a function
using the return keyword and we can then
save these results into a variables like we did here.
Also functions can call other functions
like we did here.
And they do not need to return something.
So it's not mandatory, alright?
So that's the fundamentals of functions.
Next up, let's talk about something called
function expressions and function declarations.