function automatically returns the value

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • chellappa

    #1

    function automatically returns the value

    hi All,
    function automatically returns the value,am not used "return"
    i am using Linux -gcc complier
    please tell me.... what is problem...

    source
    =====
    #include <stdio.h>
    main()
    {
    int a,b,c,sum;
    printf("ENTER ANY THREE NUMBERS :\n");
    scanf("%d%d%d", &a,&b,&c);
    sum=calsum(a,b, c);
    printf("sum = %d\n", sum);
    }
    calsum(x,y,z)
    int x,y,z;
    {
    int d;
    d=x+y+z;
    }
    output
    =====
    ENTER ANY THREE NUMBERS :
    23
    23
    23
    sum = 69

    Thanks All

    by
    chellappa

  • Richard Heathfield

    #2
    Re: function automatically returns the value

    chellappa said:
    [color=blue]
    > hi All,
    > function automatically returns the value,am not used "return"
    > i am using Linux -gcc complier
    > please tell me.... what is problem...
    >
    > source
    > =====
    > #include <stdio.h>
    > main()
    > {
    > int a,b,c,sum;
    > printf("ENTER ANY THREE NUMBERS :\n");
    > scanf("%d%d%d", &a,&b,&c);
    > sum=calsum(a,b, c);
    > printf("sum = %d\n", sum);
    > }
    > calsum(x,y,z)
    > int x,y,z;
    > {
    > int d;
    > d=x+y+z;
    > }
    > output
    > =====
    > ENTER ANY THREE NUMBERS :
    > 23
    > 23
    > 23
    > sum = 69[/color]

    foo@bar:~/scratch> ./foo
    ENTER ANY THREE NUMBERS :
    TWENTY-THREE TWENTY-THREE TWENTY-THREE
    sum = 45768

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999

    email: rjh at above domain (but drop the www, obviously)

    Comment

    • Mike Wahler

      #3
      Re: function automatically returns the value

      "chellappa" <N.Chellappa@gm ail.com> wrote in message
      news:1132759896 .672034.17140@g 47g2000cwa.goog legroups.com...[color=blue]
      > hi All,
      > function[/color]
      [color=blue]
      > automatically[/color]

      accidentally
      [color=blue]
      > returns the value,am not used "return"
      > i am using Linux -gcc complier[/color]

      Then you're using a *very old* version, or have
      it configured to ignore serious errors, or use
      some very strange extension(s).
      [color=blue]
      > please tell me.... what is problem...[/color]

      Undefined behavior.
      [color=blue]
      > source
      > =====
      > #include <stdio.h>
      > main()
      > {
      > int a,b,c,sum;
      > printf("ENTER ANY THREE NUMBERS :\n");
      > scanf("%d%d%d", &a,&b,&c);
      > sum=calsum(a,b, c);
      > printf("sum = %d\n", sum);
      > }
      > calsum(x,y,z)
      > int x,y,z;[/color]

      This is archaic syntax, not valid standard C.

      calsum(int x, int y, int z) // C89

      int calsum(int x, int y, int z) // C99
      [color=blue]
      > {
      > int d;
      > d=x+y+z;
      > }[/color]

      Undefined behavior. return statement required.

      What probably happened for you was that the value of
      'd' just accidentally happened to get stored in a
      register which was also accessed by the calling
      function. The standard C language can't be used
      to explain the behavior because you don't have
      a standard C program. If you want to inquire
      about the quirks of gcc, see a gcc group.
      [color=blue]
      > output
      > =====
      > ENTER ANY THREE NUMBERS :
      > 23
      > 23
      > 23
      > sum = 69[/color]


      -Mike


      Comment

      • Thad Smith

        #4
        Re: function automatically returns the value

        chellappa wrote:[color=blue]
        >
        > hi All,
        > function automatically returns the value,am not used "return"
        > i am using Linux -gcc complier
        > please tell me.... what is problem...
        >
        > source
        > =====
        > #include <stdio.h>
        > main()
        > {
        > int a,b,c,sum;
        > printf("ENTER ANY THREE NUMBERS :\n");
        > scanf("%d%d%d", &a,&b,&c);
        > sum=calsum(a,b, c);
        > printf("sum = %d\n", sum);
        > }
        > calsum(x,y,z)
        > int x,y,z;
        > {
        > int d;
        > d=x+y+z;
        > }
        > output
        > =====
        > ENTER ANY THREE NUMBERS :
        > 23
        > 23
        > 23
        > sum = 69[/color]

        The problem is that your program works by accident, since calsum does
        not explicitly return a value. On another implementation it may not.
        In general, when your code doesn't conform to the standard (and
        implementation specifications) you get undefined behavior. That
        occurs with your program. It is particularly nasty because it may
        work in some cases, or implementations , and not others.

        A powerful technique for avoiding those cases is to enable all
        warnings and heed them. You should have gotten appropriate warnings
        for your code.

        --
        Thad

        Comment

        • Christopher Benson-Manica

          #5
          Re: function automatically returns the value

          Mike Wahler <mkwahler@mkwah ler.net> wrote:
          [color=blue]
          > Then you're using a *very old* version, or have
          > it configured to ignore serious errors, or use
          > some very strange extension(s).[/color]

          Or he's getting better at trolling...

          --
          Christopher Benson-Manica | I *should* know what I'm talking about - if I
          ataru(at)cybers pace.org | don't, I need to know. Flames welcome.

          Comment

          • Mike Wahler

            #6
            Re: function automatically returns the value


            "Richard Heathfield" <invalid@invali d.invalid> wrote in message
            news:dm24l5$rpd $2@nwrdmz03.dmz .ncs.ea.ibs-infra.bt.com...[color=blue]
            > chellappa said:
            >[color=green]
            >> hi All,
            >> function automatically returns the value,am not used "return"
            >> i am using Linux -gcc complier
            >> please tell me.... what is problem...
            >>
            >> source
            >> =====
            >> #include <stdio.h>
            >> main()
            >> {
            >> int a,b,c,sum;
            >> printf("ENTER ANY THREE NUMBERS :\n");
            >> scanf("%d%d%d", &a,&b,&c);
            >> sum=calsum(a,b, c);
            >> printf("sum = %d\n", sum);
            >> }
            >> calsum(x,y,z)
            >> int x,y,z;
            >> {
            >> int d;
            >> d=x+y+z;
            >> }
            >> output
            >> =====
            >> ENTER ANY THREE NUMBERS :
            >> 23
            >> 23
            >> 23
            >> sum = 69[/color]
            >
            > foo@bar:~/scratch> ./foo
            > ENTER ANY THREE NUMBERS :
            > TWENTY-THREE TWENTY-THREE TWENTY-THREE
            > sum = 45768[/color]

            I get output of "ELEVENTY JILLION AND SIX", followed by my
            speakers emitting the faint sound of coyotes howling
            in the distance. And my nose is beginning to itch.
            Maybe th%)v^ *H)^_()&./7[NO CARRIER]



            Comment

            • Keith Thompson

              #7
              Re: function automatically returns the value

              "Mike Wahler" <mkwahler@mkwah ler.net> writes:[color=blue]
              > "chellappa" <N.Chellappa@gm ail.com> wrote in message
              > news:1132759896 .672034.17140@g 47g2000cwa.goog legroups.com...[/color]
              [...][color=blue][color=green]
              >> source
              >> =====
              >> #include <stdio.h>
              >> main()
              >> {
              >> int a,b,c,sum;
              >> printf("ENTER ANY THREE NUMBERS :\n");
              >> scanf("%d%d%d", &a,&b,&c);
              >> sum=calsum(a,b, c);
              >> printf("sum = %d\n", sum);
              >> }
              >> calsum(x,y,z)
              >> int x,y,z;[/color]
              >
              > This is archaic syntax, not valid standard C.[/color]

              Yes, it's archaic, but it's still legal (except that C99 forbids
              implicit int). But of course there's no good reason not to use
              prototypes.

              --
              Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
              San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
              We must do something. This is something. Therefore, we must do this.

              Comment

              • Tatu Portin

                #8
                Re: function automatically returns the value

                chellappa wrote:
                [color=blue]
                > hi All,
                > function automatically returns the value,am not used "return"
                > i am using Linux -gcc complier
                > please tell me.... what is problem...
                >[/color]

                #include <stdio.h>
                main()
                {
                int a,b,c,sum;
                printf("ENTER ANY THREE NUMBERS :\n");
                scanf("%d%d%d", &a,&b,&c);
                sum=calsum(a,b, c);
                printf("sum = %d\n", sum);
                }

                calsum(x,y,z)
                int x,y,z;
                {
                int d;
                d=x+y+z;
                }

                In C, functions that have not defined return value,
                do return value of int.
                So, following prototypes are identical:

                int func (int x, char b);

                func (int x, char b);

                If you want to declare a function without return value,
                you use void:

                /* A function that doesn't return a value. */
                void func (int x, char b);


                Comment

                • Keith Thompson

                  #9
                  Re: function automatically returns the value

                  Tatu Portin <axel86@mbnet.f i> writes:[color=blue]
                  > chellappa wrote:[color=green]
                  >> hi All,
                  >> function automatically returns the value,am not used "return"
                  >> i am using Linux -gcc complier
                  >> please tell me.... what is problem...
                  >>[/color]
                  >
                  > #include <stdio.h>
                  > main()
                  > {
                  > int a,b,c,sum;
                  > printf("ENTER ANY THREE NUMBERS :\n");
                  > scanf("%d%d%d", &a,&b,&c);
                  > sum=calsum(a,b, c);
                  > printf("sum = %d\n", sum);
                  > }
                  >
                  > calsum(x,y,z)
                  > int x,y,z;
                  > {
                  > int d;
                  > d=x+y+z;
                  > }
                  >
                  > In C, functions that have not defined return value,
                  > do return value of int.
                  > So, following prototypes are identical:
                  >
                  > int func (int x, char b);
                  >
                  > func (int x, char b);[/color]

                  That's true in C90, but not in C99 -- and it's a bad idea in either.
                  [color=blue]
                  > If you want to declare a function without return value,
                  > you use void:
                  >
                  > /* A function that doesn't return a value. */
                  > void func (int x, char b);[/color]

                  Yes, of course, but since chellappa was assigning the result of the
                  function call to a variable, he obviously wanted the calsum() function
                  to return a value. The fix is not to change the function so it
                  returns void; the fix is to add a return statement (and to use proper
                  prototypes rather than the archaic, but still legal, pre-ANSI function
                  declarations, and to fix a few other problems).

                  The original question was why the incorrect code was still "working"
                  (i.e., the value of x+y+z was assigned to sum even though there was no
                  return statement). The general answer is that the program invokes
                  undefined behavior, and the observed behavior is as legitimate as any
                  other behavior. The specific answer probably has to do with how the
                  particular compiler uses CPU registers, but quite frankly there's
                  little reason to care.

                  --
                  Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                  San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
                  We must do something. This is something. Therefore, we must do this.

                  Comment

                  • Peter Nilsson

                    #10
                    Re: function automatically returns the value

                    Mike Wahler wrote:[color=blue]
                    > "chellappa" <N.Chellappa@gm ail.com> wrote in message[color=green]
                    > > #include <stdio.h>
                    > > main()
                    > > {
                    > > ...
                    > > sum=calsum(a,b, c);
                    > > ...
                    > > }
                    > > calsum(x,y,z)
                    > > int x,y,z;[/color]
                    >
                    > This is archaic syntax, not valid standard C.[/color]

                    As others have stated, it's valid C89.
                    [color=blue]
                    > calsum(int x, int y, int z) // C89[/color]

                    // comments are not valid C89. ;)
                    [color=blue]
                    > int calsum(int x, int y, int z) // C99
                    >[color=green]
                    > > {
                    > > int d;
                    > > d=x+y+z;
                    > > }[/color]
                    >
                    > Undefined behavior. return statement required.[/color]

                    The return statement is required because the calling function attempts
                    to
                    use the value.

                    C99 did not remove the 'feature' that, in general, non-void functions
                    are not
                    required to return a value.

                    --
                    Peter

                    Comment

                    • Tatu Portin

                      #11
                      Re: function automatically returns the value

                      Keith Thompson wrote:
                      [color=blue]
                      > Tatu Portin <axel86@mbnet.f i> writes:[color=green]
                      >> chellappa wrote:[color=darkred]
                      >>> hi All,
                      >>> function automatically returns the value,am not used "return"
                      >>> i am using Linux -gcc complier
                      >>> please tell me.... what is problem...
                      >>>[/color]
                      >>
                      >> #include <stdio.h>
                      >> main()
                      >> {
                      >> int a,b,c,sum;
                      >> printf("ENTER ANY THREE NUMBERS :\n");
                      >> scanf("%d%d%d", &a,&b,&c);
                      >> sum=calsum(a,b, c);
                      >> printf("sum = %d\n", sum);
                      >> }
                      >>
                      >> calsum(x,y,z)
                      >> int x,y,z;
                      >> {
                      >> int d;
                      >> d=x+y+z;
                      >> }
                      >>
                      >> In C, functions that have not defined return value,
                      >> do return value of int.
                      >> So, following prototypes are identical:
                      >>
                      >> int func (int x, char b);
                      >>
                      >> func (int x, char b);[/color]
                      >
                      > That's true in C90, but not in C99 -- and it's a bad idea in either.
                      >[color=green]
                      >> If you want to declare a function without return value,
                      >> you use void:
                      >>
                      >> /* A function that doesn't return a value. */
                      >> void func (int x, char b);[/color]
                      >
                      > Yes, of course, but since chellappa was assigning the result of the
                      > function call to a variable, he obviously wanted the calsum()
                      > function
                      > to return a value. The fix is not to change the function so it
                      > returns void; the fix is to add a return statement (and to use
                      > proper prototypes rather than the archaic, but still legal, pre-ANSI
                      > function declarations, and to fix a few other problems).
                      >
                      > The original question was why the incorrect code was still "working"
                      > (i.e., the value of x+y+z was assigned to sum even though there was
                      > no
                      > return statement). The general answer is that the program invokes
                      > undefined behavior, and the observed behavior is as legitimate as
                      > any
                      > other behavior. The specific answer probably has to do with how the
                      > particular compiler uses CPU registers, but quite frankly there's
                      > little reason to care.[/color]


                      Well, yes, and the main() could also return the number of characters
                      printed by printf (). Have to read more carefully.


                      Comment

                      • Mark McIntyre

                        #12
                        Re: function automatically returns the value

                        On Wed, 23 Nov 2005 18:17:04 GMT, in comp.lang.c , Keith Thompson
                        <kst-u@mib.org> wrote:
                        [color=blue]
                        >
                        >Yes, it's archaic, but it's still legal (except that C99 forbids
                        >implicit int). But of course there's no good reason not to use
                        >prototypes.[/color]

                        I guess one might be stuch with a pre-ansi compiler. Thats why gcc
                        have a website mind you...
                        --
                        Mark McIntyre
                        CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
                        CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt >

                        ----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
                        http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
                        ----= East and West-Coast Server Farms - Total Privacy via Encryption =----

                        Comment

                        • haroon

                          #13
                          Re: function automatically returns the value


                          Mike Wahler wrote:

                          [snip]
                          [color=blue]
                          > What probably happened for you was that the value of
                          > 'd' just accidentally happened to get stored in a
                          > register which was also accessed by the calling
                          > function.[/color]

                          on intel x86 it happens to be EAX. this functions returns 0 on my x86
                          and x86_64 machines because the line asm("xor %eax, %eax") clears the
                          eax contents.
                          /*CODE BEGINS*/
                          calsum(x,y,z)
                          int x,y,z;
                          {
                          int d;
                          d=x+y+z;
                          asm("xor %eax, %eax");
                          }
                          /*CODE ENDS*/

                          Comment

                          Working...