- 720p
- 540p
- 360p
- 0.50x
- 0.75x
- 1.00x
- 1.25x
- 1.50x
- 1.75x
- 2.00x
We hope you enjoyed this lesson.
Cool lesson, huh? Share it with your friends
About this lesson
Implementing and invoking blocks of code with functions.
Exercise files
Download this lesson’s related exercise files.
Functions59.1 KB Functions - Solution
59.1 KB
Quick reference
Functions
A function is a block of code meant to perform a specific task that you can call from anywhere in your program.
When to use
Use functions whenever you need to create a block of code that may be re-used whenever needed.
Instructions
To create a function:
function myFunction(parameter) {
// do something
}
To invoke a function:
myFunction();
You can pass parameters into your function by typing them when you invoke the function.
myFunction(parameter);
Functions only run when they are invoked. They can be run at any time, or many times.
Hints & tips
- Functions are blocks of code that you invoke when needed
- You can pass parameters to functions and get things returned
- 00:00 In this video I want to talk about functions, and
- 00:05 we are now well into our sort of intermediate JavaScript.
- 00:10 And functions, they're a little bit more complicated than what we've learned so
- 00:14 far, but they aren't too bad.
- 00:15 So, a function is basically a block of code
- 00:18 that is meant to perform a specific task.
- 00:21 So a function sits there until something else calls it, or we call it invoking it.
- 00:26 So some programming languages call these methods.
- 00:29 In Ruby, they call functions methods.
- 00:31 In JavaScript, they call them functions.
- 00:33 Functions, methods, they're basically the same thing.
- 00:35 So if you're familiar with methods in another programming language,
- 00:38 it's just a function in JavaScript.
- 00:39 So, let's go ahead and just build one here.
- 00:41 Jump right in and we start by calling the function key word.
- 00:45 And that's just function and then we give it some name.
- 00:49 Just call it someFunction for now and then, we put these parentheses
- 00:54 right next to it and we'll see what those are for in just a minute.
- 00:56 Then, do these curly braces again.
- 01:00 And everything between the curly braces, that's our stuff.
- 01:02 So, do something, right?
- 01:05 But everything inside here, that's the actual function.
- 01:08 That's the code that's going to be executed whenever we call this function,
- 01:11 whenever we invoke the function.
- 01:13 So, these parentheses are important, and
- 01:15 we can actually pass stuff into our function from the outside world.
- 01:19 So, if we said myNumber1 and then myNumber2.
- 01:26 Now, we can pass in things into this function and
- 01:29 do something with them inside.
- 01:31 So we can take, for instance, return myNumber 1 + myNumber 2,
- 01:38 and return I'm gonna say, functions return things.
- 01:44 You know, we're gonna run the code and
- 01:45 we're gonna return the output back to the real world.
- 01:48 So myNumber1 and myNumber2, those are called parameters, and
- 01:52 like I said we can pass things in.
- 01:54 So lets play around with this.
- 01:55 To invoke a function or call a function, we just type in the name of the function,
- 02:00 someFunction, and then parentheses.
- 02:03 And that'll do it.
- 02:04 That invokes the function.
- 02:05 Now if we wanna actually pass parameters into the function, we do it right here.
- 02:10 So I might go 1 and 4.
- 02:12 Now this is gonna pass 1 into myNumber1, and it's gonna pass 4 into myNumber2.
- 02:18 So if we run this, actually let's go document.write
- 02:27 Pass this in.
- 02:28 So we're passing in 1 and 4, and we're adding 1 and 4 so we should get 5.
- 02:33 We got absolutely nothing though, I misspelled function.
- 02:36 My spelling is terrible.
- 02:38 Boom. There we go, five.
- 02:40 So we can flat out call a function,
- 02:42 we can invoke a function like we just did right here, just by calling it.
- 02:45 Or, we can assign the output of a function to a variable, and
- 02:48 then do something with the variable.
- 02:50 So, we can go var myVariable equals someFunction and
- 02:57 then let's pass it 1 and 5.
- 03:02 And then, down here we can just output myVariable.
- 03:08 See what we're doing here?
- 03:09 We're saying assign the output of some function into this variable, and
- 03:13 then just print mVariable to the screen.
- 03:15 So if we save this, we should should get six.
- 03:17 Okay. Reload. Because one and five is six.
- 03:19 So that's pretty cool.
- 03:20 So, why should we use functions?
- 03:22 Well, they allow us to create little programs and
- 03:24 then reuse them whenever we want to.
- 03:26 So, once it's written, you can call a function whenever you want, over and
- 03:30 over again, as often as you want whenever.
- 03:32 You just invoke it with its name in the parentheses like we just did.
- 03:36 So those are functions.
- 03:37 They're really one of the fundamental building blocks of any programming
- 03:40 language, but I think you'll agree that they're not that complicated.
- 03:43 And that's all for this video.
- 03:44 In the next video, I want to talk about if-then statements.
Lesson notes are only available for subscribers.