Different Function Arguments

This is a discussion on Different Function Arguments within the C Programming forums, part of the General Programming Boards category; Hello. I would appreciate very much if anyone can help me in the following I know you can overload a ...

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    19

    Different Function Arguments

    Hello. I would appreciate very much if anyone can help me in the following

    I know you can overload a method in C++ using the same name of a function for different versions with different number of arguments

    But checking the code in C that I am working with I found the following


    a function defined in one file as


    Code:
    void SRecvEth(void)
    {
    // blablabla
    }
    then in another file I have


    Code:
    void SRecvEth(unsigned int SOCK_NO);
    and lower in that file the function called as


    Code:
    SRecvEth(SOCK_NO);
    I build the code without problems! How can this be possible? Shouldnt this generate an error?! am I missing something?


  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    am I missing something?
    Yep. The first file is missing the function prototype to check that it matches the function definition.
    Instead of having the line
    Code:
    void SRecvEth(unsigned int SOCK_NO);
    in the second file, that line should be in a header file and should be included in both files. By including it in the first file the definition can be checked against the declaration.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Registered User
    Join Date
    Apr 2012
    Posts
    19
    Thank you for the answer. And sorry for not being clear
    I mean I have received code that was written by another developer and my work now is to do a version up of the code, modify it to new requirements etc.
    So far I am reading the code I have been handled and found what I described above. Even though the arguments dont match, the code can be built, and that is what I dont understand. Is that legal in C? how come my compiler doesnt give me an error?

  4. #4
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    No, it is not legal in C, but the compiler doesn't emit an error because the code is not structured properly. The declaration for SRecvEth should be in a header file and should be included in both the implementation file for that function as well as all files that use the function. That way the compiler can check that calls are correct. Each file is compiled on its own and only knows the way to call a function in a another file because of the prototype. If the prototype is incorrect there's no way to determine that across files. The code generated for the second file is pushing an argument for the function to use, but the code generated for the function definition in the first file is not accepting that argument. It's basically just ignoring it. Obviously this is an error and you should fix it.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. too few arguments to function 'pow'
    By MRidiot in forum C Programming
    Replies: 4
    Last Post: 12-27-2011, 12:12 PM
  2. function with variable function arguments
    By Kyl in forum C Programming
    Replies: 10
    Last Post: 12-21-2011, 02:56 PM
  3. |'s in function arguments
    By timmeh in forum C++ Programming
    Replies: 6
    Last Post: 11-05-2009, 09:51 PM
  4. Replies: 9
    Last Post: 01-02-2007, 04:22 PM
  5. Function Arguments...?
    By Eminence in forum C++ Programming
    Replies: 5
    Last Post: 12-31-2002, 01:17 AM

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