Thread: Multiple declaration of function

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    45

    Multiple declaration of function

    Why does this code output 2.Isn't it illegal to declare the same function again.
    Code:
    #include <stdio.h>
        void foo();
        int main()
        {
            void foo(int);
            foo(1);
            return 0;
        }
        void foo(int i)
        {
            printf("2 ");
        }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by narendrav
    Isn't it illegal to declare the same function again.
    No, it is illegal to define (effectively, to implement) the same function again. You can declare the same function as many times as you want, as long as the declarations are consistent with the definition.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. function declaration
    By r_james14 in forum C Programming
    Replies: 4
    Last Post: 02-28-2012, 04:23 AM
  2. function declaration....
    By roaan in forum C Programming
    Replies: 8
    Last Post: 08-15-2009, 09:46 PM
  3. function declaration.
    By jas_atwal in forum C Programming
    Replies: 4
    Last Post: 01-21-2008, 04:34 AM
  4. Function Declaration
    By leeor_net in forum C++ Programming
    Replies: 5
    Last Post: 09-05-2007, 12:32 PM
  5. odd multiple declaration?
    By insomnis in forum C++ Programming
    Replies: 7
    Last Post: 05-04-2007, 07:06 AM

Tags for this Thread