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

What Are Functions?

Taking the first stab at explaining what functions are.

Video Transcript

00:01Splash. Welcome to the Deepwater. So, um, don't worry. Well, just a little bit. But
00:09don't worry that the next topic is a little bit, it's a little bit harder to grasp and
00:15get into. Variables are a little bit easier, and all the stuff that we've done so far
00:19is really stuff that you've been doing in your life anyways. You haven't done it on
00:23the computer, but you've done calculations, you've compared between things. And we're
00:29stepping now into this world which is called functions. And functions really,
00:33before we even get into the code itself, and this is kind of a new intro, because I
00:36really felt like our old intro just didn't do justice to the topic. So what
00:42are functions? And before we get what functions are, let's step back again and
00:46ask what are variables? And to do that, one great way of always trying to
00:51understand stuff in programming is stepping back and trying to look into
00:54your real world, what's going on in your world, and trying to compare between
00:58what happens in the computer and what happens in your world. Because programming
01:02is really derived from ideas of people, and people came up with those ideas
01:07from things that were around them. So when you say variables, really we're
01:11talking about things. We've seen the implementation of it, but we're really
01:15talking about things if it was the number five, or the number ten, or just
01:20a glass. You, me, this rabbit, his glasses, his ears, his mouth, his eyes, all
01:27these stuff are things. And these things are variables from our perspective.
01:31They are things. Anything that's a thing is a variable, because it could have, well,
01:35it is something we haven't learned about a constant. It's there, it's a, you know,
01:40this rabbit is that rabbit, it can't be another one, because he's already there,
01:43right? Contrary to when it's inside of a variable, then we say that I could be a
01:47lot of different values. But variables are things. There are tangible stuff. It's
01:52like a hand. It's like a room. It's like any, basically anything that you could
01:57grab, or anything you could quantify. Now, functions on the other hand are very,
02:03very tricky to people sometimes to understand, but functions are really
02:08contrary to things that you could grab. They're not tangible things, they're
02:12actions. A function is an action such as walking, breathing, eating, talking,
02:18blinking, chewing, drinking, anything that's an action is a function. And when we
02:25connect that to computers, and we connect it to programming, functions are really
02:29just the same thing. They don't store information, but they create actions, they
02:34create things, they do commands. And there's a lot of different types of
02:37functions, and in the next hour, we're really going to delve into all these
02:40different types. And we already met, and very, very shallowly, we met the
02:45trace, and we've seen the syntax itself, the syntax of the of that function, and
02:49that syntax doesn't really change. So flash has a lot of pre built functions, a
02:55lot of pre built functions that could do things, and we could take advantage of
02:59it. At the same time, we want to we're in this course, we're going to learn
03:02also about how to we take advantage of things that flash has to offer to us,
03:05how do we call functions, but we'll also learn how to create our own
03:09functions. And by creating our own functions, we could then execute them
03:13and call them on our own as well. So there's two different tiers, or there's
03:17the calling functions that already exist, such as trace, do we know what it
03:21actually does know, all we know is when we call trace, and we put things into
03:25it, it will output into our output panel. And same thing goes with walking,
03:29do we know the operations of our body and what it does when it walks? No, do
03:33we know what our body really does when it's breathing, we might, but it's
03:37not relevant. Because when we say breathe, we look at it in a holistic
03:41way. And yeah, there's it's a very complex thing. But for us, it's only
03:45breathing. Eating is very simple, because we just called it eat. And for
03:49some reason, that became simple. Trace is the same thing, it became simple,
03:53because we don't care what happens in the background. The trace for us, all
03:57it really is, is a function that enables us to communicate with that
04:01output panel and send to it things. How it does it is beyond our scope, and
04:05it doesn't really matter to us. Now, that's really what you need to get
04:09between the obviously very important to differentiate between variables and
04:13functions. Now, I wanted to put in another few terminologies that we're
04:17going to be delving and talking about them quite a lot into the future as
04:20well. And it's not important to memorize the ideas more than to understand
04:27them. Some programming languages have two different types of functions.
04:34There's a function and there's a procedure. And that's true in
04:38mathematics. Now, I wanted to explain the difference between them not
04:41because it's critically important to programming, but it is important to
04:45understanding the world that you're in. Functions contrary to procedures,
04:53you sent it something or you don't sentence something and then return
04:56something to you. What does that mean? An example could be you walk in the
05:02street and you see someone so you say hello to them. And they say hello
05:07back. So when you say hello to them, they say hello back, they return to
05:11you a gesture. Now, obviously, in a more complex an hour, maybe would be
05:15walk into the store, if you pay them money, then they'll give you a
05:19lottery ticket. If you pay them money, then they'll give you a cake,
05:24right? Obviously random. So these are functions. These are these are
05:28actions. My action led to a reaction. So a function basically reacts to
05:33our action, you could say, now in programming, a function could only do
05:38one thing, you can't ask a function, it could return to you only one thing
05:41more accurately, it can't return to you many things. And we'll look into
05:44that later on. But basically, when we call a function, we're basically, we
05:49might give it something or might not. And then it will return to us
05:51information. We might not give it anything. We might ask a certain
05:55object, what time is it? We didn't send it anything. We just asked
05:59what time it is. And it'll tell us what time it is just as much as
06:01we'd walked a person asking what the time is. They would tell us
06:04what the time is, and they wouldn't expect us to give them
06:06anything, or expect anything from us to do it. Now, a procedure on
06:11the other hand is a little bit different. A procedure doesn't
06:13return anything to you. A good way to explain a procedure. And I'm
06:18going to first explain a procedure in just from our world. A procedure
06:22is just as much as, I would say, trying to think of something
06:28simple. A procedure would probably be just like a doctor office
06:32procedure. You go to the office, the doctor does something to you,
06:36you leave. But he doesn't give you a gift when you leave. So
06:41for example, let's say I'm going to go to a laser correction,
06:44and I'm going to fix my eyes. So I won't need glasses anymore.
06:48And now our rabbit doesn't need any glasses anymore. And he
06:51leaves the office. He just went through a procedure. He
06:54didn't go through a function. Why didn't he go through a
06:56function? Because he didn't get anything for it. Yes, he got
06:59better vision. But he didn't get, it's not like that he went to
07:03a store, paid some money, and then he got apples, and then
07:06walked away happily ever after with his apples. Contrary to
07:10that, if I went into a store and I bought glasses, and I
07:13got those glasses after that, that would be probably a
07:15function. So if I would go into a procedure, they might take
07:18away from me saying that's fine. But if they don't give
07:21me something in return, then it doesn't count. All right,
07:24so that's really the biggest difference, you know, a
07:27procedure and a function. But I would add into that, that it
07:31doesn't really, really, really matter. Don't worry about it too
07:34much. The only last topic I wanted to touch on before we
07:37jump into the pre recorded videos, because it was like an
07:41intro that was created later on, is we talk about functions
07:44and we talk about procedures. Oh, sorry, we talk about
07:48functions and we talk about methods. Now, throwing a lot
07:51of terminologies out here. Now, a method contrary to a
07:55function, a method contrary to a function, a function doesn't
07:58belong to anything. A trace if I ask you who does the trace
08:02belong to it belongs to flash, it doesn't belong to a specific
08:06thing. But if I would ask you if you tell me you were
08:09walking, I know when you say that you were walking, that you
08:14are the owner of that action. And because you're the owner
08:18of that action, we call it a method. Same thing for
08:22breathing. Breathing doesn't come on its own. There's no
08:24such thing as breathing without someone owning that
08:27breath, or someone creating that breath. And that's what
08:31we call it methods. And it's a very important
08:33differentiation. And don't worry about okay, what is
08:35things in programming? Things in programming are objects or
08:38classes, and we're going to look into them. And we're
08:41going to try to start tinkering with it now. But
08:43after we learn about objects and classes, it's going to
08:46get much, much easier. So after you watch the
08:49functions course, I recommend do the exam. But if you
08:53feel that you're not 100% tight with it, don't worry
08:56about it, watch the objects video as well the
08:58objects course, and then go back to the functions and
09:01make sure that you understand everything there because
09:03they're there. It's one of those stuff that it's
09:05hard to say what should you learn first functions or
09:08objects, they kind of like overlap in a way. And
09:10it's really important to know both of them. So when
09:12you know both of them, you should have a more
09:14holistic understanding of the whole scope of
09:17functions and objects. And that's really what I
09:20wanted to say in this intro. So after you watch the
09:22first hour, as long as you understand what functions
09:24are, as long as you know how to call them, as long as
09:27you know how to use them, you're in good shape.
09:30Then go back to the go to the objects class. And if
09:33you still feel like you don't really 100% get
09:36functions, then definitely watch the functions
09:38videos again. Okay.

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!