Declaring Functions

This is a discussion on Declaring Functions within the C Programming forums, part of the General Programming Boards category; After reading the FAQ and the latest post about void main() and int main() I looked at some sample code ...

  1. #1
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681

    Declaring Functions

    After reading the FAQ and the latest post about void main() and int main() I looked at some sample code that came with my compiler.

    One thing I noticed is that they declared all the functions before the main(). IE.
    Code:
    int function1(var 1, var2);
    void function2(void);
    etc....
    
    int main()
    {
    blah blah blah
    }
    My question is this. What is the point of declaring them before main() and what effect does it have?

    I'm using Borland TurboC ("older then dirt" version)

    It never required me to use void or int before a function so I'm trying to learn that part so I can change my habit.

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    2
    It doesn't matter where you put the function definition, but if you put it after it's called and with out a prototype, your compiler will complain because it can't type check the function call. So, just either use function prototypes before you use the function and put the definition later ( below main ) or don't use prototypes and define the function before you use it.

  3. #3
    aurė entuluva! mithrandir's Avatar
    Join Date
    Aug 2001
    Posts
    1,209
    Generally speaking it's good practice to declare your prototypes before main. Why? Because it's easier to add/remove a prototype, and you can easily see what functions you have included without having to read through the whole program.

  4. #4
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,267
    >It never required me to use void or int before a function so I'm trying to learn that part so I can change my habit.<

    well it probably defaulted to void or int, what you use depends on what you wan't to return.

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. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM
  3. Shell functions on Win XP
    By geek@02 in forum Windows Programming
    Replies: 6
    Last Post: 04-19-2004, 05:39 AM
  4. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 05:58 PM
  5. functions - please help!!!!
    By linkies in forum C Programming
    Replies: 1
    Last Post: 08-21-2002, 07:53 AM

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