Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Omit return type in function prototype

from the C++ institute documentation ( an online course ):

return_type describes the type of result returned (delivered) by the function (e.g. we expect that the sine function will return a value of type float as int data is completely unusable in this context); you can use any of the C++ types as a return_type, including a very special type named void; a function of type void returns no result at all; we can say that such a function may have an effect but definitely has no result; if you omit the return_type, the compiler assumes that the function returns a value of type int

regarding to this example return_type function_name (parameters_list);


In this example:

my_function(int x) {
    return 4;
}

int main()
{
...
}

I get the following error: ISO C++ forbids declaration of 'my_function' with no type [-fpermissive]|


In this example:

my_function(int);    //Prototype


int main()
{
...
}

int my_function(int x)
{
    return 4;
}

I get the following error:expected constructor, destructor, or type conversion before ';' token


I did not find in the C++11 standard page 192 - function declaration something related to what i wanted to know (or maybe its just the fact that i did not understand).

Could you please explain when can be omitted the return_type? Is this a mistake? Or is some older version of C++?

like image 905
Cătălina Sîrbu Avatar asked Dec 08 '22 11:12

Cătălina Sîrbu


1 Answers

Could you please explain when can be omitted the return_type? Is this a mistake?

The return type may not be omitted in a regular function prototype. The resource you cited is very wrong to suggest otherwise. There is no rule in standard C++ that assumes a return type of int in a function prototype.

Or is some older version of C++?

Not of C++. C++ never allowed omitting the return type. But pre-standardized C (K&R C) did allow it and had an "implicit int" rule. And so some compilers offer an extension for compatibility with some really old C code.

But again, this isn't, nor ever was, standard C++.

like image 117
StoryTeller - Unslander Monica Avatar answered Dec 10 '22 01:12

StoryTeller - Unslander Monica