Jump to content

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

- - - - -

  • Please log in to reply
3 replies to this topic

#1
jackson6612

jackson6612

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 304 posts
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, learning C++. Using Win XP Pro and Code::Blocks. Be nice to me, please.:)

#2
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200
You may declare functions in many places other than the standard areas (remember declaring is a very simple hint, not a definition of the function), although that is a very poor programming method. Correct or not, headers are usually the correct place to store these declarations.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#3
fayyazlodhi

fayyazlodhi

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 403 posts
Also you can declare a function with any prototype any where though as already mentioned by Alexander, the only correct way to do this is to use header files.

So

 void func(void); 
Can be placed any where inside main, before main or after main (if it is not called in main)

#4
jackson6612

jackson6612

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 304 posts
Thank you, Alexander, Fayyaz.

Best regards
Jackson
I'm an outright beginner, learning C++. Using Win XP Pro and Code::Blocks. Be nice to me, please.:)




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users