[MUSIC]
>> Hello. My name is Nitya Narasimhan.
I'm a Senior Cloud Advocate at Microsoft.
Today, I want to talk about five things that are on
JavaScript functions that you can explore with code.
Let's get started.
First, what's a function?
A function is nothing more than a block of executable code.
By making it into a function,
you now have this become a reusable execution block
which you can call from anywhere,
which makes the code easier to read and easier to maintain.
How do you actually define a function?
The easiest way in JavaScript is by using the function keyword,
followed by a very simple function names.
In this case, I'm just going to call it printHello,
followed by simple brackets,
open curly braces, close curly braces, and that's it.
The execution of the task statements
that you're thinking of, that would go in here.
Now in our simplest form,
for now I'm not going to define anything.
Is this a function?
Let's check it out. I'm going to
log this function to see what it prints.
Remember at this point, it's actually
printing the definition of that,
so it's going to look at what type.
Oh, I actually wanted to print what type this was.
Let's save that. You can see that this is of type function.
Just a simple, simple thing that we did.
The simple definition is a legitimate function,
it just doesn't do anything.
In order for it to make it more interesting,
let's actually add an execution statement,
so I'm going to do the usual thing of just logging
something to the console so we can see it.
So we're going to do the ubiquitous hello world.
If I execute this now,
do you expect to see hello world?
I hope you said no, because we have defined it,
but we haven't invoked it.
Definition is just as saying,
this is what this function will do when it's called.
Invoking it is actually calling it.
Now we can look at function
and for the invocation, how do we invoke it?
Simple; use the name of the function,
open, close brackets, and then semicolon.
That's the important part.
That makes it an execution statement.
Now you see hello world,
and the reason one easy way that
you can think about remembering to put
these brackets is that think that
this function name and you execute or invoke it,
you want to pass in inputs and you want to get back a result,
you need some way to pass in inputs,
that's what those brackets are for.
They're like a container through which you pass in
arguments that get mapped onto the parameters of the function,
we'll talk about that in just a second,
but until then let's first look through naming conventions.
It turns out you can use any of
naming conventions as for the function name over here.
You can use anything for functioning with alphabets,
numbers, dollar sign, and underscore,
but no other special characters.
In here, if I try to make this print dollar,
hello, it will work.
I'll change it in three places
because I don't want it to scream at me,
and if you look at this, that works,
and you can take my word for
it that it'll work for underscore as well,
but let's try a special character.
What if we try to say make it print hello?
Just using the character itself
causes the editor to tell you this doesn't work.
You can see this little red squiggly lines.
That's all you need to know about naming.
You can use numbers,
alphabets, underscore, and dollar signs.
Everything else, not a valid functioning.
Great, we learned about functionings,
now we come to the interesting part; parameters.
If I look back at this function, printHello,
I actually say I want to execute something and I want to provide
it data that makes it execution a little bit more personal.
In this case rather than say hello world,
I actually want it to say my name. How do I do that?
I can do that by first making sure
that there's a variable called name that has
this data that should be replaced by
the actual name and then using parameters.
Think of parameters almost like
placeholder variables through which
you can pass data into the function,
and at the time of execution,
the placeholder gets replaced with the actual data.
How can I test this? I'm going to stop printing the typeof,
we already know this works. How do I invoke this?
When I have parameter defined,
I can now go in and pass the value for that parameter in here.
Let's say I make this Microsoft,
now you can see it prints hello Microsoft.
This is simple, this is awesome,
so we can also play with it just a bit.
Now that I've defined a name,
what happens if I don't give it a value?
What do you think is going to happen? Let's try it up.
What this means here is I've defined a function and I've said,
I expect a parameter name,
and I invoke it without actually
providing any arguments, it will still run.
It'll just come up undefined,
that's because JavaScript doesn't actually
validate to see whether you're using
the right number or type of arguments,
it'll just execute it.
What that means is I can also do the equal and I'll say Microsoft,
and then put in a comma and then put it in a second thing,
and what this will do is it'll effectively just ignore the second.
I'll just use the first thing as the argument for name.
So far so good? Great, all right.
Now let's think now about return statement.
Parameters are able to pass input,
but what if I want to get output out?
Return statements have to value propositions.
One is they tell you where the exit points and functions are.
It's like saying when I see a return,
I know that I'm going to stop executing those statements
inside that function block and get back to the caller,
but it also as part of the exit lets me return data.
Now the first thing I'm going to do is let's assign
a result to printHello and we'll use
the Microsoft examples, so it's cool.
In this case, all I've done is said,
"Hey, if this function return the result,
it's going to get assigned to that variable,"
and let's print this variable out and see what happens.
Note that right now I'm not actually returning
anything in the function definition, so what happens?
It comes out undefined, totally legit.
In this case, there was no return statement,
nothing was returned as a result,
the function exited gracefully
on the last statement which is the "Print",
"Return" and said, "Hey there was result undefined."
Great. If I were however to put a return statement,
I can just hit "Return".
I haven't return the value,
it'll still be undefined because I
haven't actually returned anything.
It's just, I still like putting return statements
because it's not very clear where your function exits.
If I were to return an actual value in here,
for the sake of it, let's say I return the name
and then I add another name to it.
Name hello, another string to it.
Now when I print this out,
you'll see that that string,
what have I created was return as the return result.
We've learned a bunch of things today with just a simple demo.
First we understand what functions are,
we understand how to define them,
we understand how to invoke them,
we understand what parameters are
and how to pass some data through parameters,
and what happens if you don't provide
the right type or count of arguments.
We also understood what return statements do and how
to use those to pass out data back to our calling function,
to our calling code.
I hope that was useful,
see you at the next demo.