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

Calling Functions();

How to call functions that exist in the system and how to send them parameters.

Video Transcript

00:00Let's break down actually the structure of how we actually call functions
00:05So the way we call functions as much as when we worked with variables we gave variable names
00:10functions have names as well as
00:13Trace is the function we've been working with so far trace is the name of the function and thus we
00:19first put the name of the function and
00:23Then we open a bracket put whatever information we want to pass to the function if that function
00:30accepts
00:32parameters, we're going to look at how do we actually create parameters very shortly and then we close our bracket now
00:38It's really important it's very important and critical to open and close a bracket
00:43One it makes flash know that you actually want to call the action and not call the function itself. What does that mean?
00:51Well, I'll give you a little secret. That's not really a secret. You don't let me comment that out instead of deleting it
00:57Everything in flash is variables and you already know what variables are so it makes it much easier and functions are a
01:05Unique type of variable, but they're a variable as well. They're actually an object
01:11Which is it's a type?
01:13We've said that so far we've learned about basic we've learned that we've learned about primitive variables
01:19But that's not true anymore because a function is actually a complex variable
01:24Because as we said everything is variables in flash
01:27Thus a function is another type of variable, but it's not one of the primitive values
01:32So it must be a complex variable
01:34But even when you think about it a function could do a lot of stuff
01:38It contains a lot of information to be being capable of doing that
01:42Thus it has to be a function because it doesn't store only one value. It's not like a number
01:48It's not like a boolean value. It's not like a string that just contain one value
01:52They contain a group of text or they contain a digit or they contain a true or false value
02:00so
02:01functions are complex
02:03Variables, although we didn't define them as yet. We didn't see how we construct them
02:08but everything in flash in the back underneath the hood is
02:12objects or
02:14primitive values and
02:16I'll tell you another secret that the primitive values in flash are
02:21Not that primitive behind the scenes they work almost as objects
02:25And there's just one little difference that we're gonna dig deeper into when we work into objects really and kind of get a grips on
02:32What they are, but with that said let me prove my point. Let me prove why it's important
02:37So I'm gonna trace out inside of my trace
02:40Which is kind of funny. I'm gonna trace out trace itself and
02:44See what comes back if I try to trace out trace and I run the application
02:49You're gonna see that it's gonna tell me that it found a function function and so on
02:54Basically instead of calling the trace
02:57Executing the truth this trace instead of outputting a trace inside of here
03:02It's just outputting the trace itself
03:04But if I wanted it to trace out I would have to open and close my brackets and trace out something
03:09So let's try to trace out two for example
03:12And I know this is kind of weird because we're calling a function inside of another function
03:19And let's see what happens here. Let's see. Why did this happen?
03:21So we're tracing out in this line too, right? So the flash then executes that too and traces out too, but then
03:29Our trace itself doesn't return any value back and thus our trace will return nothing
03:37Okay, or nothing in flashes world is undefined when nothing was defined here nothing returned
03:43now there are functions that return values and
03:46we said that most of these functions were going to find inside of
03:50that crazy world that we called of methods and that connects to a
03:55World that's called dot syntax dot syntax. It basically enables us to call functions that are not sitting in the same
04:03world as we're in that they're sitting in a different bucket such as the math bucket and
04:09In that math bucket, so the way we would do it we would call our
04:12Object I'm gonna call it object and we're gonna put a dot because it's a dot syntax
04:17And then we're gonna call whatever we want and therein in our scenario. We want to call our
04:22Function name and then we would open a bracket and close a bracket again
04:29Okay, but let me comment that out and let's see that in action
04:33So if I would want to call for example the math class and I'm gonna wrap that inside of a trace again
04:38So we could see what it outputs to me
04:41This time around the math bust it doesn't output anything to the screen
04:44But instead of that it returns a value which we could capture into a variable or if we wanted to we could trace it out
04:51Directly, so I'm going to try to trace out math that random which should return to me a random number between 0 and 1 and
05:00Look what I've done first of all. I didn't just called trace random
05:03Oh, I didn't call trace random inside of a bracket and and that's it
05:09Because we're dealing with a function and if we want to deal with one it get
05:13The action to actually happen we have to open and close
05:17Around brackets and if not and we run our application again
05:20We're gonna get the same type of values and tell us yes
05:22There's a function in there
05:23But it didn't really do what we wanted it to do and for it to do what we wanted to do
05:27It's really important to remember you open and close a bracket
05:31Open and close a bracket for our random and then a random value will be generated now
05:37so far we worked in a world that
05:39Now we work. We've seen traces. We've seen some you know really initial steps in working with objects
05:46Random is a bit different than our trace because traces don't return any value back
05:51But while random returns a value back a value back
05:55Let's take it one step further and say okay, so now that we know that we have
06:00We know that we have methods. We know methods that could return values
06:03There are methods that not only return values
06:06We could send to them values such as the trace the trace
06:09We sent to it a value and then it outputs it into the screen if I just
06:15Type on trace so far we've worked on trace and had only one parameter
06:20Let's say some copy
06:23And we close our our quote and we wanted to add more value than we we put the plus sign
06:28And then we typed in more value more
06:32Content and the reason was is because we were assuming that we could only send one parameter into our trace
06:40But that's not true because we could send more than one parameter so although right now. We're sending only one parameter
06:48We have the option to send more than one parameter, so if we run this application this way
06:53We're gonna see some copy some more copy, but look what would happen if we instead of them
06:58You're gonna copy that if instead of that we're gonna send two parameters
07:03and the way you send two parameters in a function is by using the
07:07comma and
07:09Then getting rid of our plus because we don't want to do plus
07:13Instead of that we're sending one value and then a comma and then the other value
07:18And if we use our trace again our trace because it's a function that knows how to deal with that
07:23We'll output this information
07:26Again now this is only true to action script 3 this is not true to action script 2 an action script 2 trace would expect only
07:32To get one parameter
07:34But in action script 3 you could send more than one parameter
07:41Let's see another example of a function, and we said that the math class which is a library of functions
07:46and properties which are variables
07:50It's a it's a library that contains a lot of different things you could do with math
07:54So let's try to trace out something out of the math that actually expects to get some sort of variable
08:00So for example, let's take the first one which is abs
08:03What is abs math that abs is a function or a method that expects to get a number of any value?
08:10And it returns a number. What is abs is absolute which means if we send to it a negative value such as let's say minus
08:164 and we close our bracket and
08:20then we close our bracket of our trace and
08:23We run our application again
08:25It's going to take our value and turn it into a positive value is going to get rid of our minus if there is a minus there
08:32Okay, on the other hand if you would try to trace out a value that was already positive
08:37It would leave it to be positive. Let's do it 14 and
08:41It will turn it as 14 so it basically gets rid of the negative value if it's negative
08:47Okay, so we've seen we've seen that we could send a parameter
08:51But we could send even more than one parameter some now if you would try to call a function
08:56That wasn't it that was expecting a parameter, but you didn't send to it a parameter, okay?
09:00So if I'm going to I'm just going to type your math done abs
09:04And I'm going to open and close the bracket, but I'm not going to send it any information
09:09If I run this application it's going to generate an error, and it's going to tell me incorrect number of arguments
09:16expected one and
09:18We can see that it's expecting at least one value now if you if
09:23Let's see
09:24Let's see what would happen for same thing if we try to use our trace
09:27And we wouldn't send anything to it
09:29And it's really helpful to any type your tracing and any function when you open your round bracket flash by default and so would
09:36Flex would give you more information of what type of information it's expecting and here you could see something kind of weird
09:43But it's telling you argument rest param. It's not very clear. What it is
09:48We know that we need some strings, but basically what it means for what we need to understand right now is
09:53That our trace could accept
09:56multiple
09:57parameters or only one
09:59but we
10:01but let's see what happens if we call our trace without any parameters and
10:06Then we'll see that we're not getting any errors
10:09There aren't any errors and the reason why there aren't any errors is because there it wasn't strictly defined that it's expecting something
10:15And when you look at this at your ish at your hints
10:18You can see that expecting it's expecting more than one but in the scenario more than one
10:22It's anything or one
10:24So I could say if I wanted I could said said one or two arguments or three or four argument
10:30It wouldn't even matter and it would know how to deal with it, and it would output more information
10:36If let's just make sure that one thing is clear our abs shows that it accepts one parameter
10:42If you know it accepts only one parameter, and you're working in action script 3 and you send it more than one parameter in
10:49Most configurations flash would generate an error
10:53Telling you that it's expecting no more than one now if you change your configurations and flash
10:57Which I don't recommend doing then it wouldn't generate an error
11:01But don't change these configurations because a lot of other developers will expect it to be in the same type of configurations
11:08and
11:09When basically I'm saying an ish to
11:12This wouldn't create an error
11:15If you descend a function more than what it's expecting it wouldn't care well in in action action script 3
11:21It's expecting exactly what 6 6 expecting and you have to deliver what it is expecting
11:26And you can see that it for example in our trace which is different
11:29It's expecting anything and
11:32The rest the arguments parameter the rest arguments parameter kind of indicated that we're gonna see how to work with that later on
11:39But we could see that this means for us is it you could put whatever you want
11:44Okay
11:45Or at least from a compiling perspective. You're not gonna get any errors relating related to compiling errors, right?
11:53I'm gonna put in the same line line previous, so it's really important to match the right amount of
11:59Parameters of arguments that your function is expecting you to deliver. Let's see one more option. So I'm gonna type math dot
12:08Let's try
12:09Let's see what we got here and you're more than welcome to just play around with these functions to get to know them and see
12:17What you could do with them
12:18But right now we're focusing on functions. We're not gonna dig too deep into into math. I'm gonna pick
12:24Math that min with basically expects two values or more
12:30you could see the value one value to you see that you have to at least send two parameters and
12:35There might be more and it will send to you a number back. So if if you send to it
12:42Let's say all these numbers
12:46You could send as much as you want because you could see you have your rest per am
12:49Which for us we know we could send as much as we want
12:53Flash will then find the minimum value the smallest value and return it to us
12:57Because we could see that it returns a value of a number
13:01now if we run this application now, we're not gonna see anything but it is working and
13:07The reason why we're not seeing anything in our output why the output is not generating is because we're not tracing it out
13:12And that's again the reason why we're always using these traces
13:15So I'm gonna we could or create a variable like we've done before
13:18Which would be a type of number because that's what our function told us it would be in the in the hints
13:23and
13:26Append that value into our number and then trace out that number or if we wanted to we could have just put that
13:32Hold all this value directly into our trace
13:37Or
13:38Which both of them would produce the same result
13:45But the first that this option will probably look cleaner and easier on the eye, but it really is down to how you prefer
13:53Working and as you're a beginning beginner developer. We're getting used to it
13:57I would recommend to try to break it down into steps because it's gonna cut down a lot of the confusion

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!