Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

returning a parent function from a nested function

Status
Not open for further replies.

crazyboybert

Programmer
Jun 27, 2001
798
GB
Hi,

I have a nested function and want to return the outer (parent) function from within the nested (child) function.


e.g

function fncParent(){
if(somecondition) fncChild();
function fncChild(){
someaction;
return; //here i want to return fncParent
}
}

I have tried using fncParent.return; from within fncChild but this gives nasty error. I want to do this to stop the remainder of fncParent executing if fncChild is called at anytime from within it. I know you can exit nested loops using break and that you can do something like

function fncExample(){
outerloop:
for(i=1;i<3;i++){
innerloop:
for(j=1;j<3;j++){
if(j>1) break innerloop; //quit innerloop
if(i==2) break outerloop; //quit outerloop
}
}
}

anybody got any ideas how to do this with nested functions?

any help greatly appreciated

rob
 
you should separate 2 functions:

function fncParent(){
if(somecondition)
valuePassed = fncChild();

// ...continue
}

function fncChild(){
// someaction;

//here you have to return the value from this
//function that will be processed by fncParent()
return (fncChildValue);
}

valuePassed is a local variable of fncParent() function that will get the value passed from fncChild()

fncChildValue is a local variable of fncChild() function that will that whould contain the result of operations; it will be passed to fncParent().


The other option is to use fncChildValue as an argument for fncParent():

function fncParent(valuePassed){
// . . .
if(somecondition)
// . . .
}

For this case when you actually call fncParent() function (like as some event handler) you should write this:
fncParent(fncChild())
 
hmm i think you miss my point slightly?

I could split the functions up thats fine. but i thought it would be neat if i could do it this way as the child function is only ever called from within the parent so i dont need it in the global scope chain.

What i wanted to do was effectuvely recreste the line

return;

inside the parent function from the child function. Does this make sense. As the example above shows with the for loops. Maybe this isnt possible but would be really neat if i could as it wouyold save some code etc. I have it working with flags at the moment so its ok.

So in this case i want to use return NOT to pass a value back to the calling function but rather to end execution of the calling (parent) function.

It may help if i explain what the function do:

parent is a form validation function.

child is an error function. if some error is found child is called and this displays an error alert and resets the appropriate form fields. I would then like to stop execution in the parent. Now this could be done really simply with an extra line at each location where the child function is called which returns the parent. However if i could return the parent from within the child it would avoid code repetiton.


its more of a curiosity than anything else as the nature of the code im doing is fairly simple.

still cheers for your thoughts

rob
 
Well, it's a question of building the right functions architecture. In general, it is recommended to split the functions, no matter what they do. They should exchange only the result values and nothing else.

Think in this way: your &quot;child&quot; is &quot;parent&quot; now in my example.
Pass the names of fields to be reset to a child function. It will show alert, reset fields and return control to parent.

function parent() {
// . . .
if (somecondition)
child(formName.fieldName1, formName.fieldName2, formName.fieldName3);

// break
// or continue
}

function child(args) {

// show error alert

for (n=0; n<args.length; n++)

args[n].value = &quot;&quot;;

}

Not sure about args.length, didn't use it for a long time. The idea is that every function recieves an array of arguments, and you can access each one of them as an ordinary array items.

Hope it will help you.
 
Cheers Starway,

The solution i have adopted in this case is a bit of a mix of both. I have decided that I probably cant end the execution of a parent function from within the child using one simple statement like:

fncParent.return;

which is fair enough if you think about it as it isnt like a loop control where you can jump out of it at any point.

oh well. in order to save some code i have maintained the nested function. This is because the validation function is used on a number of different pages and fields and hence i can call the funtion from an event handler like this:

onblur=&quot;fncValid(this)&quot;

and then use the argument in the function for the form object in use. That way i dont have to pass anything else aroubnd and the coe is good and neat.

However at each point where i call my &quot;child&quot; function i now have to have something like:

if(somecondition){
fncInValid(argument);
return; to exit parent fnc
}

which isnt ideal but works. i was just hoping to get rid of the returns and exitr the parent form the child.

such is life.

all this is just in case you are interested anyway cheers for your thoughts and if anyone should stumble across or knows of a method for achieving what i described id like to know just outta curiosity.


rob

:-Q


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top