milkmachine = function(argument){
    var r=1, k=2;
    return r;
milkmachine(); //returns r, good
milkmachine(); //this should return k

closed as unclear what you're asking by user16764, Dan Pichelman, FrustratedWithFormsDesigner, Robert Harvey, gnat Oct 31 '13 at 20:48

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    Could you try to compose a theoretical example? "return something somewhere else in the code" doesn't make sense to me -- the function returns wherever it was called. You could store the return value and use it later, or you could trigger an event listener elsewhere in the code, maybe. But I can't say for sure what you need because I don't fully understand what you want. – apsillers Oct 31 '13 at 19:29
  • 5
    This just looks like a fundamental misunderstanding about how functions work. A function with a return statement will return a value. You access that value later by storing it into a variable so that you can refer to it later. – Robert Harvey Oct 31 '13 at 19:38
  • right but can you change what the function will return outside of the function? – Kinnard Hockenhull Oct 31 '13 at 19:44
  • @KinnardHockenhull So you're asking: is it possible to alter the code of the function, after it's been defined, to make it return whatever is in the internal variable k instead of what's the internal variable r as it was defined to return? – paul Oct 31 '13 at 19:46
  • 3
    I think the short answer to this question is "No". – Greg Hewgill Oct 31 '13 at 19:48

The short answer, as Greg Hewgill commented, is: no (to the best of my knowledge). There are a few shoddy ways you could get that behavior as well as a few recommended ways:

Shoddy Way #1:

One way is to have two versions of the function: one that returns r, the other that returns k. This is not recommended because it is duplicating the same code, just returning something different.

Shoddy Way #2:

Another option would be to add another parameter to the function which would specify the variable you want to be returned:

milkmachine = function(argument, retWhat){
    var r;
    var k;

    //do something with arguments and variables

    if (retWhat == "k")
        return k;
        return r;

This is also bad since you're using one function to do multiple things and a function should only do one thing; also, it gives external code more info about the inner workings of the function than is good. (There are probably other, better reasons this is bad, but I can't think of any at the moment.)

Recommended Way #1:

Instead of returning just one value, you could return both values wrapped in a container object:

milkmachine = function(argument){
    var r;
    var k;

    //do something with arguments and variables

    return new MilkMachineRetrunValue(r, k);

var retval = milkmachine("some arg");
// access retval.r here
// access retval.k here

// or you could directly access the variable
// that you wanted within the return wrapper
// after the call like so:
// milkmachine("some arg").k

Recommended Way #2

Use an object:

function MilkMachine() {

MilkMachine.prototype.run = function(argument) {

    //do something with arguments and variables

var mm = new MilkMachine();
mm.run("some arg");
// access mm.r here
// access mm.k here
  • 7
    Recommended Way #1 simplified: return {'r': r, 'k': k} – Izkata Oct 31 '13 at 20:12

Not the answer you're looking for? Browse other questions tagged or ask your own question.