Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is return type before the function name?

The new C++11 standard adds a new function declaration syntax with a trailing return type:

// Usual declaration
int foo();

// New declaration
auto foo() -> int;

This syntax has the advantage of letting the return type be deduced, as in:

template<class T, class U>
auto bar(T t, U u) -> decltype(t + u);

But then why the return type was put before the function name in the first place? I imagine that one answer will be that there was no need for such type deduction in that time. If so, is there a reason for a hypothetical new programming language to not use trailing return type by default?

like image 316
authchir Avatar asked Oct 03 '12 02:10

authchir


People also ask

Why do we use return type before main function?

The return value of main() function shows how the program exited. The normal exit of program is represented by zero return value. If the code has errors, fault etc., it will be terminated by non-zero value. In C++ language, the main() function can be left without return value.

Why do we write return in function?

A return statement ends the execution of a function, and returns control to the calling function. Execution resumes in the calling function at the point immediately following the call. A return statement can return a value to the calling function. For more information, see Return type.

Is return type is important in function?

If you don't define a return type you are not defining a function. By definition a function declaration must have a return type. Think of forward declared functions. You always define a return type in both the forward declaration and the definition.

Should functions always return the same type?

To meet the following two rules. Rule 1 -- a function has a "type" -- inputs mapped to outputs. It must return a consistent type of result, or it isn't a function.


1 Answers

As always, K&R are the "bad guys" here. They devised that function syntax for C, and C++ basically inherited it as-is.

Wild guessing here: In C, the declaration should hint at the usage, i.e., how to get the value out of something. This is reflected in:

  • simple values: int i;, int is accessed by writing i
  • pointers: int *p;, int is accessed by writing *p
  • arrays: int a[n];, int is accessed by writing a[n]
  • functions: int f();, int is accessed by writing f()

So, the whole choice depended on the "simple values" case. And as @JerryCoffin already noted, the reason we got type name instead of name : type is probably buried in the ancient history of programming languages. I guess K&R took type name as it's easier to put the emphasis on usage and still have pointers etc. be types.

If they had chosen name : type, they would've either disjoined usage from declarations: p : int* or would've made pointers not be types anymore and instead be something like a decoration to the name: *p : int.

On a personal note: Imagine if they had chosen the latter and C++ inherited that - it simply wouldn't have worked, since C++ puts the emphasis on types instead of usage. This is also the reason why int* p is said to be the "C++ way" and int *p to be the "C way".

If so, is there a reason for a hypothetical new programming language to not use trailing return type by default?

Is there a reason to not use deduction by default? ;) See, e.g. Python or Haskell (or any functional language for that matter, IIRC) - no return types explicitly specified. There's also a movement to add this feature to C++, so sometime in the future you might see just auto f(auto x){ return x + 42; } or even []f(x){ return x + 42; }.

like image 124
Xeo Avatar answered Oct 26 '22 03:10

Xeo