Win a copy of The Business Blockchain this week in the Cloud forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why is a function/method not allowed to return multiple parameters while taking in multiple params?

 
JonathanC Anand
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why is a function/method not allowed to return multiple parameters while taking in multiple parameters?

Why not?

 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65335
97
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sometimes the answer is as simple as: that's just not the way the language is designed.
 
Simon Roberts
Author
Ranch Hand
Posts: 170
7
Java Linux Netbeans IDE
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JonathanC Anand wrote:Why is a function/method not allowed to return multiple parameters while taking in multiple parameters?

Why not?

public (String, Integer) processData (String str, Integer val) {
Integer value;
String code;
// Do something
return (code, value);
}


1) Languages, so far as my experience goes, have never done this, so it would cause surprise
2) You don't need to (though this is true of params too) as you can return an aggregate type (object)
3) What would the syntax look like for assigning the result to something?

int a, b;
(a, b) = doStuff();

maybe, but that goes back to point 1) it's not what programmers generally expect.

I suspect there's a point 4) which is that mathematicians (I'm not one!) generally seem to treat functions/algorithms as producing one thing. If the thing is a matrix (i.e. array) that's fine, but it's one thing.

Just thoughts...
Cheers,
Simon

 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi JonathanC Anand,

First of all, a warm welcome to CodeRanch!

JonathanC Anand wrote:Why is a function/method not allowed to return multiple parameters while taking in multiple parameters?

Using generics you can easily create a container class which can be used to return "multiple" parameters from a method/function. And because it uses generics you can reuse it for every combination of parameters

Here's a class which can hold 2 values

And then the processData method can use this class very easily (with very little effort and change)

And you could even reuse the Tuple2 class to create a tuple which can hold for example 4 values

Hope it helps!
Kind regards,
Roel

(Disclaimer: creating your own generic classes is not on the OCA exam, I just used it here for demonstration purposes only)
 
Campbell Ritchie
Sheriff
Posts: 51332
86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:Sometimes the answer is as simple as: that's just not the way the language is designed.
Some languages do permit multiple returns. But great care is needed because multiple return is error‑prone. Bear is right: that is how the language was designed. RDN has shown you how you can mimic multiple return.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12263
36
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Simon Roberts wrote:I suspect there's a point 4) which is that mathematicians (I'm not one!) generally seem to treat functions/algorithms as producing one thing. If the thing is a matrix (i.e. array) that's fine, but it's one thing.

By definition, a mathematical function can only return one thing for a given set of inputs (and must always return the same output for the same input every time).
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Some languages do permit multiple returns. But great care is needed because multiple return is error‑prone.


Just out of interest, what about multiple returns do you consider error prone? I've used them extensively in Python (and a little in C++) with no issue. The only things I can think of that you might say are actually the result of the dynamic typing rather than the multiple returns.
 
Joe Harry
Ranch Hand
Posts: 10128
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tuples or rather anonymous types is what comes to my mind!
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We probably need a definition of what counts as multiple returns. I'm not aware of any formal definition (i'm sure someone will correct me if I'm wrong).

You could count arrays or collections as multiple returns, but that doesn't seem very helpful. Personally I don't consider it as multiple returns unless you can 'unpack' the variables in the same statement as the method call.
 
Piet Souris
Rancher
Pie
Posts: 1511
32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:(...)
By definition, a mathematical function can only return one thing for a given set of inputs (and must always return the same output for the same input every time).

There are stochastic functions, where, given some input, the output can vary. Drawing from
some population, throwing a die, Wiener Processes, to name a few, although even then it depends
on your definition of 'multiple answers'.

Anyway, with Java 8 and a functional interface, you have all the liberty one can ask for.
For instance:

You can then have all kind of input and output, Tuples, Triples, Void, whatever.

The question is: is this 'f42' a function that returns 'multiple things'?

Greetz,
Piet
 
Campbell Ritchie
Sheriff
Posts: 51332
86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike. J. Thompson wrote: . . . Just out of interest, what about multiple returns do you consider error prone? . . .
It is very easy to get them out of order, as you can with multiple arguments to a method. Obviously strong typing helps.

I actually have a language which supports multiple return; because it is strongly typed it is easy to get a compiler error. But getting ijk out of order will cause a logic error.
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Mike. J. Thompson wrote: . . . Just out of interest, what about multiple returns do you consider error prone? . . .
It is very easy to get them out of order, as you can with multiple arguments to a method. Obviously strong typing helps.

I actually have a language which supports multiple return; because it is strongly typed it is easy to get a compiler error. But getting ijk out of order will cause a logic error.


Ah yes, I see what you mean. As with all things I guess its possible to do it badly, It needs to be simple, intuitive and properly documented. Passing multiple parameters to a method suffers from exactly the same problem, especially in Java. I hope named parameters get introduced in a later version!
 
Campbell Ritchie
Sheriff
Posts: 51332
86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I didn't explain it very well at first, did I. But success at last.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic