A Function in C++ is a chunk of Code that is used to conduct a particular task. And that chunk is only executed when it is called.
Say if you want to add two numbers, you can have an add Function that would be dedicated to add the numbers.
Similarly, if you want to divide two numbers, you can have a divide Function that would divide the numbers.
So, instead of placing all the codes in a single place. You can distribute the work among different Functions. So, that your code looks more structured and clean.
Below are the rules to create a Function :
Below is how an add function would look like:
int add(int firstNumber, int secondNumber) { int result; result = firstNumber + secondNumber; return result; }
And thats how we define a Function.
But it was mentioned in the Function description that a Function only executes only when it is called.
So, how a Function is called?
Let us see in the below example.
#include <iostream> using namespace std; void myFunction() { cout << "This is my first function"; } int main() { myFunction(); return 0; }
So, in the above code, there are two parts,
void myFunction() { cout << "This is my first function"; }
myFunction();
So, in the above code we have defined a Function, myFunction(). You can give any name to the Function(In this case we have given the name myFunction()).
void myFunction() { cout << "This is my first function"; }
And the work of the Function, myFunction() is, just to print This is my first function.
Now, just remember one thing, the above Function, myFunction() will never execute until it is called.
And thus we have called the Function, myFunction() from Main() function.
int main() { myFunction(); return 0; }
And the Function, myFunction() is called,
void myFunction() { cout << "This is my first function"; }
Now, let us look at another example of adding two numbers and returning the result using aFunction.
#include <iostream> using namespace std; //Add function starts here. int add(int firstNumber, int secondNumber) { int result; result = firstNumber + secondNumber; return result; } int main() { int firstNum = 5; int secondNum = 8; int addedResult; addedResult = add(firstNum,secondNum); cout << "The added value is : " << addedResult; return 0; }
Let us explain the above example with a practical scenario.
Say you are given a task of adding two numbers. And you would follow the below steps to achieve it :
int add(int firstNumber, int secondNumber)
result = firstNumber + secondNumber;
return result;
So far, we have seen how a Function works. Now, let us see who is the caller?
When the program execution begins,
And we initialise the first variable firstNum with the value 5.
int firstNum = 5;
Then in the next line, we have initialised the second variable secondNum with the value 4.
int secondNum = 8;
Then we call the add(int firstNumber, int secondNumber) Function.
addedResult = add(firstNum,secondNum);
And C++ searches for a Function with two arguments.
When it finds the Function, it assigns the value of firstNum to firstNumber and secondNum to secondNumber. And expects a value in return.
And gets into the block of the int add(int firstNumber, int secondNumber) Function.
This is where the numbers are added,
result = firstNumber + secondNumber;
And the result is stored in a variable result.
And in the next line we return the added value(i.e. result).
return result;
And the returned value goes back to the line, where the Function was called.
addedResult = add(firstNum,secondNum);
And the variable value gets the added result(i.e. 9) from the variable result.
And in the next line the added value is printed as output.
cout << "The added value is : " << addedResult;