So now we've got
some key basics about functions,
but we're still not done
because this function is still not ideal,
it's still not perfect.
It works but we have a couple of bad practices in there,
which we don't typically wanna do.
I did explain why we can change adult years
if our code looks like this.
But maybe you get the feeling over the last lecture that
that's a lot to memorize,
and it is.
And sometimes you need this behavior,
which I described,
that you can shadow variables
and that you don't wanna do it in other scenarios
but you can also make life easier for you.
It would be best if this function doesn't even have to know
about the variable name of our outside variable here,
if it could work stand alone.
Because at the moment,
if I ever changed this from adult years to,
let's say total adult years,
I have to change this name down here
and down here,
and also inside of the function.
And again,
because this is a simple script
that's not too difficult.
But you always have to keep in mind
that ultimately you will write more complex code
and then having to make changes,
in dozens of places,
maybe even across multiple JavaScript files,
is very annoying.
So therefore,
it would be better if this function
wouldn't even have to know this external variable name.
And indeed that is something you typically wanna strive for,
that your functions work
without knowing external variable names.
And when it comes to deriving this result here,
this result of this calculation,
what we can do in a function
is we can return the result of a calculation
with the special return keyword.
Now, what does returning a value mean?
It means that this function does now
not just perform a certain operation
or possibly multiple steps.
You can have multiple lines of code in there,
that would be fine.
But that instead when you call that function,
you now get a value from calling that function.
So before this function,
without the return keyword,
didn't return anything.
Calling that function doesn't produce a new value.
Now with the return statement,
calling that function does produce a new value
and calling that function does return this result.
Hence, we can work with that result here
in the line of code where we called this function.
And we can, for example,
store it in a variable again.
In total adult years, for example.
Here we can store the result of calling that function.
Now, of course,
that still means we're using
that variable name down there.
And if we were to change it,
we have to change it in these places down there.
But it's very often the case
that you store functions like this in separate files.
And then you at least
don't have to dive into those files as well,
to make such variable name changes.
And in your main file
where you are using a variable
in different places in your code,
you can always use the search
and replace feature of your IDE of Visual Studio Code,
to quickly change the name in all the places
where it needs to be changed.
So this typically is a better way of doing it
than what we did before.
And in addition
and even more importantly than this,
we don't have to change the name everywhere thing,
we now also made a huge step
towards making our function more reusable,
more flexible.
Before we returned,
we always stored the result
of our calculation in total adult years,
inside of that function.
And that means
that if we ever would want to use
this function in a different place of our code
and store the result in a different variable,
it's just wouldn't work,
as long as we directly referenced total adult years
from inside this function.
Now with the return statement that changed,
now we can use this function anywhere in our code
and in the place where we use it.
So in the place where we call this function,
we can then decide where the result should be stored.
And that is a huge benefit of using the return statement,
instead of directly accessing a variable in the function
and storing our value there,
because now our function is more reusable and more flexible,
even though that's not something we utilize
or need for this simple demo here.
But it is something you typically wanna go for
when you write code.
Because ultimately,
it will make your life as a developer easier.
We also of course,
wanna do that down here,
where I call calculate adult years again.
Because we now need to store that return value,
otherwise that returned value goes into the void
and we won't be able to work with it.
Now, if I save this again,
I again get these alerts with 14 and 27.
Let me close this and reload
to make that a bit easier to read.
And that's a great step forward
and a very important feature,
functions don't have to return the value.
For example, alert here doesn't return a value.
We're not storing this anywhere,
but they often do.
And especially if you have a function
that performs a certain mathematical operation,
it's very common that you do want to return
so that the result of this operation
then can be stored in different variables in your code.