return statement check

Discussion in 'C++' started by Nan Li, Oct 10, 2005.

  1. Nan Li

    Nan Li Guest

    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
     
    Nan Li, Oct 10, 2005
    #1
    1. Advertising

  2. Nan Li

    lallous Guest

    "Nan Li" <> wrote in message
    news:...
    > 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
     
    lallous, Oct 10, 2005
    #2
    1. Advertising

  3. > 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
     
    Niels Dybdahl, Oct 10, 2005
    #3
  4. Nan Li

    Ian Guest

    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
     
    Ian, Oct 10, 2005
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    3
    Views:
    811
  2. Jay McGavren
    Replies:
    11
    Views:
    1,149
    Alan Krueger
    Jan 16, 2006
  3. Seong-Kook Shin
    Replies:
    1
    Views:
    498
    Richard Bos
    Jun 18, 2004
  4. tedsuzman
    Replies:
    2
    Views:
    7,098
    Michel Claveau, résurectionné d'outre-bombe inform
    Jul 21, 2004
  5. Greenhorn
    Replies:
    15
    Views:
    839
    Keith Thompson
    Mar 6, 2005
Loading...

Share This Page