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

Sending A Param

Sending a param/variable to a function.

Video Transcript

00:00Right now we've worked with our abs function, but we kept our variable defined inside of our abs function.
00:06And in the real world for our function to be useful, we would need to be able to send that information in dynamically.
00:12And it's quite easy to do that in Flash.
00:16And the way we do that, such as we've seen when we worked with the math class directly, so if I just trace it out here,
00:22math.abs, and we sent into it some information, and we close our function, and we close our trace function.
00:33And then we would get the output of our abs function, which would come back as 200.
00:38Now, as similar as our structure with our function that we sent this variable into it, same thing we do for our definition of our function.
00:48So as we define our function, we would add into the function the type of value that we want to return.
00:55Now, since ActionScript 2, we would strict type and define what exactly the value is.
01:01So let's start off first for setting our number.
01:03So I'm going to just set A, which basically what I'm going to do, I'm going to take our variable A,
01:08and I'm going to set it by putting inside a colon, and I'm going to say what type it is, a number.
01:14And if you notice, there's a little difference between the way we've done it on line 8 and in line 7.
01:20In line 7, we don't define a new variable, although we are defining a new variable, we wouldn't put var A.
01:26It would come back as invalid.
01:28As you have to define new variables inside of abs, it would kind of be redundant to put the word var in there.
01:35Not only would it be redundant, it's also not legal.
01:39So we could delete now our line 8, where we're defining our variable internally,
01:43because now we're passing the variable through our abs function.
01:48So that's all we had to do to change our function to accept an external variable.
01:54So if I try and trace out again another trace, but this time around,
01:57instead of calling my math.abs, I'm just going to call abs and send it the same value
02:02as we've sent to the math.abs, let's see what would happen.
02:06Now this is going to be very interesting, because when we run this, it's not going to do the exact same thing,
02:10and let's see why.
02:12As we run the application, we're going to see that we're going to get two values back.
02:16And if you're confused about, let's see why that's happening.
02:19Because our abs function doesn't return anything.
02:23While our math.abs does return a value, and then we're tracing it out,
02:27our abs function is actually tracing out the value directly.
02:31Thus, if we try to trace out from our function, this function, our abs function doesn't return anything.
02:37And in other words, it returns undefined.
02:40So by adding that extra trace, we're actually not really doing the right thing.
02:45And all we really have to do is call our abs as our abs function is creating the trace directly.
02:51Now obviously in a real application, this wouldn't be very useful,
02:54because if we wanted to turn a value to be absolute, we would want to get that value back.
03:00So we could assign it back into a variable, or use it directly.
03:05So in this scenario, this function is still not useful,
03:07and we still have one more piece of information to learn,
03:10and get back to to learn how to work with it in this way.

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!