External functions

This is a discussion on External functions within the C Programming forums, part of the General Programming Boards category; I'm having trouble understanding how to use external functions. Does an external function have to be prototyped in a header ...

  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,386
    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-26-2009, 12:21 AM
  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, 12:02 PM
  4. Help with error
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 04-17-2002, 10:36 AM
  5. Compiler errors
    By BellosX in forum C Programming
    Replies: 2
    Last Post: 09-21-2001, 04:24 AM

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