Why do functions in some popular languages ​​return only one type of result?

Why do functions in some popular languages ​​return only one type of result?

ie I mean why compilers give an error with the following syntax

public int int returnTwoIntegers(){ ....... ........ } 
+6
function stack syntax stack-trace memory
source share
13 answers

Daniel Weinreb, who worked on both the Lisp dialects (which had multiple return values) and Java (which is not the case), explains why :

When we added a return function with multiple values ​​to the Lisp machine Lisp (where it came from Common Lisp), I thought that the desire to avoid consing was one of the big motives. At that time, we did not have good GC technology, and writing Lisp programs to carefully minimize consing was very common.

Another reason was expressiveness: to make it clear that the function actually returned more than one result, instead of entering a small "type" of the list (for example, "two file descriptor lists and a logical expression ...").

In Java, I saw the definitions of small classes whose sole purpose is to return multiple values. The advantage of this approach is that the types and values ​​of the return values ​​are documented (at least if you choose clear names and put comments!). But this is pretty verbose, in my opinion.

When I first looked at the early Java specification, I was surprised to see that there was no way to return multiple values. I wanted to lobby by adding this, so I tried to come up with a usage example that was simple and very convincing. I could not do that! So I did not try to lobby this. Java designers quite clearly tried to avoid a lot of bells and whistles (and syntactic sugar), and, of course, much could be said for this.

+2
source share

Functions return only one value, because its "they" method invented it in the old days of assembler. Basically what happens is that the function pushes the return value onto the stack. Then the caller pops the value from the stack. If the function returned more values, the caller would not know how many values ​​for pop and the stack would be unbalanced (which would cause the program to crash).

+22
source share

The function (in the languages ​​mentioned in your tags) has a single output , but there should not be one value for this output . Your output can easily be a class that encapsulates any amount of data that you want.

Also note that you can also pass multiple input arguments by reference to your function, thereby increasing the number of returned outputs.

+18
source share

To simplify the question.

You can always return T[] , Pair<L,R> , Set<E> , etc. into these languages ​​to emulate the return of multiple values. This applies to most scenarios of return values ​​there.

You should not change the semantics of the language so much as to get so little.

Related Questions

  • Using java method to return multiple values?
  • Return multiple values ​​from C ++ function
  • Return multiple values ​​from function C
  • Method must return multiple values
  • Elegant ways to return multiple values ​​from a function
+10
source share

My best guess is that it is carried over from mathematics. There you can have a function with several parameters and a value, which is the result.

 f(x1, x2, ...) = y (the formula might be something like x1 + x2^2 + x3^3 + ...) 

Even if you want to map a multidimensional domain into a multidimensional range, you usually write like:

 f1(x1, x2, ...) = y1 (perhaps y1 = x1 + x2^1 + ... like above) f2(x1, x2, ...) = y2 (and y2 = x1 - x2^2 + x3^3 - x4^4 +...) ... 

since each y gets its own mathematical formula based on inputs, which in programming will simply translate into several functions, it makes sense to divide it as follows.

The problem sometimes is that the formulas for y1 and y2 can be very similar or rely on an iteration of the same or something else that sharing them together can make more sense. In python you can do

return a1, a2;

and then name it like

x, y = f ();

but in most languages ​​you can only return one variable. This does not stop you from making it a complex object with multiple values ​​included.

+6
source share

Go go supports multiple return values

+2
source share

I think that in most cases one return value is enough, and the mathematical concept of a function (or Lambda calculus) also has one return value, so it’s quite clear that the language developers are trying to adhere to the “standard”. In fact, having multiple functions that need multiple return values ​​can be confusing and can be a sign of poor design, for example. missing composite class.

The "classic" approach for returning multiple values ​​uses Tuples. Some languages ​​have a built-in Tuple type (e.g. Scala), but you can define it yourself in all languages ​​that support type parameters in some form (templates, generics ...). For Java, there is a JavaTuple lib.

+1
source share

A function should fill out a clearly defined task. Besides the fact that you can always return an array: maybe something was completely wrong with your software if you need to return different values ​​from different data types.

In addition, it is much easier for a person to read a function with a single return value.

+1
source share

Lua also supports multiple return values: you can, for example, write the following:

 function foo() return 1, 2, 3 end a, b, c, d = foo() 

after execution, a, b, c will hold the values ​​1, 2, 3, and d will be equal to nil. Similarly, you can do the following:

 function bar() return true, 2, 3 end if bar() then -- do something intelligent end 

the if statement in this case only works with the first returned value - if you need other values, you will have to store them in variables, as usual:

 a, b, c = bar() if a then -- do something intelligent end 

so you see: in Lua, all return values ​​that are not needed are thrown away (for example, 2, 3 in the if example).

+1
source share

The function returns an answer, why will it be created to give more answers? And this answer can be any data structure that you like, so it can contain many values ​​:)

0
source share

You can do this in perl:

 sub foo { return (10, 20); } my ($a, $b) = foo(); # sets $a to 10 and $b to 20 

And similar designs in other languages.

0
source share

This will help to read the source code. This is soooo humane-friendly this way! If this function would have more than one return value, this line cannot be written:

  if (SomeFunction(a, b) != SUCCESS) { // do error handling } 
0
source share

I think that we are talking about memory allocation and stack organization. The main thing is context switching during a function call. the functional frame is stored on the stack the frame contains the function variables, general registers and return values ​​I know that a lot

0
source share

All Articles