A Function? What Is That?
void CalculateTwoNumbers()
{
int a = 7;
int b = 3;
int result = a + b;
std::cout << "The Result is: " << result;
}
int main()
{
CalculateTwoNumbers();
}
// Declared Below Main
void CalculateTwoNumbers()
{
int a = 7;
int b = 3;
int result = a + b;
std::cout << "The Result is: " << result;
}
The compiler is telling us that the build has failed which means this code will not run, and that the CalculateTwoNumbers identifier is not found.
The problem is that we declared the function below the main, and main doesn’t know that this function exists because it’s below it, so we need to move it above the main function like this:
// Declared Above Main
void CalculateTwoNumbers()
{
int a = 7;
int b = 3;
int result = a + b;
std::cout << "The Result is: " << result;
}
int main()
{
CalculateTwoNumbers();
}
Functions With Parameters
Let’s rewrite the function so that it takes two integer parameters:
void CalculateTwoNumbers(int a, int b)
{
int result = a + b;
std::cout << "The Result is: " << result;
}
int main()
{
CalculateTwoNumbers(5, 33);
}
Function That Returns A Value
Next type of function we have on the menu are functions that return a value.
The void in the function declaration tells the compiler that this function doesn’t return a value.
And when I say value I mean of the variable types. So when a function returns some value, that value is going to be a float, integer, boolean or a string.
Let’s rewrite our CalculateTwoNumbers function:
int CalculateTwoNumbers()
{
int a = 3;
int b = 7;
return a + b;
}
int main()
{
std::cout << "The sum between a and b is: " << CalculateTwoNumbers();
}
int main()
{
int result = CalculateTwoNumbers()
std::cout << "The sum between a and b is: " << result;
}
Function That Returns A Value And Takes Parameters
int CalculateTwoNumbers(int a, int b)
{
return a + b;
}
Now this function is flexible as it gets. It takes two parameters and then calculates the addition of those two parameters.
This means that we can pass any two numbers every time we call this function to execute:
int main()
{
std::cout << "The sum between a and b is: " << CalculateTwoNumbers(10, 20);
std::cout << "The sum between a and b is: " << CalculateTwoNumbers(1, 2);
std::cout << "The sum between a and b is: " << CalculateTwoNumbers(3, 3);
}
If you run the app now you will see 30, 3, and 6 printed in the console.
But you will see it like this: “The sum between a and b is: 30The sum between a and b is: 3The sum between a and b is: 6” – the printed lines are not separated or printed one below another.
We can fix that by adding “\n” in front of the second and third line inside the cout:
std::cout << "The sum between a and b is: " << CalculateTwoNumbers(10, 20);
std::cout << "\nThe sum between a and b is: " << CalculateTwoNumbers(1, 2);
std::cout << "\nThe sum between a and b is: " << CalculateTwoNumbers(3, 3);
Where To Go From Here
In this post you learned how to group your code into functions for easier maintenance and calling. You also learned about the different types of functions that you can create.
To continue on your learning journey you can take a look at our Conditional Statements Lecture which is the next lecture in this tutorial series.