473,772 Members | 2,564 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2303
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

iEYEARECAAYFAkk QX5QACgkQPFW+cU iIHNottACeIqONj HogSOIhwP0xxm3S obgI
WPAAni5YA8z2fF6 f2aACjBTEREbtQ6 bv
=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...@gmai l.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(ch ar const*);
int foo()
{
if (rand() < 10)
return 777;
report_error("I llegal 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...@gmai l.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

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

Similar topics

2
3056
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 to have an easier time understanding what I do. Therefore this weekend I'm going to spend 3 days just writing comments. Before I do it, I thought I'd ask other programmers what information they find useful. Below is a typical class I've...
5
3046
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) pointer, or reference Date: 27 Aug 2005 15:13:23 -0400 From: Neal Coombes <nealc@trdlnk.com> Organization: Aioe.org NNTP Server To: (Usenet) Newsgroups: comp.lang.c++.moderated
16
1999
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 see how the return value comes to play here. Ex
20
3613
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 exit(n)) statement provide both function call and return features of the C programming language? /* headers omitted */ int main (void)
2
1923
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 The question may be silly or even meaning less but please............
12
2116
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
13720
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 Warn if any functions that return structures or unions are defined or called. (In languages where you can return an array, this also elicits a warning.) Easy enough to disregard, but I'm trying to understand the rationale.
7
2790
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
2485
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. Here's the approach I'm currently looking at. I throw exceptions only from constructors. Destructors, of course, do not throw exceptions. All other functions return a signed integer. The values are all stored in one large header file (as...
0
9621
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9454
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10264
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10106
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9914
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7461
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5355
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5484
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4009
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.