Functions are a way of making sets of instructions (code) for the program. One analogy I saw compared it to baking. If you were going to bake several recipes of cookies, you could write out the instructions and ingredient list 5 times - or you could write it once, and then follow the same instructions again for each batch.
Functions in C++ are set up like
1 2 3 4 5
|
returntype functionName (datatype inputvariable, datatype inputvariable2)
{
instructions that do things to input variables;
return something;
}
|
They are defined OUTSIDE the main, either inside a Class definition, or globally.
It's a bit confusing to explain because many of these function parts are optional. For example, a function does not have to 'return' anything, in which case the returntype would be void. It also doesn't have to accept any information in the input.
Suppose you just wanted to display something - heck, a TicTacToe board - over and over. Since the purpose of that function is just to cout an array, it doesn't need any input and it doesn't need to 'return' anything. We will use "void" as the return type because no return is needed. The parenthesis at the function beginning will be empty since it doesn't need any input.
Your function would look as follows.
1 2 3 4 5 6 7 8 9 10
|
void displayBoard()
{
cout << "| " << board[0][0] << " | " << board[0][1] << " | " << board[0][2] << " |\n";
cout << "|___|___|___|\n";
cout << "| " << board[1][0] << " | " << board[1][1] << " | " << board[1][2] << " |\n";
cout << "|___|___|___|\n";
cout << "| " << board[2][0] << " | " << board[2][1] << " | " << board[2][2] << " |\n";
cout << "|___|___|___|\n";
return 0;
}
|
Any time you wanted to redisplay the board again in your main() (which by this time you might have noticed is a function itself!) you would do
displayBoard();
and the program would find that function, go through the steps in it, and then return to the main and continue with whatever you've programmed next.
I also want to show you a function that accepts arguments (data sent in via the brackets) and returns something. Suppose you have a date stored in int variables. Let's say it is stored in
1 2 3
|
int day;
int month;
int year;
|
Since the datatype is int, these are stored as numbers. But what if you want to let the user enter any date, and then later cout that date with the month written as a word, like "January 4, 2012"? How do you convert the int month to the correct string - January, February, etc - automatically?
Well you have a function like so. In this function the return type is "string" because it's going to give us a string of characters. The argument it takes (int m) is an integer. Whatever integer we send in when we call the function will be stored in "m". Then it declares a string variable named "mw" to store the word in.
Please note that any variable declared inside a function only exists inside that function. So it doesn't matter if 2 different functions use the same variable names, so long as it doesn't confuse you when you're programming it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
|
string wordMonth(int m)
{
string mw;
if (m==1)
{mw="January";}
else if (m==2)
{mw="February";}
else if (m==3)
{mw="March";}
else if (m==4)
{mw="April";}
else if (m==5)
{mw="May";}
else if (m==6)
{mw="June";}
else if (m==7)
{mw="July";}
else if (m==8)
{mw="August";}
else if (m==9)
{mw="September";}
else if (m==10)
{mw="October";}
else if (m==11)
{mw="November";}
else if (m==12)
{mw="December";}
return mw;
}
|
At the end there it says "return mw;" which means the function is going to return whatever it just stored in the string variable 'mw'.
You could just call this function by saying
wordMonth(month);
which will take whatever info is currently stored in int month, and send it in to the variable m in the function. The function will then store the appropriate word in mw, and return that. But when it returns that information, nothing is really done with it. It's not being stored in a variable in main. It's not being output with cout. So instead we will do
cout << wordMonth (month) << " " << day << ", " << year << endl;
Yes... you can call the function inside a cout statement! This will send the numeric month in and output whatever string is returned, followed by a space, the numeric day, a comma, and the numeric year.
I hope this helps.. making any sense so far?