ikerhurtado.com
You're in
Iker Hurtado's pro blog
Developer | Entrepreneur | Investor
Software engineer (entrepreneur and investor at times). These days doing performant frontend and graphics on the web platform at Barcelona Supercomputing Center

Relearning C++: Notes on functions

5 Mar 2015   |   iker hurtado  
Share on Twitter Share on Google+ Share on Facebook
In this post I write down some notes as from my study of C++ on functions, argument passing, return of values, and so on.

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.

Argument Passing

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.

by reference

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.

Pass by const reference unless you need to change the value of the argument (excepting for fundamental types).

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.

For more detailed explanation see: 7.3 — Passing arguments by reference « Learn C++

by address

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.

For more detailed explanation see: 7.4 — Passing arguments by address « Learn C++

Arrays

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.

Default arguments

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.

For more detailed explanation see: 7.7 — Default parameters « Learn C++

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

Function pointers

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

For deeper explanation see: 7.8 — Function Pointers « Learn C++

POST A COMMENT: