Functions should be declared in header files and defined in source files. The source file that defines a function should include the header that contains that function’s declaration. So, the compiler will verify that the definition and declaration are consistent.
A function parameter is a variable declared in the prototype or declaration of a function. Otherwise, an argument is the value that is passed to the function in place of a parameter.
Parameter initialization works the same way as variable initialization. When a function is called, all of the parameters of the function are created as variables (they are automatic objects and have local scope), and the value of the arguments are copied into the parameters. Of course, when the function block terminates they are destroyed.
The type of a parameter determines the interaction between the parameter and its argument. When the argument value is copied, the parameter and argument are independent objects. However, if the parameter is a reference the parameter is bound (is an alias) to its argument.
Local variables are initialized if their definition contains an initializer. Otherwise, they are default initialized.
It's a good idea to pass arguments by reference instead of doing it by value in order to avoid copying objects of large class types or large containers. In addition, functions must use reference parameters to operate on objects of a type that cannot be copied (like IO types).
Using const in a reference parameter is useful for two reasons: it ensures that argument will not change at function execution and tells the programmer reading the code that this function is not intended to modify the parameter.
Because references are typically implemented by C++ using pointers, and dereferencing a pointer is slower than accessing it directly, accessing values passed by reference is slower than accessing values passed by value.
Because pass by reference is generally safer than pass by address, pass by reference should be preferred in most cases. This option only has flexibility advantages.
Because it's not possible to copy an array, arrays must be passed by reference. When we pass an array to a function, we are actually passing a pointer to the array’s first element.
C/C++ does not allow functions to return an array. Hence, in order to modify the contents of an array you need to rely on pass-by-reference to work on the same copy inside and outside the function.
A default argument is specified as an initializer for a parameter in the parameter list. It's possible to define defaults for one or more parameters. However, if a parameter has a default argument, all the parameters that follow it must also have default arguments.
Default arguments are specified in the function prototype, and cannot be repeated in the function definition.
Returning values by value, reference and address
This interesting and tricky topic is very well explained here:
It's worthy to note that when a function return a reference, this is a lvalue and can be used in the same ways as any other lvalue. Like this:
MyFunction(myArray, 3) = 5; // this sets the value 5 in the forth array position
A function pointer is a pointer that aims a function rather than an object. Like any other pointer, a function pointer points to a particular type. A function’s type is determined by its return type and the types of its parameters.
int myfunction(int x); // function of type int(int) int (*functPtr)(int) = myfunction; // functPtr points to function myfunction()
The parentheses around *functPtr are necessary -otherwise, we declare functPtr as a function that returns a pointer to int:
When we use the name of a function as a value, the function is automatically converted to a pointer. For example:
functPtr = myfunction1; // functPtr now points to the function named myfunction1
Moreover, we can use a pointer to a function to call the function to which the pointer points —there is no need to dereference the pointer: int x = functPtr(4); // calls myfunction1