PDA

View Full Version : Calling a function inside another function



flashdaddy
July 1st, 2008, 12:56 AM
Hi All,

I've set up two functions (friendsClick and policyLink) I would like to use everything inside of friendsClick inside of policyLink but I'm not sure how to go about doing it. I get the following error:
1136: Incorrect number of arguments. Expected 1.

My code looks similar to:



function policyLink(e:MouseEvent){
friendsClick();
"all my other info here"
}


I can just copy everything from the friendsClick function and paste, but I'm trying to limit my amount of repetitive code (I'm trying to get better at that). Please Help.

Krilnon
July 1st, 2008, 01:58 AM
You probably should have posted more of the relevant code, since I can think of a few things that might be causing the issue.

Try:
function policyLink(e:MouseEvent = null)

Or:
friendsClick(null);

It's probably the first one, but I can't be sure.

tousdan
July 1st, 2008, 04:37 PM
You have many choices:

You could extract the shared code from your functions into another functio with a more descriptive name (IE: if I had a button called 'btnAddUser', i'd take the code from btnAdduserClick and put into a function called AddUser(). Then you would just have to make both of your event listeners call this one function.

You could make both of your buttons share the same function. Note that this will only work if both functions take the same type of parameters.

Both solutions from Krilnon will work but only if you do not use the parameter of the function.

flashdaddy
July 1st, 2008, 08:27 PM
Hi All,

I've set up two functions (friendsClick and policyLink) I would like to use everything inside of friendsClick inside of policyLink but I'm not sure how to go about doing it. I get the following error:
1136: Incorrect number of arguments. Expected 1.

My code looks similar to:



function policyLink(e:MouseEvent){
friendsClick();
"all my other info here"
}
I can just copy everything from the friendsClick function and paste, but I'm trying to limit my amount of repetitive code (I'm trying to get better at that). Please Help.


I got the problem solved. Apparently you can't put a function inside another function when you have an event listener associated with it. I had to do something like this:


oldFunction.addEventListener(MouseEvent.CLICK, btn_Fade);

function oldFunction(e:MouseEvent){
newFunction();
}

function newFunction(e:MouseEvent){
"all my functions"
}


Something like that anyway. It worked out for me though. Thanks to all those who helped out.