First look at Functions
In the heart of every programing language there are two main things: variables and functions. Variables help us store information and functions help us create actions. In this lecture we will see and understand the basics of functions. Don't worry if this is hard (it should be hard). In a few chapters we will go back to functions and clear things up.
Video Transcript
00:00All right, we're still in chapter one.
00:01We're still on our way to our first animation.
00:05So far, we've learned a lot.
00:06We've learned about JavaScript and functions.
00:09We've even created one on our own.
00:11I wanted to just reinforce what we've learned so far.
00:15And to do that, what I really want to do
00:17is I want to comment out all these lines of code.
00:21Now, to do that, I'm going to use the slash and star,
00:25and then go to the end of it and type star and then slash,
00:29which will enable me to comment out a large block of code.
00:33Now, I'm going to go into my Firefox.
00:36And you can work with your own browser.
00:39And you're going to need to have Firebug.
00:41So make sure that you install Firebug.
00:43Just do a Google search for Firebug
00:45and install it on your browser, unless you already
00:48are working with a different browser
00:49and have a debugger and have access to a console.
00:54And that's what I want you to go to.
00:56If you just install it and you went on to Google
00:58and search for Firebug, I believe
01:00that I created even a short URL, which is smarturlit slash
01:08fox3.
01:10If you go to fox3, you'll get the web developer.
01:14Do install it if you're running with Firefox.
01:16It's going to be helpful throughout the course.
01:18And also smarturlit fox4, which will take you to Firebug.
01:25And install it.
01:25Once you install it and it's up and it's running,
01:28go back into our page, right click anywhere,
01:32and then select Inspect with Firefox
01:35just to open up Firefox.
01:36And once you do that, you'll have your console.
01:39And your console is a really great place
01:41because you could easily test things out
01:43by just typing some JavaScript right into the page
01:47and just getting it reply back to you.
01:49So it's a very, very great way to practice and see things
01:53in action.
01:54So for example, if we would take that,
01:56like we had our document, it actually
01:58will give us extra information.
02:02Document, and we would actually be
02:05able to see that document right away inside of our console.
02:07So it's very, very handy, very, very useful.
02:10So the next thing I want to talk about,
02:12let's jump right into our editor again.
02:15And I want to recap by doing it all over.
02:17So I'm going to go ahead and create a new function.
02:20And I'm going to create a function, function,
02:23by giving the name function, giving it
02:26a name such as, let's say we'll call this Talk, or TalkBack.
02:33Let's call it TalkBack.
02:34Now, the names don't really matter.
02:36I open and I close a round bracket.
02:39I then open a curly bracket and close a curly bracket.
02:43And all the content of the function
02:45will be inside of our TalkBack function.
02:48Now, what I want my TalkBack function to be able to do
02:51is I want it to be able to accept a parameter
02:55and to echo it out and to TalkBack, basically.
02:59So to do that, I'm going to put here basically a value.
03:02But notice how I'm not putting here a string.
03:04I'm putting here a variable, which is the first time we've
03:06ever seen a variable.
03:09And I'm going to go ahead and I'm going to create an alert.
03:13And I'm going to send it into the alert.
03:15And deliberately, we're making something a little bit
03:17complicated.
03:18But over the time, and as we progress,
03:20it's going to get easier and easier.
03:22So literally, what I'm going to do is I'm going to say,
03:25I'm going to send you a string.
03:26No, you didn't say, oh, great.
03:32This is great, because it's going
03:33to show us a problem that we have here.
03:35And we'll solve it.
03:36So one is, notice how we're using a single quote
03:39and a single quote to close.
03:41But we're getting this weird content.
03:43And because we're using a single quote, also for it
03:46didn't.
03:47So for us to be able to tell the browser, hey,
03:49this isn't actually script.
03:53It's actually part of my copy.
03:55I'm just going to put your slash.
03:58Now, by putting a slash, it's basically
03:59telling the computer this isn't a code thing.
04:03This is part of the text.
04:05And what I'm literally doing is I'm taking a copy saying,
04:07no, you didn't say space.
04:10And then I'm putting here a plus value
04:12to add to it whatever value is sent to TalkBack.
04:16And I'm going to close my bracket.
04:18I'm going to save it ahead.
04:20And let's go back into our browser.
04:23And I'm going to go right into my console area.
04:25And I'm just going to TalkBack.
04:27I'm going to type your talk back.
04:31And I'm going to type your, oops, I didn't write it
04:33correctly, talk back.
04:37And I'll send some copy.
04:40Hello there.
04:44And I'm going to send it in by typing enter.
04:46So first of all, I see that the function TalkBack
04:49is not defined.
04:50Oh, and that is because we didn't refresh the page.
04:52So let me refresh the page because the TalkBack function
04:55was created only after the page was loaded.
04:58So we see that TalkBack is now noticeable here, TalkBack.
05:06And I'm going to send to it, again, TalkBack.
05:09And I'm going to send to it, hello there, close my bracket.
05:13And there we go.
05:14No, you didn't say hello there.
05:16All right, so our function is TalkBackingToUs,
05:20which is kind of wicked.
05:21Now, obviously, it could become, I don't know if obviously,
05:23it could become very annoying if every time you
05:26want to test something out, it pops up on your alert box.
05:30To make sure that it doesn't go to your alert box,
05:32what you could do is instead of calling the function alert,
05:35I could call the console.log.
05:43Now, the console.log basically is our console here
05:48in JavaScript, or in our browser, and it's
05:51going to log our information here.
05:53Instead of having it display and basically
05:56visible for the user, this is a little bit less
05:58visible to the user.
05:59So if I click here, refresh, and I try to TalkBack again,
06:04and I say, yo.
06:07And I close it, and I send it.
06:09So first of all, we'll see here that TalkBack
06:11is not defined because we did a lowercase b by mistake,
06:15talking about how important this stuff is
06:17and how you learned, right?
06:18So first of all, names, letters are critical.
06:21So now it's working.
06:22But again, I want to just talk about that for a second.
06:24If we're ready, that came back.
06:26The name of your function, it's literally what you write there.
06:30So if you have an uppercase B, you
06:31have to call it always with an uppercase B.
06:35If you don't, then it won't work.
06:37Now, another thing is that every function must be unique.
06:40So you can't have a function called TalkBack
06:43and create another function called TalkBack.
06:45Because if you do that, then whenever
06:47you call for the TalkBack, you wouldn't know how to get there.
06:50Beyond that, you can't call functions that already exist.
06:53So you can't create a function called
06:55alert because the alert function exists.
06:57What you would do is you would make it impossible
06:59to reach your alert if you have two functions that
07:02are called alert.
07:03All right, so that is it for our TalkBack.
07:06What I want you to do is take a step back and play around
07:09with this.
07:09A little bit of homework before you move on
07:11to the next lecture is I want you
07:13to create a function that talks to itself.
07:16How about create another TalkBack and call it TalkBack2
07:20and have one function talk to another function, just once?
07:24All right, we're not going to do that ourselves,
07:25but you're more than welcome to send it or put it
07:28in the comments.
07:28We would love to see your creation and just
07:31create a function that's talking to another function.
07:33Isn't that fun?
07:35In this lecture, we learned also about variables.
07:37We didn't dig too deep into it, but we will
07:40as we progress onwards.
07:42So now we've seen functions, we've seen variables,
07:45and we've seen how to work with built-in functions.
07:48In the next lecture, we're going to be ready to create
07:51our first animation because now we have all the skills needed
07:56to create our first, first, first, first animation.
07:59So we'll see you in the next lecture
08:00where we're going to wrap up this course chapter, that is,
08:06and we'll be able to see the end of the chapter,
08:09that animation that we promised you.
08:11So let's jump right in there and let's see it in action.
