return statement check

N

Nan Li

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
 
L

lallous

Nan Li said:
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()
 
I

Ian

Nan said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top