473,396 Members | 1,804 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Forgetting to write a return in a function returning a value

Why doesn't my compiler (g++) flag something like this as an error:

int main()
{
}

?
Nov 4 '08 #1
12 2285
he*******@gmail.com writes:
>Why doesn't my compiler (g++) flag something like this as an error:
>int main()
{
}
Because main's special. Try the same thing in another function.

Nov 4 '08 #2
he*******@gmail.com wrote:
Why doesn't my compiler (g++) flag something like this as an error:

int main()
{
}

?
Because it's not an error, in fact is is absolutely correct. In C++ the
void argument is implicit for any function. For main, return 0 is
implicit if you don't write an explicit return. Also for main the only
two proper ways to write it are:

int main()
{
}

and

int main(int argc, char* argv[])
{
}

int main(void) is wrong.

--
George Kettleborough
Nov 4 '08 #3
G Kettleborough wrote:
he*******@gmail.com wrote:
>Why doesn't my compiler (g++) flag something like this as an error:

int main()
{
}

?

Because it's not an error, in fact is is absolutely correct. In C++
the void argument is implicit for any function. For main, return 0 is
implicit if you don't write an explicit return. Also for main the only
two proper ways to write it are:

int main()
{
}

and

int main(int argc, char* argv[])
{
}

int main(void) is wrong.
No, it isn't. It's ugly, but it's not wrong.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Nov 4 '08 #4
On 4 Nov, 10:48, t...@eng.cam.ac.uk (Tim Love) wrote:
hector...@gmail.com writes:
Why doesn't my compiler (g++) flag something like this as an error:
int main()
{
}

Because main's special. Try the same thing in another function.
Following your suggestion, I tried the following:

int f()
{
}

int main()
{
return f();
}

This compiles okay too. So I don't think what I'm describing is
specific to main.
Nov 4 '08 #5
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Jensen Somers wrote:
Because in C++ main() is the only function which does not explicitly
needs a return statement [1]. It may also depend on the type of warning
level you specified.
Indeed, it depends. If you had used flag -Wall (in gcc), you would have
got warning for int f().

Pawel Dziepak
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)
Comment: Using GnuPG with Fedora - http://enigmail.mozdev.org

iEYEARECAAYFAkkQX5QACgkQPFW+cUiIHNottACeIqONjHogSO IhwP0xxm3SobgI
WPAAni5YA8z2fF6f2aACjBTEREbtQ6bv
=hpct
-----END PGP SIGNATURE-----
Nov 4 '08 #6
he*******@gmail.com wrote:
On 4 Nov, 10:48, t...@eng.cam.ac.uk (Tim Love) wrote:
>hector...@gmail.com writes:
>>Why doesn't my compiler (g++) flag something like this as an error:
int main()
{
}
Because main's special. Try the same thing in another function.

Following your suggestion, I tried the following:

int f()
{
}

int main()
{
return f();
}

This compiles okay too. So I don't think what I'm describing is
specific to main.
That's compiler-specifc. Comeau complains:
warning: missing return statement at end of non-void function "f"
This
int f() {return 0;}

int main() {}

OTOH, compiles without warning.

Schobi
Nov 4 '08 #7
Victor Bazarov wrote:
>
No, it isn't. It's ugly, but it's not wrong.

V
Well the standard defines only those two. Or do you mean that int
main(void) is simply the explicit version of of int main() so equivalent?
--
George Kettleborough
Nov 4 '08 #8
On 4 Nov., 11:39, hector...@gmail.com wrote:
Why doesn't my compiler (g++) flag something like this as an error:

int main()
{

}
As others have told you, main is special. It is not required to have
an explicit return, and if it returns via the end, this corresponds to
return 0. Don't ask me why that silly rule was implemented.

Now, take another function:

int foo() {}

Surprisingly this function is also ok: you just can't call it:
execution of f will lead to undefined behaviour. Why is this so?
Because it can be very difficult if not impossible to determine if a
function returns anything or not. Let's expand foo a little:

external void report_error(char const*);
int foo()
{
if (rand() < 10)
return 777;
report_error("Illegal random result in foo");
}

Now, this function might or not return, depending on report_error.
report_error might call exit, it might throw en exception or it might
just log the error and return. It might even do one of the three
things depending on some external state.

It is only when report_error returns, we get undefined behaviour.
Now, you could burden the programmer and require him to write a dummy
return-statement, but this might have its own problems: it adds dead
code to the function, and this can be a burden in some environments,
especially if the stuff returned is something more complicated than a
simple int and it might make code somewhat obfuscated (imagine the
call being replaced by a throw).

