getting return value from function without return statement.

Discussion in 'C Programming' started by Seong-Kook Shin, Jun 18, 2004.

  1. Hi.

    Just curiocity,

    Because of pre-ANSI C, it is possible to have a function without specifying
    return type of a function (which makes the return type 'int', though) and
    give no 'return' statement. For example:

    foo(int a, int b)
    {
    int sum;
    sum = a + b;
    }

    void
    bar(void)
    {
    int i = foo(1, 2);
    ...
    }

    In above code, the 'i' variable in bar() will have no useful value.
    But what does the standard says? I overlooked the ISO C standard, but
    found no mention about this problem.

    Is this a kind of 'undefined bebavior'? Or 'unspecified behavior'?

    If there's wrong point on my explanation, please enlighten me. ;-)
    Or, could you give me the section number of ISO C document where it deals
    with such case?
    Seong-Kook Shin, Jun 18, 2004
    #1
    1. Advertising

  2. Seong-Kook Shin

    Richard Bos Guest

    (Seong-Kook Shin) wrote:

    > foo(int a, int b)
    > {
    > int sum;
    > sum = a + b;
    > }
    >
    > void
    > bar(void)
    > {
    > int i = foo(1, 2);
    > ...
    > }
    >
    > In above code, the 'i' variable in bar() will have no useful value.
    > But what does the standard says?


    That it invokes undefined behaviour (which is worse than having no
    useful value; it could crash, for example):

    # 12 If the } that terminates a function is reached, and the value of
    # the function call is used by the caller, the behavior is undefined.

    That's from 6.9.1.

    Richard
    Richard Bos, Jun 18, 2004
    #2
    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. Greenhorn
    Replies:
    15
    Views:
    784
    Keith Thompson
    Mar 6, 2005
  2. lovecreatesbeauty
    Replies:
    8
    Views:
    563
    Kevin D. Quitt
    Mar 15, 2005
  3. lovecreatesbeauty
    Replies:
    0
    Views:
    267
    lovecreatesbeauty
    Mar 14, 2005
  4. Tim Fröglich

    Getting ID, calling url, search for value, return value

    Tim Fröglich, Jan 7, 2006, in forum: ASP .Net Web Services
    Replies:
    1
    Views:
    126
    Josh Twist
    Jan 10, 2006
  5. alex
    Replies:
    3
    Views:
    514
    Richard Cornford
    Dec 28, 2006
Loading...

Share This Page