Thread: where to make function declaration, before main( ), or within main( )?

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    254

    where to make function declaration, before main( ), or within main( )?

    Hi

    So far I have always seen function declarations of user-defined functions being made before the main( ). But I have seen someone who made the declaration within the main( ) for a functions which returns a value? Is this correct? Can we also do this for functions which returns nothing - I mean of void return type?

    I don't have a compiler on my computer right now so can't test it? Please help me. Thanks.
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    It's nothing to do with what type the function returns.

    You can add a function prototype declaration at the start of any scope opening brace. It's not illegal, just weird.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User gardhr's Avatar
    Join Date
    Apr 2011
    Posts
    151
    Quote Originally Posted by jackson6612 View Post
    Hi

    So far I have always seen function declarations of user-defined functions being made before the main( ). But I have seen someone who made the declaration within the main( ) for a functions which returns a value? Is this correct? Can we also do this for functions which returns nothing - I mean of void return type?

    I don't have a compiler on my computer right now so can't test it? Please help me. Thanks.
    Declaring a function within a function is rarely very useful, methinks. Not a common practice anyway. Whether or not it returns a value is irrelevant.

    Sidenote: Regardless, always keep in mind those "most vexing parse" ambiguities...

  4. #4
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    I was taught to have the prototype before main..as in
    int max(int x, int y);
    int min(int x, int y);
    then have the definition after main's body
    Code:
    int main()
    {
    }
    int max(int x, int y)
    {
    }
    IMO i think that is easier to read if people do it like my lecturer said...A classmate was asking me to read her code..when I saw a function declared and defined in main() itself..I just wanted to cry then go to sleep.
    You ended that sentence with a preposition...Bastard!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. function declaration in Main function
    By filipn in forum C Programming
    Replies: 2
    Last Post: 11-11-2009, 01:31 PM
  2. Make main() return 3 - PUZZLE
    By Queatrix in forum A Brief History of Cprogramming.com
    Replies: 19
    Last Post: 01-01-2006, 12:43 AM
  3. Circular main <- main.o dependency dropped.
    By Queatrix in forum C++ Programming
    Replies: 4
    Last Post: 10-21-2005, 02:32 PM
  4. Main Declaration error
    By starkhorn in forum C++ Programming
    Replies: 11
    Last Post: 06-22-2005, 09:04 AM
  5. void main(), int main(), argc, argv[]????
    By Jonny M in forum C Programming
    Replies: 3
    Last Post: 03-06-2002, 09:12 AM