View Full Version : Function explanation
Xisco
November 11th, 2005, 08:30 AM
I ve a bit of missunderstood about functions: as i read the functions works:
basically 2 types:
named (can be lonely)
unamed (need and object)
- can i define a function wherever i want? ( _root, MC...) and call it from everywhere?, before or after i created it?
- if the variables of a function are local variables of that function. How can i store the result of a calculation of the function, in a variable outside it?
apologize my poor english
Regards!
Mirandir
November 11th, 2005, 09:07 AM
1.) No a function must be created before it can be used. However when using the named functions syntax: "function myFunction() {... }" it will be created first thing when that frame's code executes. That means before any other code of that frame can run. But after the code of any parent.
Something like this:
_root -> Create named functions
_root -> Run code.
_root.mc1 -> Create named functions.
_root.mc1 -> Run code.
_root.mc1.submc1 -> Create functions.
_root.mc1.submc1 -> Run code.
But other than that they can be executed from anywhere assuming that you specify a path to it using dot-notation.
"unnamed" functions ( myFunction = function(){...} ) are created in sequence just like any other script.
e.g:
Unnamed functions
incorrect:
myFunction();
var myFunction = function()
{
trace("Hello world!");
}
As unnamed functions are created in sequence with the normal frame code any functioncalls to it must come after the function statement
Correct:
var myFunction = function()
{
trace("Hello world!");
}
myFunction(); // myFunction is created so "Hello world!" is written into output panel.
Here the function call is done afterwards and thus the function runs as it should.
Named functions
myFunction();
function myFunction()
{
trace("Hello world!");
}
Even though the function declaration is after the function call this code will work as it should since that kind of function declaration is always run before any other code of in that skript.
2.) Using the return statement you can make your function return data. However only a single "variable" can be returned. And the execution of the function is autamitcally ended after the return statement is run.
e.g:
var sum = myAddition(1, 1);
trace(sum); // Should write 2 into the output-panel.
function myAddition(a, b)
{
// Adds a and b and returns the sum.
return a + b;
// Code below this point will never run as the return statement will terminate
// further execution of function code
}
/Mirandir
senocular
November 11th, 2005, 09:18 AM
/Mirandir :thumb:
Xisco
November 11th, 2005, 11:06 AM
Another hero for my hero´s list
Thanks
Regards
Xisco
November 11th, 2005, 11:11 AM
another question:
if now sum is 2, and var sum is in _root, can i access it from everywhere? ( functions included?)
mathew.er
November 11th, 2005, 11:38 AM
If sum is declared in root, than you can access it by _root.sum using dot syntax to show path to it.
If it would be declared in mc2, which is inside mc1, which is in root, than path to sum would be _root.mc1.mc2.sum
You can also use relative paths like "this", which is current namespace or "_parent", which is an object one level higher in hiearchy.
So lets say that we are in our mc2 and relative path to sum, which is declared in root would be _parent._parent.sum and so on...
I could also mention _global, which makes a variable or a function accessible from everywhere after declaration... so you could access sum after declaring it like _global.sum = something by only using sum
//edit: I forgot to mention that if you use "var" for declaring a variable inside a function, it will be local and will expire after function ends...that is that you couldnt access, because it won exist any more =)
senocular
November 11th, 2005, 01:00 PM
note that local function variables dont exactly expire after the function ends. They can continue to exist after a function completes just so long as you have a way to reference them.
Xisco
November 11th, 2005, 02:53 PM
Much appreciated!
Thanks
icio
November 11th, 2005, 03:04 PM
Something else that you may wish to look into is `return`
Here's an example:
function myFunction(a, b) {
return a+b;
}
var myVar = myFunction(10, 20);
trace(myVar);
Hope this helps :thumb:
2.3K Posts :)
claudio
November 12th, 2005, 07:03 AM
note that local function variables dont exactly expire after the function ends. They can continue to exist after a function completes just so long as you have a way to reference them.
You mean if the activation object became persistant and the var can still be found in the scope chain?
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.