Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Aborting an outer function from within a function that it calls

Is it possible to make youth return/stop executing from within box? Directly, not something like:

function youth(){
  var check = true;

  function box(){
    //code
    check = false;
  }

  //code
  while(check){
    //code;
  }
  return false;    
}

(where there is a check variable which box changes)

like image 381
tcooc Avatar asked Feb 25 '23 11:02

tcooc


1 Answers

No, you cannot, unless you throw an exception from the inner function (but that doesn't really count as "returning," and it's not really a good design pattern anyway).

It does not make sense to allow a function to make its caller return a value, since the calling function might not even be declared in the same context -- perhaps it is some other function that accepts a function argument, and that passed-in function shouldn't be able to modify the called function's behavior.

like image 115
cdhowie Avatar answered Apr 26 '23 11:04

cdhowie