Hey. I felt like writing a tutorial on something and since I've seen a lot of unneccessary complicated ones on functions, I decided to try & make a really simple introduction to them that anyone new to programming should be able to get.

Before we start, there's two important questions to answer. First of all, what is a function? And secondly, why and when should we use them?

Very simply put, a function is a piece of code (statements) that is separated from the main function. Any code we have in our functions are not automatically executed when the program runs - instead we need to call them from our main or from another function. What is the purpose of this then? First of all, functions are useful for avoiding code redundancy. Often when we code we will need to repeat similar tasks several times. Instead of having to rewrite the same code over and over, we can create one single function containing this code and then call it whenever we need to do that task. Functions are also useful for keeping your programs structured and not clogging down your main.

Imagine you have some kind of errorchecking in your program. If an error occurs, maybe you want to log the error, inform the user and then shutdown. Now what looks better? This

Code:
ofstream ut;
int error = 0;

int main(){

//imaginary code executes here

if(error==1){
ut.open("debug.txt", ios::app);
ut << "Error 1 occured!\n";
ut.close();
cout << "Error  has occured, this program will exit";
exit(1);
}

//some more imaginary code executes here

if(error==2){
ut.open("debug.txt", ios::app);
ut << "Error 2 occured!\n";
ut.close();
cout << "Error 2 has occured, this program will exit";
exit(1);
}

return 0;
}
Or this:

Code:
ofstream ut;
int error = 0;

void ThrowError(int errorcode) //our error function
{
ut.open("debug.txt, ios::app);
ut << "Error " << errorcode << " occured!\n";
ut.close();
cout << "Error " << errorcode << " has occured, this program will exit";
exit(1);
}

int main(){

//execute some imaginary code

if(error==1)
ThrowError(1); //here we call our error function

//more imaginary code

if(error==2)
ThrowError(2);

return 0;
}
It's fine if you don't understand everything that's going on in the code yet. It's enough if you can see that now, instead of having to rewrite all the code for opening our debug file, writing to it, closing it and displaying a message to the user, everytime we want to log an error we only need to write one line of code where we call the error log function. Now we can write less code and also our main looks a lot cleaner.

Now with those basics explained - let's have a look at the prototype functions have.

type name (argument1, argument2 etc)

What's this? If you look at my ThrowError function, it's type is void. Void means we don't want to return any data from our function, only execute the code within it. And it's name is of course "ThrowError". The arguments within the brackets are any type of data we want to send to our function. In the case of ThrowError, you can see it takes one argument, a int we refer to as errorcode. Now when we do "ThrowError(1)" from our main, the function will recieve the errorcode and know that error 1 has occured.

Let's have a look at some different types of functions now. First the simpliest one, a type void taking no arguments. Functions that takes no arguments and returns no data are generally not very dynamic - however if we have a chunk of code for something we want to do, it might be neater to put it in a function.

Code:
void Print()
{
cout << "Hai I'm a function!\n";
cout << "I'm supacool!\n";
}

//This function simply prints the text to the screen

Print(); //this is how we call it
I hope you understand functions of type void now. Let's look at another type of function, one that returns something. Imagine we are making a calculator and want to do the calculations in a function. We will want one for addition for instance. How should this function prototype look then? As we are working with numbers, we could use the type int for the function. And as we want to add two numbers to each other, preferably we want to be able to send two ints to the function for addition, meaning it will take two arguments. Let's have a go at it:

Code:
int Addition(int x, int y)
{
int z;
z = x + y;
return z;
}
I think you get the prototype now. But what is going on here inside the function? Let me explain.

First we declare a new local variable, z. Variables declared inside a function are not global and cannot be accessed from other functions.
Next we let z be the value of y added to x. If this function was called with let's say 250 and 300, z would now hold the value 550.
Finally we return z. As we will see now when we look at how we call this function, the value z holds is returned to where we called it from.

This is how we would call this function:

Code:
int number1, number2, result;

int main(){
cout << "Enter a value: ";
cin >> number1;
cout << "\nEnter another value: ";
cin >> number2;
result = Addition(number1, number2); //Here is the function call
cout << "\nThe result of the two added numbers is << result << "\n";
return 0;
}
I hope you can see now the purpose with returning z from the function. By calling it like we do and assigning the function to our variable result, the returned value (550 or whatever it is) will be stored in our result variable.

Time to wrap this up. A few important points about functions:

1. Functions are used to get rid of code redudancy and for structure/neatness in projects
2. There's two main types of functions, those that do not return anything (type void) and those that do return something (can be type int, type string etc).
3. Functions that do not return anything can be called like this "MyFunction();". Functions that do return something needs to be called with a variable of their type, for instance "ResultInt = Addition(number1, number2);" ResultInt now holds the returned value.
4. All types of functions can take arguments (data passed along when calling them) but a function doesn't need to take arguments. When we do want to call them with arguments, we use "MyFunction(arg1, arg2);"
5. If your function takes arguments, you need to call them with all arguments. A function looking like this "int Addition(number1, number2, number3)" must be called like "myint = Addition(550, 1000, 1024);". You can't call it with for example only two arguments ("myint = Addition(550, 1000);").
6. Functions can be called from main but also from other functions.
7. Functions needs to be defined in your project before you try to call them. This can be done in two different ways. The first one is placing the whole function before your main function like so:

Code:
void SomeFunction()
{
cout << "MPGH";
}

int main(){
SomeFunction();
return 0;
}
The second way is defining your function before main and then placing the actual function someplace else. Example:

Code:
void SomeFunction(); //This is how we define a function

int main(){
SomeFunction();
return 0;
}

void SomeFunction()
{
cout << "MPGH";
}
When outlining your programs/projects, try to make good use of functions. Imo there is no point in overusing them, that can be just as messy as a cloggy main. But for all times where you need to execute a lot of code or need to repeat some sort of task, it is probably smartest to put it in a function. Think about what you want/need the function to do and define your prototype after that.

Hope you enjoyed reading this and maybe learned something new. Thank me if you appreciated this tutorial.