Return Something;
To really make our function work we need to have a way to return values back to who ever called them. This is what we'll look at in this video.
Video Transcript
00:00We are back and I pre-typed or more accurately I pasted in two functions that we've created earlier
00:07Which was our min function that accepted three variables and then traced out the minimum value and our apps function
00:14which accepted one variable and
00:17Then output it the same variable only in an absolute value without any negatives in a world with no negativity
00:25And the reason we were missing a piece of the puzzle was basically
00:28we're not really getting any value back from a function and as we said in one of our previous discussions is that a
00:35Function that doesn't return anything is not really a function is it you know in some languages. They call it a procedure
00:41Mathematically, it's not really a function a function gets a value then it returns of it processes it
00:47Processes it and then returns a value, but our function so far didn't return anything now
00:53there's nothing wrong with functions that don't return anything, but
00:56We need to have a way to build functions that could return something and it's not that hard to do it what I wanted to do
01:02Before I show you what we want our application to be capable of doing we want to take our min value
01:08and let's say we're going to send the value of 10 20 and minus 1 and
01:13We want to be able to duplicate that value with our abs value
01:18And let's say we send our abs minus 100 now. This looks kind of confusing, but what am I doing here? I'm taking min
01:27Which is going to return hopefully when we're done here
01:30It's going to return a value which will be minus 1 and we're going to want to duplicate that
01:34With the value of minus 1 absolute now that doesn't make any sense
01:38Why would we do that we would never do that, but we would want this value to come back as minus 100
01:44Now if we run our application now, we're going to see that that's not exactly what we're going to get back
01:49We're going to get a lot of weird
01:51Things going on here right now, but if we would use our built-in math
01:57Library we could have done that easily. We would just type math dot
02:02Min and we could send it 10
02:0520 minus 1 and then we could duplicate it times our math dot abs again
02:12And we could send minus 100 again
02:16And if we run this application this time around when we're not using our functions
02:20We could see that we will get a value back and that value will be minus 100 and we want our application to be capable
02:26Of having that same similar functionality now the way we could do that and I traced back
02:32I said back our original trace back in with our our own functions that we defined and
02:37The piece of the puzzle that we're missing is that our function doesn't return a value to us
02:42It's tracing a value out instead and in the words of what I said the answer to that question is all we really have
02:48To do is change our from our tracing out value to return the value
02:54re
02:55turn the value
02:58Return space and whatever value we want to return back now before I continue any further
03:04It's really important to notice that you could only return one value back
03:08You can't return multiple values or more accurately. You can't return multiple variables
03:15Although we could send as many variables as we want in our function description
03:19We could send as many attributes we want to our function, but we cannot return more than one variable at a time
03:28Action script 1 2 & 3 do this part quite similarly only there were few more features added into action script 2 in
03:36Action script 3 that didn't exist in the definition of function previously
03:40So since action script 2 we had a little bit better compiling well action script 3 got even better
03:46But in action script 2 & 3 we we could define what type of value to expect what type of value our function could return
03:54Now with that said I could run this application because our we're running fruit flu through flash
03:59Which is not that strictly typed it doesn't really care if we define it or we don't define it
04:04And if we run our application, although we could change that settings, but the default settings out of the box
04:09It's just as it doesn't matter
04:11And if we run our application the way it is right now
04:14We are getting warmer. Okay, so we have still a problem, right? We're getting a hundred back, which is good
04:22But we're having here some sort of issue here. No, well a hundred is not exactly what we wanted
04:26Well, what we're missing is we didn't change our abs as well. So it's still kind of working funky
04:31So instead of that, let's change our abs as well to be return
04:36Return our a value instead of tracing our a value and let's try running the application again and see what happens
04:42and
04:44Success now that both our functions return a value instead of tracing it out
04:49We're getting the value that we would expect
04:51So now at this point where our application is working only thing is we need to add well
04:57It's not that we need to but we want to add right now
05:00It's everything is good and easy because we're sending a number and we're returning a number and all is good and nice
05:06But what would happen if we made a mistake and instead of sending our number back?
05:11We would send a string back by mistake
05:14That could happen. It happens quite often
05:17Now if you'd run our application, we would get a weird bug
05:19We wouldn't really it would be really hard for us to isolate this and figure out why is that happening?
05:24Especially if this wasn't a string directly and it was some sort of variable
05:29the way to avoid that is that by helping the
05:32Compiler know what type of value to expect to get back from our min function
05:37And the way we would do that is after we open and close our bracket right after we define our variables
05:43We're gonna set we're gonna type a colon again similar to how we define our variables and we're gonna tell flash
05:51We're gonna tell the function actually what type of value should flash expect to get in return from this function
05:58So now I'm gonna tell flash that we're expecting to return a number as well
06:02And if we send something that's not a number it will generate a compiling error
06:07Which will help us then figure out why is our application why our application doesn't work. So if instead of sending our min value
06:14I delete that and I'm gonna send Ben again and I run the application again
06:19We're gonna get an error and the error will tell us that implicit correction of a value of type string
06:25To an unrelated type number in line 17 and we could see yep. That's exactly what we did
06:32Okay, so it's really useful to be able to add types
06:36So let me just add here one more type. Well, it's set our abs to be to give it a type as well
06:42So after our opening and closing round brackets, we're going to tell flash colon number
06:47We're gonna return a number
06:50Thus as much as we wanted to make sure when we called our abs function
06:54That to make sure that we're sending a number or you would generate an error, right?
06:59So if I would send here in our abs instead of minus 100 if I would send Ben and I would run it
07:06Well, it didn't give us an error which well, I would hope it would but it doesn't I
07:11Guess in a more stricter mode it would give us an error. But what actually is happening flash is converting that number
07:17That string into a number dynamically. So we didn't get our compiling error as we would hope to
07:23But as we are return value is set back then we are getting return a
07:30Compiling error if we set if we don't return the value that's correct
07:35Okay, so flash is not you know
07:37It's not idiot proof if we make a mistake not every mistake flash is capable of capturing and giving us information about
07:44okay, so
07:45this kind of gives us it should give you a little bit more comfort and working with
07:52Returning value. So now we actually know how to build real function real functions the way that they were originally
07:57thought of by
07:59mathematicians a long time ago, I guess
08:02so what I would recommend is again if this is a bit kind of crazy still then just jump back to your drawing boards or
08:10more accurately your flash and
08:12start tinkering with functions building your own functions and
08:16Returning values such as build the build functions that mimic behaviors in the math class such as math that max
08:25Okay, look into the math functions and see what type of function it has and see what could you recreate on your own?
08:31What type of values you can know how to add the parameters and return the values
