Like Tree1Likes

Calling a function with same name but different return type!!!

This is a discussion on Calling a function with same name but different return type!!! within the C Programming forums, part of the General Programming Boards category; Hi, I have a query. I have the following code : Code: #ifndef TEST_H_INCLUDED #include"test.h" #endif extern int func(); int ...

  1. #1
    Registered User poornaMoksha's Avatar
    Join Date
    Sep 2011
    Location
    India
    Posts
    41

    Calling a function with same name but different return type!!!

    Hi,

    I have a query. I have the following code :
    Code:
    #ifndef TEST_H_INCLUDED 
     #include"test.h" 
     #endif 
      
     extern int func(); 
      
     int main(void) 
     { 
         int ret = func(); 
         printf("\n Executed!!! with ret[%d] \n", ret); 
         return ret; 
     }
    In the above code, I have declared function func() as returning integer and made a call to it in function main() expecting a return from it. This function func() is declared separately in the another file :

    Code:
    #ifndef TEST_H_INCLUDED 
     #include"test.h" 
     #endif 
      
     void func() 
     { 
         printf("\n Inside func()\n"); 
     }
    Here I have defined the functon func() but please note that the signature of this function here says that it returns nothing (void).

    When I compile the above two files with gcc on linux system, I see no compilation or Linking errors and the executable works fine with func() being called.

    Just wanted to ask how is this possible as the declaration and definition of func() has different signatures?

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,618
    Formally, what you are getting is undefined behaviour. In practice, depending on how the compiler sets things up for function call, your code will call the void func() and the return value you get could be anything.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Sunshine, and read this, this, and this before posting again.

  3. #3
    Registered User poornaMoksha's Avatar
    Join Date
    Sep 2011
    Location
    India
    Posts
    41
    But when I put the definition of func() in the same file from where it is being called and remove the extern keyword from the declaration then I get compilation error. So if I see from a no error situation to an error situation only two things changed :

    1) Removed 'extern'
    2) Put the definition in the same file which contains declaration

    I am not able to conclude which of the above two changes are causing the impact and how?
    So don't you think there has to be a concrete reason for this behavior??
    Last edited by poornaMoksha; 11-17-2011 at 04:41 AM. Reason: missing word

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,618
    If the function definition is in the same file as the declaration, or the call point, then the compiler can spot the mismatch and complain. That is not possible if they are in separate files.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Sunshine, and read this, this, and this before posting again.

  5. #5
    Registered User poornaMoksha's Avatar
    Join Date
    Sep 2011
    Location
    India
    Posts
    41
    I just verified that If I introduce arguments to the function func() while defining it, then also the compiler or linker does not complain. I mean, if I change :

    Code:
    #ifndef TEST_H_INCLUDED 
     #include"test.h" 
     #endif 
      
     void func() 
     { 
         printf("\n Inside func()\n"); 
     }
    to

    Code:
    #ifndef TEST_H_INCLUDED 
     #include"test.h" 
     #endif 
      
     void func(int a ) 
     { 
         printf("\n Inside func()\n"); 
     }
    and keep the other file same. Then also compiler or linker does not complain.

    Is it that only the address is used while calling if function is defined in a separate file? And since name of the function(which represents address) remains the same so we can change return type and arguments?

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by poornaMoksha View Post
    Is it that only the address is used while calling if function is defined in a separate file? And since name of the function(which represents address) remains the same so we can change return type and arguments?
    Striaght up... what is it with all these people just lately who seem obsessed with finding loopholes in their compilers?

    When you do stuff like this you are playing with "undefined behavior" which can mean two things...
    1) The result will be unpredictable ... Which function actually gets called???
    2) It will probably only work on your compiler, others may do something different.

    C does not support function or operator overloading (that's C++)... so don't bother wasting your time trying to fake it.

    The fact that your compiler/linker doesn't return errors really just tells me that you need to turn up the warning level on your compiler and linker...

  7. #7
    Registered User poornaMoksha's Avatar
    Join Date
    Sep 2011
    Location
    India
    Posts
    41
    I am using the '-Wall' option. Do I need to turn up some more options in order to see some warnings or errors?

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by poornaMoksha View Post
    I am using the '-Wall' option. Do I need to turn up some more options in order to see some warnings or errors?
    Nope that should do it... provided you're on the newest version of GCC...

    Looks to me like you found a loophole alright...
    However, I will leave you with rule #1 in the programmer's book of giant screwups...

    1) "Compiles" does not mean "Works".

  9. #9
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,523
    Quote Originally Posted by poornaMoksha View Post
    I am using the '-Wall' option. Do I need to turn up some more options in order to see some warnings or errors?
    Seems not..
    This scenario may be a left over from the lint days.

    Anyway...@poornaMoksha: Nice username. I hope you know what the phrase means !
    Manasij Mukherjee | gcc-4.9.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  10. #10
    Registered User poornaMoksha's Avatar
    Join Date
    Sep 2011
    Location
    India
    Posts
    41
    @CommonTater

    Actually I am not trying to fake function overloading here but I was just curious over how come main() which is declared to return an integer can be defined as 'void main()'...This led me to simulate the scenario using the function func() and since I did not get any error so i put up my doubt in this thread.

  11. #11
    Registered User poornaMoksha's Avatar
    Join Date
    Sep 2011
    Location
    India
    Posts
    41
    @manasij7479
    It means complete liberation

  12. #12
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,523
    Quote Originally Posted by poornaMoksha View Post
    @manasij7479
    It means complete liberation
    I know that...
    That is one of the few 'items' in our philosophy not corrupted by 'zealotry'.
    Manasij Mukherjee | gcc-4.9.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  13. #13
    Registered User poornaMoksha's Avatar
    Join Date
    Sep 2011
    Location
    India
    Posts
    41
    True...

  14. #14
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by poornaMoksha View Post
    @CommonTater

    Actually I am not trying to fake function overloading here but I was just curious over how come main() which is declared to return an integer can be defined as 'void main()'...This led me to simulate the scenario using the function func() and since I did not get any error so i put up my doubt in this thread.
    Yes, main() can also be declared void... but you do so at the risk of causing errors in the OS or Parent program.

    C return values are stored in a CPU register... a void func() does not place any known value in that register because it is assumed it's not needed... When a program with void main() returns, the OS gets garbage in that register. If the OS or Parent program actually tries to use that value (and, yes, it's a relatively common practice) you can get all kinds of strange results.

    Thus we come to rule #2... "Just because you CAN do something does not mean you SHOULD".

  15. #15
    Registered User poornaMoksha's Avatar
    Join Date
    Sep 2011
    Location
    India
    Posts
    41
    Thanks.
    I pretty much got my answer.
    Would like to thank all those who helped me here.
    Moderators may close the thread!!!

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to properly return MySQL results to a calling function?
    By Effenberg0x0 in forum C Programming
    Replies: 3
    Last Post: 06-09-2011, 04:42 AM
  2. Replies: 2
    Last Post: 11-03-2010, 08:00 AM
  3. Function return type
    By nedim in forum C++ Programming
    Replies: 2
    Last Post: 09-27-2009, 01:58 PM
  4. function with no return type
    By abhi_86 in forum C Programming
    Replies: 12
    Last Post: 11-25-2006, 11:48 AM
  5. Replies: 6
    Last Post: 04-09-2006, 05:32 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21