Thread: External functions

  1. #1
    Unregistered
    Guest

    External functions

    I'm having trouble understanding how to use external functions. Does an external function have to be prototyped in a header file or can it just be prototyped in a C source file and defined in another C source file? Can you provide an example of using an external function involving 2 or more files.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    file1:

    void myfunction( void ) {...do stuff...}

    file2:

    void myfunction( void ); //prototype it before you use it
    ...stuff goes here...
    ...now call the function...
    myfunction( );

    Additionally, you could do:

    file1.h
    void myfunction( void );

    file2:
    #include "file1.h"

    ...now call the function...
    myfunction( );


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    file1.c

    int function (void)
    {
    return 1;
    }


    file2.c

    extern int function (void);

    int other_function (void)
    {
    int retval;

    retval = function (void);
    return retval;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Is it legal to have functions within functions?
    By Programmer_P in forum C++ Programming
    Replies: 13
    Last Post: 05-25-2009, 11:21 PM
  2. unresolved external symbols...linking errors in VC++
    By rammohan2b in forum C++ Programming
    Replies: 2
    Last Post: 01-22-2009, 02:19 AM
  3. Problem with OpenGL tutorial
    By 2Biaz in forum Windows Programming
    Replies: 18
    Last Post: 09-16-2004, 11:02 AM
  4. Help with error
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 04-17-2002, 09:36 AM
  5. Compiler errors
    By BellosX in forum C Programming
    Replies: 2
    Last Post: 09-21-2001, 03:24 AM