02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School
Previous VideoNext Video

Understanding functions and parameters

In this lecture we will dive deeper into functions to really understand how they work. The first step is to understand the difference between variables and functions. While variables are just containers of data, functions are containers of action.

So far we worked with the built in functions but we didn't really dive into what they actually do. In this lecture we'll understand how functions work and discover how parameters work.

Parameters are a fancy name for variables. Not just regular variables but variables that are being sent into a function. You want to send in variables into a function when you want to have customization to inaction. Such as, a running function you would want to tell the function who should run.

Video Transcript

00:00In the last few chapters, we kind of store functions. We don't we don't really know what they are
00:06That's exactly what this chapter is all about in this specific lecture
00:09We're gonna look at functions see how they function until you could function with functions. So let's start functioning
00:18Functions and action to do that. I'm gonna go back into my editor
00:22And what I want to do now is I just want to talk about the most basic basic type of function
00:27and I want to you know, like we just talked about the difference between variables and functions, but
00:36Let's take a deeper look into what is the process of calling a function?
00:41So the first step is we call the action itself, which the action itself is the name of the function. So let's call it that
00:48function name now every function has a name and needs a name for us to be able to call it now there's a difference between
00:56Calling a function and triggering a function. So
00:59Just by saying the name of the function
01:02I'm not really doing anything. It's just like saying the word run versus actually running
01:06So to be able to tell the computer hey take this function execute it make it do what it does
01:11we want to open up a round bracket and a closed RAM bracket and literally this tells the
01:17Computer this tells JavaScript this tells most programming languages. Hey take this function and execute it make it run
01:25Now most default functions just don't have most functions are not empty of parameters
01:31The reason is because usually when you create a function it usually has a little bit of customization
01:42For example, if you wanted to create a function that's called, let's say run
01:46You might want it and send into the function up one parameter
01:50So we might have a function that's called a run and maybe one parameter will be who's gonna run
01:55So who should run we might be the first parameter
01:59So maybe we have here a few people that could run and then we when we run our application literally speaking
02:05We could have used the same function to make a few things run
02:08Now what I'm saying that when you look at one of the built-in functions, for example is the alert
02:13So the alert that we saw in chapter 2 we said we could send here some message
02:18To the user and that message the user is gonna go through to the user and display now
02:24We're using the same alert
02:25but we could create hundreds of different messages using the same function because what the
02:29Alert function does is opens up an alert window and puts inside whatever content we want to send to that
02:36Function sets a perfect example of a built-in function that uses that one parameter. Now. These are called parameters
02:43Now by the way, I'm putting a semicolon again, we talked about it, but just as a reminder
02:48semicolons help us tell the computer that one action has been completed and we really want to always put a semicolon
02:55Because some programming languages it's a mandatory in them in JavaScript
02:59It's a little bit more forgiving, but it'll it's a very important thing to do
03:03Although it doesn't do anything really in JavaScript. It has a really important
03:08Rule for you and programming languages in general to be always be aware
03:12Where did something end because you could very easily break a line here just for visuals and you might get confused and beyond that
03:19But just by putting a semicolon you're helping the browser you're helping JavaScript
03:23Render a little bit quicker because it knows where one action is done
03:26It doesn't to figure it out if an action just completed or not
03:30So so that's about that. Let's look now about what happens if we need to send more than one parameter
03:36What if we have our run function and we want to have?
03:39Who should run and I say had there a house should run up there
03:43So let me fix that who should run and maybe we also want to say how fast
03:49All right, so this is an example of a theoretical function and we can definitely see that in action when we look at the prompt and
03:58In the prompt if you recall we had your the question
04:02The question was our first parameter and our second parameter was the default value
04:08So that was our two parameters. So this the function prompt enabled us to basically select
04:13To send in two pieces of information and the way we separated them
04:17We put them inside of those round brackets between the opening and closing wrap brackets just like you did with one item
04:23but instead we separated between them with a comma and
04:44And you're right
04:44If you wanted to add multiple items more than two you could if the function accepts them you could put your another something
04:50parama 3
04:52parama 4 and
04:55So forth and so forth. Okay, this is just as a so forth
04:58This is not saying that you would actually put inside of codes
05:00This is a just so you know that you could put a more more more more if you wanted to
05:05So that isn't that is the the examples of all the different basic
05:10Types of ways you could call a function you could call a function of sending no parameters into it
05:15You could send one parameter into it and you can send multiple
05:18Parameters into it and the way you know how many parameters you put into it is by looking into the documentation of the language
05:25JavaScript has a lot of built-in functions that are ready to be used and also
05:30Once you add in third party plugins items codes versus jQuery
05:34They come with a bundle of functions that you could use same is true with the tween max
05:40So we're gonna because we're focused on tween max in our training and looking at the basics of animation
05:45Let's call tween max and let's do one thing one call and and wrap up this video
05:50What I want to do is I really want all these three items to be invisible
05:55But I don't want to do it with CSS because if I do it in CSS
05:59What that means is is that if a user doesn't have JavaScript or a search engine is indexing our site
06:06You will see that there's nothing there, which is never a good thing
06:09So what we want to do is only if a user has JavaScript and it's enabled to set it by default to be invisible and then
06:17Make it visible. So let's do that
06:19So let's gonna we're gonna go into the tween max and we're gonna call the set parameter and we're gonna first say
06:24What do we want to set so I'm gonna look inside of here
06:27What I want to do is I want to set every h2 in my case
06:30There's only one h2
06:31But I want to make sure that every h2 is invisible and the next thing is I'm gonna add here an object
06:36And we're gonna talk about what objects are
06:38later on but what I want to do is I want to put here a property and
06:43And value in this case, I want to use the opacity and I want to put the value of zero here
06:50Now don't worry too much about the construction of this object, but this is an object
06:55We're gonna look at it and talk about it once we're in the chapter about objects
06:58But just by the way, this is an object. We're sending an object as a second parameter
07:02So literally we're looking at this. This is our first parameter. This is our second parameter
07:07You don't have to worry too much about what objects are at this stage
07:09But we're gonna definitely dig into it a little bit later on
07:12But what we're doing here is we're calling a set function that belongs to tween max now
07:17Notice the little difference between our run that we had here, which was a global function the alert
07:22Which was a global function versus our set function, which belongs to a specific
07:28object or belongs to a specific
07:31library of assets of tween max
07:33So basically we're asking from the library of tween max to use the function set and with it
07:38We're sending two parameters the h2 and whatever we want to do with it in this case
07:42We want to set the opacity to zero
07:44So if I save this and I go back into my browser and I click on refresh
07:49We're gonna see that now my headline has become had become has his is invisible now
07:55This is it for this lesson and in the next lesson
07:57We're gonna continue talking about we're gonna take a small break from talking about
08:01Functions and we're gonna stick into with the set function because I want to dedicate some time talking about selectors
08:07Which is a CSS thing if you don't know a lot of CSS next lesson is gonna be very critical and in it
08:13We're gonna talk about how do you select things?
08:15What are the rules of selections in CSS which those are the rules that we're sending as a string to the first parameter of the set
08:22And we've seen this also when we were working with the two and the from functions
08:26Which we're gonna see again in this lesson. So we'll see you in next lesson when we cover them

Ready to Level Up Your Skills?

Join thousands of learners on 02GEEK and start your journey to becoming a coding expert today!

Enroll Now for Free!