Multiple return statements

C++, C#, Java, PHP, ect...
Post Reply
Mardonis
Posts: 139
Joined: Wed Jun 29, 2011 7:54 pm

Multiple return statements

Post by Mardonis »

I'm having an issue with a piece of code that i have in a function. Using Netbeans with jslint.

Code: Select all

function addItem(e) {
    'use strict';
    var t = {},
        n = e.name;

    return $.extend(t, this.defaults), $.extend(t, this.list[n]), t.life = t.hitPoints, $.extend(t, e), t;
}
Where the first "," and "$" are, I am getting a error that says: Expected ';' and instead saw ",". I have looked up on how to do multiple return statments and it says you can use commas so I'm at a loss on what I'm doing wrong. Would I need to make more variables and assign them to the varius return statements and then just return the multple variables? Also would that work for easablity of reading it by doing that?

Thanks
Mardonis
User avatar
rockinliam
Posts: 466
Joined: Sun Jun 07, 2009 11:26 am

Re: Multiple return statements

Post by rockinliam »

Best to return an array of your data, nice and simple, in other languages you might use a struct or something like that.
Skillset: C/C++, OpenGL, C#, Lua, PHP, MySql, Web Dev etc.
Website: https://liam-griffiths.co.uk/
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: Multiple return statements

Post by Jackolantern »

As others have mentioned, this is not possible. You cannot use commas in return statements in Javascript this way. You will either need to return an array or an object with the values.
The indelible lord of tl;dr
Mardonis
Posts: 139
Joined: Wed Jun 29, 2011 7:54 pm

Re: Multiple return statements

Post by Mardonis »

Will get on it. Thank you for the good info.
User avatar
hallsofvallhalla
Site Admin
Posts: 12023
Joined: Wed Apr 22, 2009 11:29 pm

Re: Multiple return statements

Post by hallsofvallhalla »

I am going to be a little goofy and give another way of doing things. Not saying it is the right way but just for sh&ts and giggles, i need a break from staring at C# and Linq.

Code: Select all

var ReturnedResult = GetResult();
var Results = ReturnedResult.split(",");

function GetResult()
{
 var First = "First";
 var Second = "Second";
 var Third = "Third";

 var FinalyResult = First + "," + Second + "," + Third;

 return FinalResult
}
Mardonis
Posts: 139
Joined: Wed Jun 29, 2011 7:54 pm

Re: Multiple return statements

Post by Mardonis »

Thank you for this example. I'll take a look in doing this one also.
User avatar
hallsofvallhalla
Site Admin
Posts: 12023
Joined: Wed Apr 22, 2009 11:29 pm

Re: Multiple return statements

Post by hallsofvallhalla »

i would definitely use a single array over the way I did it.
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: Multiple return statements

Post by Jackolantern »

Yeah, I wouldn't suggest to do it that way at all haha :lol:
The indelible lord of tl;dr
User avatar
Chris
Posts: 1580
Joined: Wed Sep 30, 2009 7:22 pm

Re: Multiple return statements

Post by Chris »

That doesn't look right unless it were Python, but that's not Python. Make a list with square brackets.

Code: Select all

return [$.extend(t, this.defaults), $.extend(t, this.list[n]), t.life = t.hitPoints, $.extend(t, e), t];
Fighting for peace is declaring war on war. If you want peace be peaceful.
Post Reply

Return to “Coding”