Do functions confuse you?

closed account (iAk3T05o)
When making my functions, they always confuse me except when it's a void menu () function.
I get really confused when making it so i end up putting everything in int main() .
What do you do when/before writing functions?
Thanks.
Functions are very cool and useful in C++.
First of all the primary reason for which one will create a function is that one would want to do some complex stuff, elsewhere(not in main) or at least wants his programme to look organized.

Basic syntax:
<return type> <function name>(parameters);

The <return type> tells the data type the function would return to its caller. eg: int would return an integer
<function name> is the desired ID of the function
<parameters> are the extra data passed to the function by the caller

eg:
1
2
3
4
5
6
7
int add(int a,int b) {return (a+b);}

int main()
{
     cout<<add(2,3);
     return 0;
}


The add function is called passing 2 and 3 as parameters.
The function then does the addition and returns the sum, of variable type int to main()
The main function then outputs the result.

For more info: http://www.cplusplus.com/doc/tutorial/functions/

HTH,
Aceix.
closed account (iAk3T05o)
For basic stuff like that, it's easy.
But in something like a tictactoe game, i can't figure it out.
Followed the link?

Aceix.
closed account (iAk3T05o)
I don't need to (i have the pdf, along with 4 other tutorials). The code there is the same with yours.
Think of functions as sub programs. So you have your main program that runs the game.

Then the sub programs would be stuff like move player X, draw game, display welcome message, display goodbye message, check if winner stuff like that.
closed account (iAk3T05o)
That's the thing. I know what they should do when thinking about it but when writing it, i get confused on what should go where etc.
Topic archived. No new replies allowed.