Creating Functions
In this video we are going to create our first function.
Video Transcript
00:00As you can notice, we already are inside of a function.
00:03And we haven't really created it on our own,
00:05although we are dynamically creating it.
00:08But we didn't look too much into the depths
00:10of how to create it.
00:12But we could leverage this kind of copy-paste
00:14that we've been doing for a while
00:15and see how we could create our own function.
00:17And if you look at this function,
00:19this function's name is main.
00:22And this structure, although we're
00:24going to ignore the public and private for now
00:26until we get into the world of, you know,
00:28the deep blur into the world of classes and objects,
00:31we're just gonna create something very similar
00:33to our function right here.
00:35And the way we do that, we're gonna still work
00:37inside of the zone that we're permitted to work in for now
00:41until we get into the world of object-oriented programming,
00:43that is.
00:44Okay, so let's, enough intros, let's build out a function.
00:47And the way you build out a function,
00:48you start off with the word function,
00:51letting Flash know that you're defining now a function.
00:54You follow it with the name of your function.
00:58And let's call this function abs,
01:01kind of similar to the abs function
01:04that we've seen the math class contain.
01:08We're gonna open a bracket, round bracket,
01:11close a round bracket, open a curly brace,
01:17enter, enter, close a curly brace.
01:22Okay, so we have a function, we've created a function now.
01:27Now it doesn't really do anything yet,
01:28so let's make it do something.
01:31And inside of our function,
01:33we're gonna create a new variable.
01:35And that variable, variable will be var a,
01:40which will be a number,
01:42oh, not a null, a number.
01:46And let's give it a value of minus five.
01:51We wanna actually make sure that the value
01:54that we're gonna output will be a positive value.
01:57So what we wanna do is we're gonna ask if a,
02:00we're gonna do that inside of our round bracket,
02:02if a is smaller than zero,
02:06that means it's a negative value.
02:07And if it's a negative value,
02:09then we want to duplicate a by minus one, right?
02:14So we wanna make a, whatever it was, times minus one.
02:20Now if we wanted to, we could have leveraged the,
02:24the shorthand ifs, we could have done it a million ways,
02:26but we've decided to do it this way.
02:28And although we don't really have more than one parameter,
02:30I've opened a round bracket and closed a round one,
02:32but we could have not if we wanted to.
02:36And the next thing I wanna do,
02:37I just wanna trace out that value of a.
02:42And let's see if our application runs,
02:43and I'll tell you in advance, it won't,
02:46because we created a function, but we never executed it.
02:49So if we call our function now,
02:52we're gonna see that nothing happens.
02:53Although we created a function, and here is our function,
02:56we never called it.
02:57And to call our function,
02:59we're gonna need to actually call it.
03:01Now, contrary to, well, let's first execute it,
03:05and then set it before me,
03:07starting to add more things to the plate.
03:10Let's add a call to the function.
03:14And this is how we call functions,
03:15same way as we've done with the trace,
03:17and we've seen with the math.abs.
03:20And if we run the application again,
03:23it will output five,
03:25as it turned our minus five into a positive value,
03:28and then it traced it out.
03:30If our number would always be,
03:31if our number would be a positive value,
03:33then the if would never happen,
03:34it would just move on into the trace itself.
03:37So this is the basic structure of a function.
03:40What I would like you to do now is take a break,
03:45and create your own function.
03:46Let's say create a function that doesn't matter
03:49but reverses a number from being positive to being negative.
