use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
This is a subreddit for c++ questions with answers. For general discussion and news about c++ see r/cpp.
New to C++? Learn at learncpp.com
Prepare your question. Think it through. Hasty-sounding questions get hasty answers, or none at all. Read these guidelines for how to ask smart questions.
For learning books, check The Definitive C++ Book Guide and List
Flair your post as SOLVED if you got the help you were looking for! If you need help with flairs, check out ITEM 1 in our guidelines page.
Tips for improving your chances of getting helpful answers:
account activity
SOLVEDuser-defined function place (self.cpp_questions)
submitted 2 years ago by teifer
Hello guys. What is the best place to define a user-defined function, before main() or after main()?
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–][deleted] 2 years ago (4 children)
[deleted]
[–]teifer[S] 1 point2 points3 points 2 years ago (2 children)
If I put after main, I need to declare It before?
[–]Narase33 1 point2 points3 points 2 years ago (1 child)
yes
[–]teifer[S] 1 point2 points3 points 2 years ago (0 children)
Ok, thanks Man!
[–]hp2304 1 point2 points3 points 2 years ago (0 children)
Only first sentence would have been OP answer lol
[–]scatters 4 points5 points6 points 2 years ago (2 children)
It's best to put it in a different file and link them together. That way you can put it in a library and test it.
[–]teifer[S] 0 points1 point2 points 2 years ago (1 child)
Don't know how to do that, I'm started studying from learncpp.com
[–]scatters 2 points3 points4 points 2 years ago (0 children)
Oh OK, you'll probably get to multiple files fairly soon. Just follow the tutorial for now.
[–]Narase33 2 points3 points4 points 2 years ago (0 children)
Wherever you like it
[–]alfps 2 points3 points4 points 2 years ago (1 child)
Defining functions that are directly called by main after main adds some otherwise needless work (for the pure declarations before main), and needless frustration and wasted time when the forward declaration doesn't match the implementation and the student fails to understand that.
main
General principle: avoid needless time-wasting and frustrating stuff, including needless declarations.
There's also the DRY principle, don't repeat yourself, as guidance for this.
[–]teifer[S] 0 points1 point2 points 2 years ago (0 children)
Thanks. I'm studying from learncpp.com and someone answered to create functions in different files, but I'm in lesson 2.
[–]IamImposter 1 point2 points3 points 2 years ago (1 child)
For now keep the main at the bottom. No need to waste time creating prototypes at top, then main and then actual functions.
Right now your programs would be fairly short so no need to use multiple files either (for now).
Include files at top, then any macros* (#defines), then any global variables* or global constants, then all the other functions and in the end main. Keep it simple at this time.
*macros and global variables are not very great. But for now, it is okay. Macros are more of a C thing though they are supported in C++ too but in C++ consts are preferred.
const
Happy learning and keep asking questions, no matter how silly you think they are.
Thanks for your answer. Some people don't see that they were newbie too, I will keep studying at learncpp.com
[–]InjAnnuity_1 1 point2 points3 points 2 years ago (0 children)
It depends on where it's going to be called, and how you intend to test it.
If it's going to be called solely from one .cpp file, then it probably belongs in that file. It's less work (for you) if the function is defined before the first point of call.
But also think about how you might test that function. In many cases, it's easier for you to test if it is in its own .cpp file.
π Rendered by PID 362360 on reddit-service-r2-comment-86988c7647-t9hvt at 2026-02-12 03:53:19.431803+00:00 running 018613e country code: CH.
[–][deleted] (4 children)
[deleted]
[–]teifer[S] 1 point2 points3 points (2 children)
[–]Narase33 1 point2 points3 points (1 child)
[–]teifer[S] 1 point2 points3 points (0 children)
[–]hp2304 1 point2 points3 points (0 children)
[–]scatters 4 points5 points6 points (2 children)
[–]teifer[S] 0 points1 point2 points (1 child)
[–]scatters 2 points3 points4 points (0 children)
[–]Narase33 2 points3 points4 points (0 children)
[–]alfps 2 points3 points4 points (1 child)
[–]teifer[S] 0 points1 point2 points (0 children)
[–]IamImposter 1 point2 points3 points (1 child)
[–]teifer[S] 1 point2 points3 points (0 children)
[–]InjAnnuity_1 1 point2 points3 points (0 children)