That said - if you've followed me so far - every decent compiler will
(or can be made to) warn about the missing return statement, and most
compilers also have the ability to treat certain warnings as errors,
so basically it is a question of setting your environment up in a way
that satisfies your requirements.

/Peter
Nov 4 '08 #9
he*******@gmail.com wrote:
On 4 Nov, 10:48, t...@eng.cam.ac.uk (Tim Love) wrote:
>hector...@gmail.com writes:
>>Why doesn't my compiler (g++) flag something like this as an error:
int main()
{
}
Because main's special. Try the same thing in another function.

Following your suggestion, I tried the following:

int f()
{
}

int main()
{
return f();
}

This compiles okay too. So I don't think what I'm describing is
specific to main.
Invoke it in conforming mode and you will.

--
Ian Collins
Nov 4 '08 #10
George Kettleborough wrote:
Victor Bazarov wrote:
>No, it isn't. It's ugly, but it's not wrong.

V

Well the standard defines only those two. Or do you mean that int
main(void) is simply the explicit version of of int main() so equivalent?
Yes, I meant that the forms '()' and '(void)' are equivalent. See
8.3.5/2. In my [purist's] view, the '(void)' is an abomination, but
it's a hold-over from C, so it's legal. Perhaps you meant something
other than "ill-formed" when you wrote "wrong", then I apologise.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Nov 4 '08 #11
he*******@gmail.com wrote:
Why doesn't my compiler (g++) flag something like this as an error:

int main()
{
}

?
Because in C++ it is not an error, in a sense that it is not ill-formed.
(And no, in that regard 'main' is no different from any other
function). C++ language specification does not require compilers to
detect and diagnose execution paths that flow off the end of a
value-returning function without an explicit 'return' statement. In
general case, flowing off in such a context produces undefined behavior
(not in 'main' though, which is where is does indeed get special), but
the program still remains well-formed. In other words, it is your
responsibility to remember to include an explicit 'return' statement
into every possible control path of a value-returning function. Some
compilers might help you to detect the paths where you forgot to provide
a 'return', but in any case they are not required to do so.

--
Best regards,
Andrey Tarasevich
Nov 4 '08 #12
On Nov 4, 7:18 pm, Ian Collins <ian-n...@hotmail.comwrote:
hector...@gmail.com wrote:
On 4 Nov, 10:48, t...@eng.cam.ac.uk (Tim Love) wrote:
hector...@gmail.com writes:
Why doesn't my compiler (g++) flag something like this as an error:
int main()
{
}
Because main's special. Try the same thing in another function.
Following your suggestion, I tried the following:
int f()
{
}
int main()
{
return f();
}
This compiles okay too. So I don't think what I'm describing
is specific to main.
Invoke it in conforming mode and you will.
Not necessarily; it's undefined behavior. And compilers that
warn when in fact you can't reach the end can be very irritating
as well.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Nov 5 '08 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: lawrence | last post by:
I've been bad about documentation so far but I'm going to try to be better. I've mostly worked alone so I'm the only one, so far, who's suffered from my bad habits. But I'd like other programmers...
5
by: Neal Coombes | last post by:
Posted to comp.lang.c++.moderated with little response. Hoping for better from the unmoderated groups: -------- Original Message -------- Subject: Return appropriately by value, (smart)...
16
by: G Patel | last post by:
Hi, If I want to call functions that don't return int without declaring them, will there be any harm? I only want to assign the function(return value) to the type that it returns, so I don't...
20
by: lovecreatesbeauty | last post by:
Hello experts, Is the following code snippet legal? If it is, how can exit() do the keyword return a favor and give a return value to the main function? Can a function call (or only this...
2
by: mosesdinakaran | last post by:
Hi everybody, Today I faced a problem where I am very confused and I could not solve it and I am posting here.... My question is Is is possible to return a value to a particular function ...
12
by: huangshan | last post by:
hi all In what condition i need( or mast) a (templates)function return value by reference? can you give me a example thanks
13
by: markn | last post by:
Running some code through static analysis, I noticed that gcc will generate a warning if a function returns an aggregate, controlled with this flag (from the gcc manual): -Waggregate-return...
7
by: asit | last post by:
#include <stdio.h> //#include <stdlib.h> int main() { int c; printf("c before call=%d\n",c); c=message(); printf("c after call=%d\n",c); return 0;
4
by: barcaroller | last post by:
I am trying to adopt a model for calling functions and checking their return values. I'm following Scott Meyer's recommendation of not over-using exceptions because of their potential overhead. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.