Tip:
Highlight text to annotate it
X
In this movie, we will talk about doing something called Returning Values from
functions. If you are following along, open up the Returning_Values.fla. If you
don't have the Exercise Files, just type the same code that I have in Actions panel.
So I will select the first keyframe in the Actions layer and open up the
Actions panel. So I have an empty function called getTotal and I had that
definition there. And then, I am running the function on line 6. This empty
function is also called the skeleton of a function. It's nothing more than the
definition of the getTotal function and it doesn't really define what the function does.
So here what I am going to do is create some variables inside of the getTotal
function that I am going to use within this function. The purpose of this
function is to take different groceries and to calculate the total value of the
groceries and to give that value back. So basically returning a value involves
running a function, performing some type of calculation, and then giving back
some type of data. So I am going to create two variables inside
of the getTotal function. I am going to create a variable called steakPrice
capital P for price, the data type is going to be a number capital N and the
value is going to be $4 and 99 cents so 4.99. Now let's type a semicolon to end
the statement. I will create another variable and I will call this chiliPrice,
capital P, the data type will be number because I am using a decimal number.
I will set that equal to 1.99, then the semicolon to end the statement.
And now of course, when we test the movie, nothing is going to happen. What I
want to do is get the total of the values of stack price and chili price by
using this getTotal function as the name of course implies. So what you have to
do to get return data type is place your cursor after the closed parenthesis,
when you are defining the function. So I have get total, open and closed
parenthesis and then after that, I am going to type a colon, I am going to tell
Flash what type of data this is going to return. So I am going to type number
with capital N, so this function is going to run and it's going to give back a number.
I am also going to talk about how to use that data in just a second, but here I
am just going to focus on how to get that number out of the function so that we
have :numbers, saying that Flash is going to return a number when the function
runs. And then at the bottom of the function, I need to actually manually help
Flash to send a value out of the function. So whenever you have a data type
that's going to return, you need to have a return statement at the end of your
function and the return statement needs to be the same data type as the data
type defined in the return values. So what I am going to do is type return all
lowercase, then a space, and then I am going to do some math here. It seems to
be very simple, I will just type steakPrice + chiliPrice. And so this is going
to return sum of steakPrice and chiliPrice. Then type the semicolon after
that. So the function is going to run and then data is going to be sent out of the
function and then we can use that data. So now, this isn't really super exciting.
If I test the movie right now, I am not going to see anything, it's not really
tracing any value. The way that return, values or functions work is they kind
of treat this function running here that I have on line 8 as sort of like
a variable. So I want you to think of get total function as being a number variable
because of its return data type. So when we run the function, get total
is the same as having the value of steakPrice + chiliPrice.
So if we wanted to see that value somewhere, we can wrap, running to get total
function inside of a trace statement. So that's I am going to do here. I am
going to type trace right before getTotal and I am going to wrap getTotal in
parenthesis. So I am running a function inside a trace statement. That's
because getTotal is going to run, and then the value of getTotal here that I
have selected is going to be replaced with the return value of steakPrice + chiliPrice.
So I am going to test the movie and see the total in the Output window.
Command+Return on the Mac, Ctrl+Enter on the PC, so it's 6.98. I will close the
Preview window and return to the Actions panel. So you can use return values in
functions to reform task like the one we have here.
So our function kind of takes two values, adds them together and then returns
the result. And again, all you have to do to define the return value for
function is define the return data type with the colon right after the
parenthesis. We need to define the function. Make sure they have a return
statement returning the type of data that corresponds to the return data type
and then you can get that value by running the function.