Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is it necessary to include the return type in a function definition even if it's specified in the declaration?

Tags:

c++

When you define a function in a separate cpp file, why is the return type necessary to indicate which specific function you would like to define if the compiler knows that a method cannot overload another method with the same signature who only differ by return type? Isn't foo::bar(params) enough to specify this?

Header file

class foo{
public:
void bar();
};

Source file

foo::bar() //equivalent to function declaration? throws error
{...}

Thank you

like image 460
john doe Avatar asked May 13 '16 20:05

john doe


People also ask

Why do we use return type in function?

The result of a function is called its return value and the data type of the return value is called the return type. Every function declaration and definition must specify a return type, whether or not it actually returns a value.

Why is it necessary to pass and return values into a function?

Some functions' return value is relevant, another function may not be required to return anything. In general functions return values because they are relevant to the flow of your programme.

Is return type necessary?

It is never necessary, you can declare it whenever you like.

What is declaration and definition of a function when is declaration of a function necessary and when is it optional?

A function declaration tells the compiler about a function's name, return type, and parameters. A function definition provides the actual body of the function. The C standard library provides numerous built-in functions that your program can call.


2 Answers

I don't think there's any inherent reason that this couldn't be done, but using it would lead to inconsistency. After all, you don't have to declare a function if its definition is visible, but in that case, you'd have to include the return type. So function definitions that have a preceding declaration wouldn't need a return type, but function definitions that didn't, would. And if you removed the declaration you'd have to rewrite the definition. Altogether too much fiddling around.

EDIT: not to mention the convenience of having the return type in front of you when you're looking at the definition of the function.

like image 63
Pete Becker Avatar answered Sep 29 '22 11:09

Pete Becker


It doesn't seem to be a design goal of C++ to eliminate all possible redundancy from programs.

That is not even a good goal to have, at least if taken to extremes.

It is useful for the same information to be repeated in two places, and have an error be flagged if they conflict.

Some programming languages have redundancy built into basic control syntax, like while condition ; do .. if condition ; then .... end if; ... end while; The while must be terminated by end while, and if by end if. The compiler then knows exactly which one is missing. Proponents of such languages insist that this is better than a diagnostic about one of fifteen possible braces not being closed. :)

The return type being repeated in the definition is also helpful to the programmer in another way: the programmer doesn't have to jump to the declaration to see what the return type is.

It would actually take extra work to remove the return type in some cases. Declarations in headers can be produced by exact copy and paste. Or even with automated tools. You wouldn't want a header-generating tool to strip away the return type from your definitions.

We should question the necessity of declarations in the first place. The definition of a function is the primary artifact; the declaration is something that is just factored out for the sake of a particular compilation model. In other words, the question is "given that we have a function definition, why do we have to repeat any part of it anywhere".

like image 23
Kaz Avatar answered Sep 29 '22 12:09

Kaz