Thread: Function declaration

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    2

    Unhappy Function declaration

    Hi all,

    I am somewhat confused about the concept of function declarations in header files. I discovered (by coincidence) that my IDE allows me to use a function foo() which is defined foo.c in another c file bar.c even when I comment out the declaration of foo() in foo.h.

    I somehow read about this before but I totally forgot about it. Does this mean that a function is always global? If so, what is the use of a header file if it only contains function declarations?

    thank you,

    Jef

  2. #2
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    If you define functions in foo.c and want to use them in other source files, you declare them in a header file, which can be included by the other source files. The compiler needs that declaration for proper compiling of your source files. Your compiler should produce a warning, did you check your compiler messages?

    Probably your IDE is quite "smart" and enables you to use non-static functions in other files without declaring them in a header file.
    Last edited by Shiro; 09-13-2008 at 06:45 AM.

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Probably your IDE is quite "smart" and enables you to use non-static functions in other files without declaring them in a header file.
    It is just part of the C standard. Any callled function without previous declaration is ssuposed to have a prototype
    int foo(...);

    if real prototype is different - you asking for troubles. So good practice - to have prototypes for every function you are going to call - for this purpose you will include the proper include file...
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Registered User
    Join Date
    May 2008
    Posts
    87
    Quote Originally Posted by Jef Patat View Post
    I somehow read about this before but I totally forgot about it. Does this mean that a function is always global? If so, what is the use of a header file if it only contains function declarations?
    Jef
    One way that header files are really useful is for allowing your program to use functions from other libraries on your system. Consider the C standard I/O library. Your system has a compiled library file(s) on it that contains functions like printf(), but you probably don't have the source code. So when you want to write a program that uses printf(), you need to know how it is declared, so you include stdio.h in your source file. Then when you compile your program and the compiler encounters a use of printf(), it can check it against the declaration to know how to understand your source code.

    If you didn't use a header file, you'd need to declare all the functions you wanted to use at the top of your source file.

    It is fine to have the function defined in another source file, or even an already compiled library file on your system. The linker will take care of finding the function definition.

    Notice the difference in terminology. A function declaration declares for the compiler the function type, number of arguments and type of arguments. A line like
    Code:
    int foo(char *);
    is a function declaration.

    Without it, the compiler won't know how to understand foo() when it encountered it in your source code. What is the return type? How many arguments does it take? How should the compiler know?

    A function definition is the code that makes up what the function does. The linker will make sure that the proper instructions get executed where foo() in encountered (based on the information you pass along to the linker).

  5. #5
    Registered User
    Join Date
    Sep 2008
    Posts
    2
    Quote Originally Posted by vart View Post
    It is just part of the C standard. Any callled function without previous declaration is ssuposed to have a prototype
    int foo(...);
    That's similar to what I mean by 'read about this before'. Most of the functions I use are of type void foo(void) however. So shouldn't there be an error/warning if I comment out the declaration in the headerfile? Or is void the same as int in this case? I can also get code working for a function like this: const char *uart0Puts(const char *string) by removing the declaration. The compiler only complains if I take away the definition. My IDE is crossstudio which is using the gnu compiler.

    thank you, Jef

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    So shouldn't there be an error/warning if I comment out the declaration in the headerfile?
    A warning, if you enable enough warnings.
    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

  7. #7
    Registered User
    Join Date
    Sep 2008
    Posts
    13
    I hope this isn't too off topic but I didn't really want to start a new topic when there were so many that are somewhat similar.

    I understand that in the function you need to define a return type, I just want to make sure that I understand the concept there

    if I have a function:

    Code:
    int foo(...)

    can the return type only be an integer? I know there are other types of functions, void, float, etc. but do those types confine a function to returning only the type that they are defined as?

    does that statement even make sense, again sorry if there's any confusion I'm pretty new to C and programming in general and I'm trying to get it down.

    Thanks

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    can the return type only be an integer?
    As declared, the return type is the int type.

    I know there are other types of functions, void, float, etc. but do those types confine a function to returning only the type that they are defined as?
    A conversion between types is possible. Of course, if the return type is void, then one simply does not return anything.
    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

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Yes. int foo() means that the function will return an int. Now that int can be an expression (you can do "return x+5" or something similar), and if it is a float expression it will be converted down to an int.

  10. #10
    Registered User
    Join Date
    Sep 2008
    Posts
    13
    awesome. I really appreciate the help. I'm at the stage where I can kind of make basic math functions just to get my feet wet but run into problems in that I sometimes try to do things that the compiler doesn't like.

    Again, I appreciate the help

    thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 01:49 PM
  3. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  4. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM