[Tutorial][Community]5.Level 1-5, functions

This is the introduction to the functions. You can imagine functions like little programs in your main program. For example if you need to use some math in your program dozen of times you can use functions for keeping things simple. Here is an example:
You have two integer variable and you need to find minimum and store result into result:

int a1 = 50;
int a2 = 30;
int result;

if (a1 < a2){
	result = a1;
}
else{
	result = a2;
}

it’s simple huh? Yes. But if you need to do this job dozen of times it’s really boring. Every time you need to write again and again this simple thing. Or you can use functions:

int minimum(int firstValue, int secondValue) {
	if (firstValue < secondValue){
		return firstValue;
	}
	else{
		return secondValue;
	}
}

First line we say this functions return type of int. This means you can store returned value into int type variable. Then we give a name to function. You can give any name you want. But It is good to choose name according to the job. Then inside the brackets we can declare parameters as much as we need. In this tutorial we need two parameters. Parameters looks like variables. Actually it’s variables. But it’s local to the function. The purpose of parameters is to allow passing arguments to the function.
Second line we test values. If test result is true function returns firstValue. If test result is not true function returns secondValue.
So we can use our function in program now:

int a1 = 50;
int a2 = 30;
int result;

result = minimum(a1, a2);

Also you can find many tutorials and examples about functions. This tutorial very simple. Here is another page about C++ functions.