View Full Version : function explanation
chic0
December 28th, 2004, 08:48 PM
Hello, I just started learning AS a day or 2 ago :glasses:
Now, I don't really understand how to use functions,
I read the function page on kirupa.com, but I saw other (random) files where the functions included a lot of information between the brackets..
like:
test = function(a, b, c, d, e, ...){
}
Could anybody help me understand when/why to use all that data?
I tried google, but than didn't help :blush:
Thanks,
lostinbeta
December 28th, 2004, 09:59 PM
Those are called arguments, you can use them to dynamically change the value of a variable/item within the function... for example...
function doubleNum(num) {
return num*2;
}
trace(doubleNum(3));//returns 6 in the output window
Of course that function is utterly useless, but it's an example.
You can add as many arguments as you want, as long as they are seperated by a ','.
maximum_flash
December 29th, 2004, 02:05 AM
also, it is important to note that arguments have to be passed to the function in the same order as they are required.
for example:
function quotient (a, b){
trace (a/b)
}
when you call it, you must give the values in the same order. if you do not give a value, the value is given the default of "undefined"
so, when calling it:
quotient (4, 2) // would output 2
if these parameters had not been passed properly, you would have had some pretty adverse results.
basically the same thing lostinbeta said, just a little more you should know about arguments in functions. good luck!
*edit* // can't spell
chic0
December 29th, 2004, 01:00 PM
Ok, so:
Parameters are handy because u can specify different values each time you use the function.
But u can also let the function work without these parameters? (static values)
Greetz
lostinbeta
December 29th, 2004, 02:32 PM
Correct.
Arguments are not required when writing a function. However, if you do define an argument and use that argument within the function, you need to be sure to add it within the call of the function otherwise it will be passed to the function as null/undefined.
tofuisonmyside
December 30th, 2004, 10:13 AM
my 2 cents.
a function is a set of actions to make something.
for example, you want to position a movie clip on stage
you can create a function for this task.
//supposing you have a movieclip named myMC on stage
function position_MC(){
myMC._x = 50
myMC._y = 120
}
position_MC()//call the function
you can see this is correct but it can be used with params
function position_MC(x,y){
myMC._x = x
myMC._y = y
}
position_MC(50,120)//call the function
better but you can do more things, for example, use this function for all your movieclips on stage
function position_MC(mc,x,y){
mc._x = x
mc._y = y
}
position_MC(myMC,50,100)//call the function
Hope this helps
lostinbeta
December 30th, 2004, 10:45 AM
better but you can do more things, for example, use this function for all your movieclips on stage
function position_MC(mc,x,y){
mc._x = x
mc._y = y
}
position_MC(myMC,50,100)//call the function
Hope this helps
And to elaborate on that note, after you have a full understanding on how functions work and how/when to use them. When it comes to things like the above where you need to target objects and/or movieclips you can get into prototypes...
MovieClip.prototype.position_MC(x, y){
this._x = x;
this._y = y;
}
//useage
myClipsInstanceName.position_MC(50, 100);
But that is for the future after you have a better understanding of basic functions, targeting, etc.
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.