Thread: General question about declaring a function in C

  1. #1
    Registered User
    Join Date
    Dec 2015
    Posts
    5

    General question about declaring a function in C

    Hey guys, I'm a pretty novice programmer, so this might seem like some fantastically-simple question.

    Suppose I have something like:
    Code:
    #include <stdio.h>
    void foo(void);
    int main(void)
    {
        int x;
        void foo(void);
    
        blah blah blah code;
    
        return 0;
    }
    
    void foo(void)
    {
        barcode;
    }
    What is the purpose of declaring the function void foo(void) twice (that is, once at the top of the program, and then again in main())?

    The answers I'm getting from searches are less than satisfactory. I'm getting the feeling that if I declared the function only within main() that the function is only callable within main(), too? Whereas functions declared at the top of the program are callable anywhere? But even so, that still doesn't answer the question of why declare it twice?

    Confirm, deny, extrapolate?

    Thank you.

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,729
    You don't have to declare it twice, that's redundant. You're more or less spot on about why both of them exist as options.
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Dec 2015
    Posts
    5
    Quote Originally Posted by GReaper View Post
    You don't have to declare it twice, that's redundant. You're more or less spot on about why both of them exist as options.
    That's what I figured.

    I've been conducting some tests on my own, and so that brings me to a secondary, perhaps also fantastically-simple, question: Are there advantages, if any, to declaring the function within main() only rather than at the top of the program? That is, why would you want to declare a function locally, rather than globally, especially if that function has to be defined somewhere sooner or later, anyway?

  4. #4
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,729
    I can think of something but it's a little bit of a stretch:
    Imagine you're making a C library and define a function inside the header file, with static and inline, for use in multiple source files. The problem is, that function has to call another that is defined inside the source file, but you don't want to declare it in the header file because that would expose it to the user of your library, which you might not want.
    Devoted my life to programming...

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,793
    Quote Originally Posted by GReaper View Post
    I can think of something but it's a little bit of a stretch:
    Imagine you're making a C library and define a function inside the header file, with static and inline, for use in multiple source files. The problem is, that function has to call another that is defined inside the source file, but you don't want to declare it in the header file because that would expose it to the user of your library, which you might not want.
    Well it is easy solved by having separate API headers - which are distributed with the library and internal headers - that are distributed only with source code
    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

  6. #6
    Registered User
    Join Date
    Sep 2010
    Location
    Europe
    Posts
    87
    Quote Originally Posted by feckless View Post
    Whereas functions declared at the top of the program are callable anywhere?
    Have a look here.

    Also, if you call the function in the main, just use foo() instead of foo(void).

  7. #7
    Registered User
    Join Date
    Dec 2015
    Posts
    5
    Quote Originally Posted by nerio View Post
    Also, if you call the function in the main, just use foo() instead of foo(void).
    My question wasn't actually about calling the function. It wasn't necessarily about how and where to declare either. In the example in the OP, void foo(void) is declared twice, once at the top of the program (as is practice for many programmers), but also once in main(). My question was why. The real question (and answer) is why not. As it has been pointed out, this seems to be unnecessary and redundant.

    Anyway, thanks everyone.

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    4,539
    What is the purpose of declaring the function void foo(void) twice (that is, once at the top of the program, and then again in main())?
    Probably just a mistake. In this simple program with only the two functions either will work. However if you go the route of prototyping inside another function, such as main(), just realize that you can only call the function from the function that has the prototype.

    Jim

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 06-07-2010, 09:04 AM
  2. question about declaring function extern
    By movl0x1 in forum C Programming
    Replies: 2
    Last Post: 07-25-2007, 05:16 AM
  3. general function calling
    By techrolla in forum C Programming
    Replies: 4
    Last Post: 01-09-2004, 11:50 AM
  4. general question regarding a function parameter
    By mlupo in forum C Programming
    Replies: 7
    Last Post: 10-13-2002, 07:32 PM
  5. Declaring the format function
    By GaPe in forum C Programming
    Replies: 1
    Last Post: 09-11-2002, 09:10 AM