Afrikaans
Akan
Albanian
Amharic
Arabic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
French
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Hungarian
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranî)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Russian
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
All right let's start learning about functions before we do.
We're going to create a new folder where we can put all of our function related files as the basics
directory is getting a little crowded.
I'm actually going to switch the folder that I have open in the Visual Studio code editor.
I'm going to open from the desktop my Jap's boot can't folder directly.
Then along side of basics I'm going to make a new directory and I'm going to call this one functions.
And in here we're going to create our very first file and we can just call this one functions 101.
Dot J.
S.
Excellent.
Let's also go ahead and switch the directory we're running are commands from.
I'm going to use C.D dot dot to go up a folder into the J.S. boot camp directory then forward slash
functions to end up in the functions folder.
And then I'll just use clear to clear the terminal output.
So as I mentioned in the intro functions are little subprograms.
It's a little bit of code we can run over and over again.
And there are three important parts to a function.
Right here we have the function we have the function input we have the actual code to run.
So what are we trying to do.
Are we trying to convert a temperature fetch and user print a message.
And we have the output we're going to ignore the input and the output for the moment and we're just
going to focus on creating our first function a function that runs some code.
We're going to do that right down below.
We're going to use LET.
Like we would if we were to make a string number or boolean let greed user.
That's an I'm going to call this function and I'm going to set it equal to.
And this is where we have to use the function syntax.
So much like the if statement there is a specific set of rules around defining a function we have to
follow those.
The first one is to use the function keyword we literally type out function.
The next is to open and close a set of parentheses.
We're going to talk about what goes in here later.
For now though it's perfectly fine to leave it empty and then we are going to open and close a set of
curly braces and hit enter and there we have it our very first function.
It do anything.
No but it is indeed valid.
So much like our IF statements have a code block where we can put the code we want to run like for example
right inside of here.
Our functions also have a code block that creates its own scope right here.
Anything we want to do when the function executes goes right inside of here for the moment we're just
going to go ahead and use log to print a message.
Later on we'll figure out how to convert a temperature or combine variables call other functions or
do whatever else we need to do in here what are we going to do.
We are just going to print a little message to the screen.
Welcome user excellence.
Now that our function actually does something we can't run the file just yet.
Well we could run the file but the functions never going to execute at this point.
We have to find a function defining a function would be like defining a javascript file.
Yes it exists but you haven't actually done anything with it.
If we want this code to run when we execute this file we have to call it.
So if I were to execute the script node functions 101.
Jay asks What do I get.
I don't get an error I just get nothing.
The program didn't run.
If I do want to run this little sub program I have to call the function right here.
I execute it by referencing it by name.
So greet user.
This alone isn't enough I have to use the function call syntax which is an opening and closing parentheses
after the name.
Now we'll talk about what goes inside of here a bit later too.
For now it's perfectly fine to leave it empty.
If we save the file and rerun things what do we get we get.
Welcome user printing to the screen which is fantastic.
We have created and called our very first function and we can call this function as many times as we
see fit in our program.
So I could paste that line two more times.
And what's going to happen if I run the script.
I'm going to see our welcome user message three times and that's exactly what I get with the most basic
function accomplished.
Let's go ahead and create something a little more practical.
We are going to introduce the input the code which we already have and the output for this one.
Let's say we want a square a number.
So what do we take into the function we take in the number we want to square what does the code do.
The code squares the number.
What's the output.
The output would be the squared number.
So if I passed in 3 the code would multiply three times three and the output would be nice.
Let's go ahead and see that in practice down below.
I'm going to use less to create Square.
I'm going to set it equal to a function so we use the function keyword we open and close our premises
and we open and close our code block using curly braces down below.
We could also go ahead and call this before filling it out square and I'm going to pass in a number.
Now how do we pass a number into a function.
But we do this by providing what's known as an argument so the input in terms of how the vocab works
for javascript is actually known as an argument or arguments plural.
So if you read a blog post or a function you're not going to see thrown around you're going to see argument.
So if we want to pass an argument into square the value to square How do we do that.
Well we provide it right here between the parentheses.
So if I wanted to provide a number I could literally just put a number right here.
I could say three for example or I could reference a variable that stores the number so I could have
a variable up above.
Let Nahm equal three.
And then I could reference that variable instead of just putting 3 right in there.
Both of those have the exact same effect.
And for simplicity's sake I'm just going to stick with passing in three right there.
Let's go ahead and delete this line completely.
And we're back to where we started.
So at this point we are passing in a single argument to square we're passing in the number three.
The question is how do we actually get access to this value inside of the function code.
Well we do that by naming the value right here in the parentheses for the function definition.
So much like we would name a variable.
We go ahead and name the argument right here.
We can call this one name.
Let's go ahead and actually do something with Naam and to make sure the value is getting passed in correctly
right here.
Cancel a dot log.
I'm just going to print out and some will focus on squaring the number in just a second.
If I save the file and rerun it I would expect to see welcome user three times and then I would expect
to see three a single time.
Let's go ahead and rerun the file.
And that is exactly what we get right here.
The argument value is getting passed in.
We are seeing three get passed into the function and when we print it what are we getting.
We're getting the value we are getting three.
Now we can add multiple calls to square as well I could call Square right here with 10 if I wanted to.
And now if we rerun the script the first time the function runs the value will be three.
And the second time the function runs the value will be 10.
Over here we get three we get 10.
So this is how we can set up basic input.
We'll learn more about passing in multiple values later.
And what to do with a value isn't passed in.
For now we're going to stick with a more trivial setup.
So what do we do from here.
We square the number we focus on the code side of things we can do that right here.
I'm going to call this one result equal and all I'm going to do is take the number and multiply it by
using the Ashur sign.
What the number.
So numb times numb.
Now we have this result.
So in the case of three it would be three times three which would be nine in the case of 10.
It would be one hundred ten times ten.
So we had the input arguments we have the code that actually DOES something.
The last piece is the output and the output is also known as the return value.
So just like if you read an article on functions you won't see input.
You'll see arguments you're not going to really see anyone that call the function return value the output
but that is an easy way to think about the three pieces.
It's really called the return value and that's because to return something we have to use the return
statement.
So right here how do we do that.
Well we type out return.
This is a reserved keyword in the language and it can be used a single time only in your functions.
So right here we can choose to return a single value from this function.
What's that value going to be.
It's going to be result.
So we reference it results right here.
Now we have a function that does all three things it takes in some value as the argument and uses some
code to do something meaningful and then it returns a value.
In this case it returns the squared value.
Now if we were to run the program in its current state it wouldn't do anything.
We wouldn't see any output related to our second function right here.
We get three instances a welcome user but we don't get nine or 100 printing.
That's because the return value isn't being used.
So let's figure out how to use it and then we'll move on to a challenge where you'll need to create
a function of your own.
What I'm going to do right here online 16 is create a variable.
So let the value equal.
And then on the right hand side as the thing to store in value I'm going to keep the function call.
So this is the setup we use to call a function with an argument and then store the return value.
So the end result of this is the return value.
And here we're just putting it in a variable.
We can do the same thing down below for square 10.
Let's go ahead and actually do that right here.
Let other value equal.
And then what are we going to do while we're going to get the value back from calling square with the
number 10.
The end result 100.
Now we can actually print both of these to the screen console dot Lague value.
And you'll notice something log is indeed a function.
We're calling it with some disease and we're passing in an argument whatever argument we pass in gets
printed to the console.
So now we know a little bit about this line.
We still don't know what the DOT part is and the.
Up front but we can at least break down this piece right here on the next line.
Counsel not like other value then we're going to do is save the program and see what happens when we
run it.
I'm going to rerun the script and this time around we are getting welcome user three times.
We're then getting the number nine which is the correct result and the number 100 also correct.
So there we have it.
We have a function that has a set of arguments a number.
We've got our code we have our output when we want to call a function we reference it by name with some
parentheses on the end.
In there we can pass in the input we want to provide the function.
And if we want to do something with the return value we can just put the function call over here with
a variable set up beforehand.
Now that we've seen all of this in practice I want you to create a function on your own as the challenge
for this video.
So down below a little challenge area is indeed appropriate for this challenge.
You're going to be creating a function called convert Fahrenheit to Celsius.
And as the name suggests this function is going to take in as the argument the temperature in Fahrenheit
it's then going to run the conversion algorithm and it's going to return the temperature in Celsius.
So after you create the function you're going to call it down below call a couple of times.
And if you want some sample values 32 shall get converted to zero.
And then another good one would be 68 degrees Fahrenheit which should get converted to 20 degrees Celsius.
Then the last thing you're going to do is print the converted values.
So this is very similar to what we just did above.
We defined our function.
We then called the function a couple of times and then we did something with the return value.
That's exactly what you're going to be doing down below to find the function call it do something with
the return value.
If you forget how to convert the temperature Don't worry you can just use the temp conversion project
that you created before you have the algorithm sitting right up top.
If you don't have that file anymore you can always download it.
It's available in the lecture zip for every single lecture.
All right take some time to knock this one out and then go ahead and test your work.
When you run the program you should see 0 and 20 at the bottom.
Assuming you're using the same two input values I set up right here paused the video.
And when you're done click play
How'd that go.
Hopefully you were able to get that done without too much trouble.
I'm going to kick things off by defining the function up above.
I already have the variable name typed out so I'm just going to remove the two slashes and toss let
up front and then going to set this equal to a new function count my function keyword arguments list
and the code to run.
Now there is going to be a single argument for this one.
The name you picked isn't particularly important as long as it's meaningful so maybe you will with temp.
I'm going to go ahead and go with Fahrenheit.
Next up we need to actually do the conversion process.
I have to get the value in Celsius.
We already have that code sitting right over here so I can actually just take that algorithm copy it
and paste it right in here.
Now it's important to remember this algorithm references some variables like Fahrenheit but that's OK
because we have Fahrenheit defined up above.
So we actually don't need to change anything about this line.
What's the next thing we want this function to do.
We want it to return the converted values.
So down below we can wrap up our function by using return.
What are we trying to return.
We're trying to return the temperature in Celsius.
Now we can move on to the second stage which is to actually call this a couple of times.
So right here let Tempel 1 equal and we can go ahead and use our function convert.
And you can see that the auto completion is actually picking up on the function that we created and
right here we can just click that to auto complete it and then we can pass in the value.
I'm going to pass in 32 degrees for the first one.
Let's go ahead and duplicate this line.
I'm going to go ahead and highlight the whole thing.
Copy and paste it down below.
And now we're going to do is define temp 2 for the second call and we'll change the value.
Let's go ahead and change it over to the other sample data I gave you that was sixty eight.
The last thing to do is to print those two values so cancel dot Lague temp 1 and down below that cancel
dialog passing in temp to.
With this in place we can remove those comments and we can go ahead and test our work.
Let's cross our fingers and hope everything works as expected.
Right here I'm going to rerun the script and the only two values we care about are the last two values
the logs that come from the challenge right here we're getting zero for the first one which is correct
and 20 for the second one also.
Looking good.
So there we have it.
This was a quick rundown on the basics of functions.
Now at this point you probably still have a ton of questions for example can I pass on multiple values.
What happens if I call a function and don't pass in the arguments it expects we're going to get to all
of that in this section.
At this point what I want to explore is the basic syntax.
The fact that when we create a function we have our arguments the code and our return value.
We've seen that in a couple of different situations.
Let's continue on to the next video and start exploring some of the details.
I'm excited to get to that.
I'll see you then.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.