I got a question on the way you could write your program with your own functions

Is it any difference writing the whole code of your own functions before the main?
How can I edit my code on this site properly?
My code is the following:

#include "stdafx.h";
#include <iostream>;
#include <string>;
using namespace std;

//Introduce the game
void PrintIntro() {
	constexpr int W_LENG = 5;
	cout << "Welcome to the Cows and Bulls game\n";
	cout << "You must get my guess word that is an isogram and it's long " << W_LENG <<" letters\n";
	cout << "An isogram is a word in which every letter appears only one time\n";
	cout << "Let's start the game\n";

//Execute the game
int main()
	string Guess=" ";
	while (Guess!="**")
		cout << "-----------------\nWrite a word: ";
		cout << "You typed: " << Guess << endl;
	cout << "I'm ending the program...\n";
    return 0;


We may need a bit of clarity.
When you say

Is it any difference writing the whole code of your own functions before the main?

I assume you are referring to defining the PrintIntro() function before your main() function vs just declaring it, and then defining it (writing all of the code of the function) after main. If so, there are a number of differences. To be honest, it won’t matter in such a small application, but there can be some performance implications for larger projects, particularly when you get to classes and you start creating class header and definition files.

From a stylistic sense it also makes sense (at least to me) to be able to see all of the function declarations in one section, and then reference each function definition elsewhere. Eventually, the code in this project is broken into multiple files, and you will see the value of it there.

I also suggest this link https://stackoverflow.com/questions/4757565/what-are-forward-declarations-in-c which goes into great detail of forward declarations. It will give you a more detailed and clear answer.

Your second question…are you referring to formatting code as you post on the forums? I use the icon that looks like </> right over the text box to preformat my code…not sure if you are looking for something else.

Hope this helps.

Thanks for your answer it’s very helpful.

I have applied the code formatting prefix/suffixes for you in your post, see below for further information. :slight_smile:

See also;

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms