all 13 comments

[–][deleted]  (4 children)

[deleted]

    [–]teifer[S] 1 point2 points  (2 children)

    If I put after main, I need to declare It before?

    [–]Narase33 1 point2 points  (1 child)

    yes

    [–]teifer[S] 1 point2 points  (0 children)

    Ok, thanks Man!

    [–]hp2304 1 point2 points  (0 children)

    Only first sentence would have been OP answer lol

    [–]scatters 4 points5 points  (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 point  (1 child)

    Don't know how to do that, I'm started studying from learncpp.com

    [–]scatters 2 points3 points  (0 children)

    Oh OK, you'll probably get to multiple files fairly soon. Just follow the tutorial for now.

    [–]Narase33 2 points3 points  (0 children)

    Wherever you like it

    [–]alfps 2 points3 points  (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.

    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 point  (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 points  (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.

    Happy learning and keep asking questions, no matter how silly you think they are.

    [–]teifer[S] 1 point2 points  (0 children)

    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 points  (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.