Regarding function default return value?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Shilpa Sethi
    New Member
    • Aug 2010
    • 12

    Regarding function default return value?

    e.g. The func returns 11 on this execution? Can anyone elucidate its reason?

    Code:
    func()
    {
       printf("Hello World");
    }
    int main()
    {
        func()
        return 0;
    }
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Where are you seeing 11?

    All I see is "Hello world". True, printf returns the number of characters displayed which in this case is 11 but you are not capturing the 11.

    Comment

    • Shilpa Sethi
      New Member
      • Aug 2010
      • 12

      #3
      Ok, Let me modify the code to make it more understandable:

      Code:
       func()
       {
          printf("Hello World");
       }
       int main()
       {
           printf("\n%d",func());
           return 0;
       }
      Now this code will print 11 when printf of main will execute. Can anybody please elucidate the reason for the same.

      Comment

      • johny10151981
        Top Contributor
        • Jan 2010
        • 1059

        #4
        This is really interesting.

        You are getting the string length of the string that is passed to printf function.

        but not all printf. if you add 10 more printf in this case you will get only the last string length in the last printf function.

        I am not sure but may be i read or heard from somewhere that function use AX/EAX register to return value.

        If this information is true then it is very much possible that printf function use AX/EAX register but it do not reset the register. that is why in return you are getting 11 which is your string length.

        Comment

        • johny10151981
          Top Contributor
          • Jan 2010
          • 1059

          #5
          after posting the previous answer i made a search on google. Here what I found on x86 calling convention

          It might give you a good idea

          Comment

          • Banfa
            Recognized Expert Expert
            • Feb 2006
            • 9067

            #6
            func is declared without a return type. That means this is C and it is using default int as the return type.

            That has nothing to do with the return value. Because func does not return a value then the return value of func is whatever happened to be in the location that carries the return value at the time the function returned.

            Since the last thing func does is call printf and printf sets an int return value correctly.

            Since x86 uses a register to return the return value and nothing has interfered with that register between the printf call and func returning when func returns the return value of the printf statement is still in the register and so that appears as the return value of func.

            Relying on this sort of behaviour is extremely poor practice.

            Comment

            • donbock
              Recognized Expert Top Contributor
              • Mar 2008
              • 2427

              #7
              You ought to have gotten a compiler error that func wasn't returning an explicit value. Are you sure there wasn't an error or warning message?

              Comment

              • johny10151981
                Top Contributor
                • Jan 2010
                • 1059

                #8
                I compiled on GCC it didn't gave any error message warning. compiled clean

                Comment

                • donbock
                  Recognized Expert Top Contributor
                  • Mar 2008
                  • 2427

                  #9
                  If you invoke GCC from the command line, then add -Wall, othewise explore your IDE and enable all warnings. You should be getting an error at the end of func.

                  However, the following code would not necessarily produce an error:
                  Code:
                  void func(void) {
                     printf("Hello World");
                     }
                  
                  int main(void) {
                     printf("%d\n", func());
                     }
                  In your example, func is declared without a return type (which means it defaults to returning int) but doesn't have a return statement. It is the missing return statement that I expect to produce a warning message. In my version, func is declared to return void, so no return statement is expected; thus no warning. In either case, main will print some number (I'll take your word that it is 11). That's because the format string doesn't match the argument type. It is not the compiler's responsibility to catch this kind of bug (although some will) because the types of the value arguments are implied by the format string, not specified by the function prototype. (For example, printf("%d\n", 4.5); will have interesting results, but won't produce a warning.)

                  (However, the compiler might complain about passing a void function call as an argument because there wouldn't be a value to pass.)

                  Comment

                  • Banfa
                    Recognized Expert Expert
                    • Feb 2006
                    • 9067

                    #10
                    Do you use the "-Wall" and preferably "-Wall -pedantic" switches? If not get in the habit of using them, it switches on all warnings and forces standard compliance.

                    Adding in a #include stdio.h to the supplied source, and using a ; with mingw 4.4.0 without -Wall I get no errors but with -Wall I get

                    Code:
                    bytes.c:4: warning: return type defaults to 'int'
                    bytes.c: In function 'func':
                    bytes.c:6: warning: control reaches end of non-void function
                    First warning tells of the rather out-dated use of default int return and the second of reaching the end of a function that returns a value without having a return statement.

                    Comment

                    • johny10151981
                      Top Contributor
                      • Jan 2010
                      • 1059

                      #11
                      the command i have used to test was like this:

                      Code:
                      #gcc -o rt.o rt.c
                      and it didnt generate any error.

                      Comment

                      • johny10151981
                        Top Contributor
                        • Jan 2010
                        • 1059

                        #12
                        Banfa,
                        You replied by the time i was writing the reply.
                        got the point

                        Comment

                        Working...