Functions vs. Methods
What is the difference between functions and methods.
Video Transcript
00:00Okay, so in the last video, I said that I'm not going to go into the details of what are
00:04functions or what are methods, but I lied.
00:08You caught me.
00:09Because that's exactly what I want to talk about in this video, although we're going
00:11to talk about it in a very, very limited way right now, because we don't know what
00:16objects are yet, and we're going to learn about it in the future and then revisit
00:20this.
00:21But let's talk about objects just really quickly in a high level, what are objects
00:27to understand what methods are.
00:30Or we even go into how it works and how do you do it.
00:34So objects are basically buckets of a lot of information.
00:38They could contain actions, they could contain variables, they could contain
00:41all the stuff that we've learned about before, but they could wrap all that
00:45information into an object.
00:47Just as much as in the real world, there are objects, we are objects in the real
00:52world.
00:53If a person, I'm a person, but I have hair, I have eyes, I have emotions, I have
00:57a heart sometimes, and I have a lot of other features and characteristics and
01:03I could perform a lot of different actions.
01:05I could walk, I could run, I could speak.
01:08So although my eyes are not an action, you could say that my eyes are
01:13variables.
01:14There are two, I have two eyes, those are my variables.
01:18And the actions are, I'm talking right now, which is, if I would be a
01:21program, it would be a function, a function that enables me to talk.
01:26So that would be my correlation between humans and computers and it's
01:32really, really, really linked together because we're talking about object-oriented
01:37programming and we're slowly, slowly getting into that world and we're going
01:42to see a lot of cross-references with the real world and instead of trying
01:45to keep it isolated and thinking, oh, computers are inside of that box, do
01:50expose it to your world, do try to make sense of it in the world you're
01:54living in because it's going to make programming so much easier and also
01:57because the developers that initially created ActionScript and the
02:02mythologies of object-oriented programming in the past and programming
02:06in general were all humans, although I must admit they were very geeky
02:10probably, but they were all humans and thus, because they were humans,
02:14they tried to find ways to engage computers in the most humanly-friendly
02:18way that they can.
02:20And although it's their personalities and their mythologies that
02:23came into action, you could leverage that because computers and programming
02:27were made in a very kind of human-friendly way and all that's left
02:31for you is to connect the dots between what's in your world, what's in
02:35the programming world and try to relate to it in that way.
02:39So we're saying that they're objects and we didn't really see how to
02:41create these objects yet, but I'm going to tell you a secret that our
02:46whole main application is a sort of object.
02:49We are inside of an object, just by running in Flash, we're in an object
02:53as soon as we run and it's kind of like the global scope, the world.
02:57Our application is an object, but we could have a lot of other sub-objects
03:02and objects could contain a lot of different things.
03:04They could contain variables.
03:07They could contain functions, which are actions and that's all the
03:12theory I want to give at this point and we're going to keep it
03:14theoretical right now and don't worry if you don't know how to
03:18connect this into the computer world, because we're going to have a
03:20whole full course dedicated to objects and dedicated to what there
03:24actually are and how to actually do them in that world, but I wanted
03:28to give that kind of brief intro to kind of explain the difference
03:33between a function and a method.
03:35Well, a function exists in kind of a random world, it has no
03:40connection to anything and we've met functions before and I want to
03:43type out a function right now.
03:45So I'm going to trace out hello world.
03:50As you can see right now, what am I doing in this line?
03:54I'm basically commanding Flash to perform an action.
03:59What is the action?
04:00Take this string that's inside between our brackets and between our round
04:05brackets and trace them out into our output.
04:09We're telling Flash to do something.
04:10We're telling it to do a command, to do an action.
04:14As much as in the real world, you would be telling someone to start
04:18talking or walking or something of that nature.
04:21So we're telling, but the only differences right now is if you
04:24ask who are we telling, we don't know who we're telling it to.
04:28We're kind of telling it into the air.
04:30We're not, there's nothing that no, we're not, we're not going to
04:33the output panel and saying output panel trace out hello world.
04:37We're just saying trace hello world and Flash on its own kind of
04:40knows what we're talking about.
04:44And that's the big difference between a method and a function.
04:47Well, this is a function because we're just telling the world,
04:50we're just telling something and somehow Flash knows.
04:53Well, we're going to find out really soon how Flash knows to
04:56actually do this command.
04:58Objects are basically buckets of information.
05:01And I'm going to trace out a function.
05:03Inside of that trace, I'm going to, so we could see it.
05:06Let's make it a little bit more simple to the eye.
05:08So I'm going to create a variable and I'm going to call
05:10it ran, which will stand for random number.
05:13It's going to be a type of number.
05:14And I'm going to put into it a value that comes back to me
05:17from a function.
05:20And the function could return values.
05:22In this scenario, we're not capturing anything, but in our
05:25scenario that we're working right now, we're going to
05:27have a function that's going to return values.
05:29And again, don't worry if this sounds a bit too out there.
05:33We're going to break this into pieces in the
05:35next few videos.
05:37The next thing I want to do is I want to call it a global
05:40class, a global, you could say, for our scenario, a global
05:44object, a global reference, which is called math.
05:48That's available throughout our application.
05:51And in Flash, the way we talk to specific properties of a
05:54class, of an object, and I know I'm throwing here a lot
05:58of terms, but just ignore the terms and just flow with me.
06:01Let's just focus on the functions right now.
06:03I'm going to call my math and I'm going to add a dot.
06:06And after I write the dot, I'm going to select a function.
06:11In our scenario, I'm going to select random.
06:13I'm going to open and close, and you can see in the
06:16hinting, what does Flash do?
06:19It will return.
06:20The math class will return a number.
06:23Now, don't worry what a class is.
06:25Don't worry what an object is.
06:26Just for now, for the sake of this example, assume that
06:30math is a bucket that contains functions and variables.
06:36And as much as we called a trace in the first line that
06:39enabled us to output something to the world, now we could
06:42go and tell something specifically.
06:44We want from the math library, we want to
06:47get a random number.
06:49And now if you trace out that random number, and I'm just
06:52going to trace it out, we're going to see that it's going
06:57to trace out a random number.
07:00Now, again, don't worry about it.
07:03I threw a lot of different words here that some of them
07:06were going to decrypt as we go along.
07:09But all I really want you to get out of this video is that
07:12there are two different types of functions.
07:15There are functions that are not connected to anything.
07:19They're not connected to an object.
07:22Well, there's other functions that are
07:25connected to an object.
07:26And when a function is connected to an object, we
07:29tend to call it a method instead of a function.
