My Secret Life as a Spaghetti Coder
home | about | contact | privacy statement
When I was young and didn't know much about programming, I remember someone saying to me "functions can return only one value." I also remember thinking to myself, "then I'm going to be the guy to write a language that allows you to return multiple values." I was naive back then, to say the least.

Of course, there are plenty of ways to return more than one value from a function. In languages with pointers, like C and C++, you can pass in an "out" parameter to functions, and have the function dereference that pointer, setting values to it. In languages with ADTs, you can return say, a Point which would then encompass two or more variables. In many of these, you might have to do that via an out parameter as well.

Of course, there are arrays and structs and components in Coldfusion. And there are string lists in just about any language (some of which may be implemented via arrays).

But I always wanted to do something like this:
var_one, var_two, var_three = some_method()

Fortunately for me, Ruby allows you to do just that. It is implemented as an array (to my knowledge), but the syntax is so elegant you'd never know it:

def some_function
  return 1, 2, 3
end

one, two, three = some_function

puts one #outputs "1"
puts two #outputs "2"
puts three #outputs "3"

Pretty sweet if you ask me.

Hey! Why don't you make your life easier and subscribe to the full post or short blurb RSS feed? I'm so confident you'll love my smelly pasta plate wisdom that I'm offering a no-strings-attached, lifetime money back guarantee!


Comments
Leave a comment

You can do this in perl, too. Very handy.

sub getname() {
@ret = qw(Ryan Stille M);
return @ret;
}

($first,$last,$middle) = getname();

Posted by Ryan Stille on Jan 29, 2007 at 08:48 AM UTC - 6 hrs

Cool. I haven't played around with perl much. I think once I had to fix a bug in an application that used it, and that was about it.

For those that don't know, I think the qw() turns each token separated by a space into a word. Is that right?

Posted by Sam on Jan 29, 2007 at 09:19 AM UTC - 6 hrs

Pretty close. qw() is a shortcut for creating arrays out of a list of words without having to use quotes or commas. Could also have been written like

@ret = ('Ryan', 'Stille', 'M');

or

$ret[0] = 'Ryan';
$ret[1] = 'Stille';
$ret[2] = 'M';

Posted by Ryan Stille on Jan 29, 2007 at 09:23 AM UTC - 6 hrs

You can do this with Python as well. The syntax is nearly identical. (actually one line shorter)

def some_function():
return 1,2,3

one, two, three = some_function()

print one
print two
print three

Posted by brian on Jan 29, 2007 at 10:32 AM UTC - 6 hrs

I know because of the web it didn't show up, but the whitespace you'll see in brian's example if you were to view source is significant.

Basically, you'll need to indent the return statement (He did, you just can't see it on the web).

Maybe I need to do something to preserve all whitespace in the comments, not just the crlf.

Posted by Sam on Jan 29, 2007 at 11:33 AM UTC - 6 hrs

Also, my understanding is that Ruby and Python are very similar to each other, so I wouldn't be surprised to learn that other code we see in each of them may be almost identical.

Posted by Sam on Jan 29, 2007 at 11:39 AM UTC - 6 hrs

how can return more one value in c language

with our using structure some thing like that

get me as erly as possible

try to explain with example

Posted by chandu on Oct 29, 2007 at 01:34 PM UTC - 6 hrs

Chandu - You would pass in any number of pointers, dereference them and set the values they point to. Ex:

int addTwoNumbersTwice(int w, int x, int y, int z, int *sum1, int *sum2)
{
*sum1 = w + x;
*sum2 = y + z;
}

As you can see, it's not really returning the values, it is setting them on the outside. You would call it like:

int *sum1, *sum2;
addTwoNumbersTwice(1,2,3,4,sum1,sum2);

I didn't test. I'll leave that to you.

Posted by Sam on Oct 29, 2007 at 01:49 PM UTC - 6 hrs

Why not use Ref or Out in C#

or go through this link
http://kunaldotnet.blogspot.com/2010/08/get-more-t...

Posted by ASP Dot Net Tips With C Sharp on Aug 01, 2010 at 08:13 AM UTC - 6 hrs

Leave a comment

Leave this field empty
Your Name
Email (not displayed, more info?)
Website

Comment:

Subcribe to this comment thread
Remember my details
Google
Web CodeOdor.com

Me
Picture of me

Topics
.NET (19)
AI/Machine Learning (14)
Answers To 100 Interview Questions (10)
Bioinformatics (2)
Business (1)
C and C++ (6)
cfrails (22)
ColdFusion (78)
Customer Relations (15)
Databases (3)
DRY (18)
DSLs (11)
Future Tech (5)
Games (5)
Groovy/Grails (8)
Hardware (1)
IDEs (9)
Java (38)
JavaScript (4)
Linux (2)
Lisp (1)
Mac OS (4)
Management (15)
MediaServerX (1)
Miscellany (76)
OOAD (37)
Productivity (11)
Programming (168)
Programming Quotables (9)
Rails (31)
Ruby (67)
Save Your Job (58)
scriptaGulous (4)
Software Development Process (23)
TDD (41)
TDDing xorblog (6)
Tools (5)
Web Development (8)
Windows (1)
With (1)
YAGNI (10)

Resources
Agile Manifesto & Principles
Principles Of OOD
ColdFusion
CFUnit
Ruby
Ruby on Rails
JUnit



RSS 2.0: Full Post | Short Blurb
Subscribe by email:

Delivered by FeedBurner