One easy way to conceptualize a function is as a machine. I often imagine a "wood chipper," one of those machines that take tree limbs, chips them into little pieces, and shoots the chips into the back of a truck. The chipper has an input and an output, and the internal mechanism is enclosed, so we can't see exactly how it does its job - we only know that it does its job because we can see the chips flying into the back of the truck.
Program functions are similar: zero or more inputs and zero or one output. Hopefully, we have some documentation that tells us what the function does, but if we don't have the source code, we can't see exactly how it works. Parameters are the function's inputs, and the return value is its output.
By default, C++ functions implement an argument passing technique called pass-by-value. Pass-by-value means that the inputs are expressions: constants, variables, sub-expressions formed from variables and constants connected with operators, or even other function calls. The program evaluates them and passes the resulting values into the function.
Functions with a void
return type don't return anything, but other functions can return a value. Functions that return a value must have at least one return
statement. Any valid expression, not just a variable, can follow the return operator. Using the square root function, sqrt, as an example, and assuming that x, y, and z are properly defined and initialized variables, all of the following are valid function calls: