Why can a function in C + + or Java not return multiple values, like MATLAB and some scripting languages?

Source: Internet
Author: User
Tags intel pentium
Do not say anything by reference or pointer implementation, that is to return a value

Reply content:

This is the worst std::tie ever seen. The previous answer would like to mention that the language of "really multiple return Values" (implementations) is not missing.
True "return multi-value" language has for example lua:programming in lua:5.1
It takes the flexibility of the operand stack to return all the return values on the operand stack. The most significant difference between this and "returning a tuple" is that the tuple is an extra entity, and LUA does not have an extra entity to wrap multiple return values.

As for the original question, I agree with Allen's answer, we can casually speculate, but the most primitive design decisions in the end is only the designers know.

The most common way to return a multivalued value in C + + is either to return a tuple or, more traditionally, to return a multivalued value (in other words, an incoming & a parameter or an explicit pointer) with an out parameter. Std::tuple func1 ()
{
return {n};
}

void Func2 ()
{
int A, B;
Std::tie (A, B) =func1 ();
}
Let's do this. One is more, multiple values, that is, a collection. Objection is raised against the remark that the return of multiple values is merely a syntactic sugar.
Returning multiple values in terms of implementations, may be syntactic sugars, potentially performance-enhancing tools

In the case of syntactic sugars, implementation onEquivalent to passing a struct * in C + +, then return value is stored in a struct, and the caller chooses to use it. Just support from a syntactic level is simpler (?). The wording

Performance improvements are more complex and require an introduction to the assembly and some basic algorithms (operations).
Simply put, if we pass a struct *, the compiler does not necessarily optimize the memory read-write part, then instead of using all the return values, this function will write full struct, instead of performance loss.
If the implementation is using a register or other platform feature to return multiple values, there is no problem.

The following lengthy
----------------------------
In C + + and most languages, we have two operators--/and%, divisible (DIV) and modulo (remainder/mod)
On the Intel platform, these two operations are implemented with a single instruction, and here is an example of the unsigned form of idiv (the following reference page is searched with Bing):
Intel Pentium CPU Instruction Set Reference
Written as Idiv Oprand

This instruction will be composed of r2:r0 double word as dividend, operand oprand as divisor, divide calculation, the result quotient deposit R0, remainder deposit R2.
In C + +, if you write:
unsignedc=a/b;unsignedr=a%b;
No, why not. Figure Simple Chant
But this is really an interesting phenomenon: most of the programming language is modeled after the definition of mathematical function, a function should only return a value, to mathematics learning, and the famous matrix lab Matlab, but supports the function of multiple return values. function only returns a value, the function call is an expression that can be used as a parameter to invoke other functions:
g(f(x))h(f(y))
Go supports the return of multiple values, the function signature is written in more than one type, not a tuple, and the implementation is similar to LUA, does not require additional packaging, not syntax sugar (because it does not return a struct, there is no corresponding sugar-free practice). So it has nothing to do with static types, scripting languages, just different design concepts.
Go can write like this:
package   Main  func  main   ()  {  FN  :=  func   ( a   b  int  )   ( int   int  )  { return  a   b  }  FN   ( fn   ( 1   2   }   
Think @yf Zpy is talking about grammatical sugars.
You can simply look at the Coffeescript code before and after the compilation.
Coffee code:
[A, B, c] = fun ()
The compiled JS code:
var A, B, c, _ref;_ref = Fun (), a = _ref[0 ], B = _ref[1], c = _ref[2];  
Return multiple values? It's just a syntactic sugar that returns a tuple and then assigns the value to be deconstructed. C + + can still do:

 #include 
    
      #include 
     
       #include 
      
        using namespace Std;tuple 
       
         Get_stud    ENT (int id) {if (id = = 0) return make_tuple (3.8, ' A ', "Lisa Simpson");    if (id = = 1) return Make_tuple (2.9, ' C ', "Milhouse Van Houten");    if (id = = 2) return make_tuple (1.7, ' D ', "Ralph Wiggum"); Throw invalid_argument ("id");}    int main () {double gpa1;    Char grade1;    String name1;    Tie (GPA1, grade1, name1) = Get_student (1);
      return 0;} 
        
       
      
      
  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.