473,396 Members | 1,877 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.

return statement check

Hello, all,
Does C/C++ ever check the return statement for a non-void function?
Look at my simple code below

int foo()
{

}

int main()
{
foo();
}

[nan@athena test]$ g++ foo.cpp
[nan@athena test]$ g++ -Wall foo.cpp
foo.cpp: In function `int foo()':
foo.cpp:4: warning: control reaches end of non-void function

There is only a warning, not an error.

Howver, in the java land, it is checked.

public class Main
{
private static int foo() {
}

public static void main( String [] args ) {
foo();
}
}

[nan@athena test]$ javac Main.java
Main.java:4: missing return statement
}
^
1 error
Is there any particular reason why C/C++ doesn't check the return
statement?

Thanks,
Nan

Oct 10 '05 #1
3 2214
"Nan Li" <na******@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
Hello, all,
Does C/C++ ever check the return statement for a non-void function?
Look at my simple code below

int foo()
{

}

int main()
{
foo();
}

[nan@athena test]$ g++ foo.cpp
[nan@athena test]$ g++ -Wall foo.cpp
foo.cpp: In function `int foo()':
foo.cpp:4: warning: control reaches end of non-void function

There is only a warning, not an error.

Howver, in the java land, it is checked.

public class Main
{
private static int foo() {
}

public static void main( String [] args ) {
foo();
}
}

[nan@athena test]$ javac Main.java
Main.java:4: missing return statement
}
^
1 error
Is there any particular reason why C/C++ doesn't check the return
statement?

Thanks,
Nan


Hello

If you use MS VC++ 7 with its default compiler settings, it will generate an
error when not returning a value for in functions other than in main()

--
Elias
Oct 10 '05 #2
> Is there any particular reason why C/C++ doesn't check the return
statement?


Early versions of C did not have the void type...

Niels Dybdahl
Oct 10 '05 #3
Ian
Nan Li wrote:
Hello, all,
Does C/C++ ever check the return statement for a non-void function?
Look at my simple code below

There is no requirement to do so. The short answer as to why is that it
can't always determine if the function will return (it might go in an
endless loop, throw..).

Ian
Oct 10 '05 #4

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

Similar topics

94
by: John Bailo | last post by:
The c# *return* statement has been bothering me the past few months. I don't like the fact that you can have different code paths in a method and have multiple return statements. To me, it...
3
by: Test | last post by:
Sorry, some people may have asked this question before. It is really hard to find relevant articles about this topic on the web using key words. I know it is not recommended to use return...
3
by: Marlene Stebbins | last post by:
My program has code like this: bigint bigSub(bigint minuend, bigint subtrahend) { bigint bigdiff; bigInit(&bigdiff); if(((cmp_ops(minuend.number, subtrahend.number))==1) && minuend.sign...
6
by: lovecreatesbeauty | last post by:
I ever missed a `return' statement when write a function `int HighDigit(Num)' to get the highest digit of an integer. But even if the `return' statement is ignored the function still can obtain...
15
by: Nerox | last post by:
Hi, If i write: #include <stdio.h> int foo(int); int main(void){ int a = 3; foo(a); }
13
by: jimjim | last post by:
Hello all, I ve come across the following code fragment and I was wondering why is the copy ctr called on return (rather than just returning the string statement obj. TIA string...
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?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
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
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.