Hi every single body. Now I am going to teach you how to call a function in C++, it is quite easy, as you will see.

ADVICE: You have to know that a function is a group of statements that is given name, and you can call them in the program by this name, main() is a function, and you can create lots of functions to do algorithms that may be used several times.
To create a function you have to do it this way (ignore the quotes “”):

“data type” “name of the function” (parameter1,parameter2,…)
{
Statements, algorithm
}

As you can see, every function needs a section, defined by “{ }”.

Here we have an example with basic terms (addition and substraction):

functions

Put attention at the return function, usually we use “return 0” because we do not need any value to be returned, but in this case, we need the result of the statements to be returned, so we have to write “return” and the variable where we save the result that we want to use.

And to call the function, I just have to write down the name of the function and inside parenthesis the parameters to evaluate (in the example, just two integer numbers).

I know this could be hard to understand, but I beg you to do by yourself as much as you can, analyse the example and if you still having doubts or questions, then I’ll gladly help you.

The program of the example is here: https://github.com/hrglez/TC1017/blob/master/Functions

CC BY 4.0 Creating and calling functions! (C++) by hrglez is licensed under a Creative Commons Attribution 4.0 International License.