Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
In this movie I'll explain how to make a function return a value. If you'd like to follow along, I'm in Returns.fla that can be found in the chapter 03 folder of the Exercise Files. Before I explain what it means to return a value, let's walk through this file and just check it out. I have some snow, one snowboarder in the foreground, and one in the background. I'm going to the Selection tool, or you can press V on you keyboard and let's just check their instance names: boarder1_mc. and then boarder2_mc.
I'm going to click on the actions keyframe of the actions layer to open up the Actions panel. I have a really simple function called moveBoarder and it's going to make boarder1's y subtract by 150, so it'll make it go up 150 pixels and then the .scaleX property scales an object horizontally and the scaleY property scales an object vertically. And the value of 2 that I'm giving here and here will make it twice as wide and twice as high.
So if I close the Actions panel and I test the movie, we can see that the boarder is a lot bigger. But what if, after this function runs, I want to make the height of this boarder the same as the height of the first boarder? But I don't want to run the function on the first boarder, I just want to make his height the same. Let me show you how to do that by making the function return a value. Let's open up the Actions panel again. Now I'm going to talk about what this colon void means. So function moveBoarder:void.
The colon specifies the datatype that's going to be returned when the function runs. So basically a function can run and give me back nothing or it can give me back some type of data. Let me show you how that works, because it's a little bit easier to demonstrate than to explain. So let's replace the word void and then I'm going to type the datatype that I want to return. I'm going to type Number with a capital N. So when this function runs, it's going to give me back a number. After line 5, where we set the scale y of boarder1_mc, I'm going to press Enter or Return on my keyboard and then I'm going to type the word return.
Notice that it turns blue when you type it. Then type a space and then we're going to type the value that we want to return. And again we'll see this in action in about two seconds and you'll see what a return does. So let's return boarder1_mc.y; I'll test the movie again. So notice that nothing happens different. So our function returns a number but so far this does nothing.
Now what I'm going to do is click on line 9 right before the moveBoarder function and type trace. And let's wrap moveBoarder, open and close parentheses, inside of parentheses. Just make sure you have two close parentheses here. You need to have one to close out the moveBoarder function and one to close out the trace statement. Now test the movie, and we get the boarder's new y position, which is 160.05.
Now let's actually do something with this number. So I'm going to take this trace statement and remove it. So I'm going to take away that close parenthesis then the trace and the parentheses, so we just have moveBoarder. Now we can set the y positioning or I can do anything I want with this moveBoarder, cause when this runs it's going to be equal to a number. So moveBoarder here it's not only going to run this function, it's equal to whatever the new positioning of the y is of boarder1_mc. So if I'd like to set the y positioning the same for boarder2_mc, then right before moveBoarder, I'm going to type boarder2_mc.y = So boarder2_mc.y equals moveBoarder(); So that will run the moveBoarder function, that's this whole block.
At the bottom or the last line that block will return the y positioning of boarder1_mc. It will kick it back out as the datatype number, and that will replace this function right here. So a function can do something and give you something as well. So in this case, moveBoarder open and close parentheses is going to give us the y positioning of boarder1 and that'll make the boarder2 y positioning the same as the boarder1.
So I'm going to close the Actions panel. Let's take a look at it now. Press Cmd+Return or Ctrl+Enter to test the movie. And it looks like I spelled boarder wrong again. Good times. Okay. Let's test the movie. And there we go. The y positioning is in the center. And so we see that these objects are aligned in the center. So that is how to make a function return a value. And returning a value just means when I run the function it's going give me back some type of data. So in this case moveBoarder is equal to a number, and that's specified right here after the colon, and then you also need to specify the word return, and then you can have some sort of value. So instead of boarder1_mc.y I could type any number I want and it'll give me a similar result.
And that is how you make a function return a value.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 101830 Viewers
61 Video lessons · 88581 Viewers
71 Video lessons · 72401 Viewers
56 Video lessons · 104095 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.