03.12.2012 Views

C++ for Scientists - Technische Universität Dresden

C++ for Scientists - Technische Universität Dresden

C++ for Scientists - Technische Universität Dresden

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

42 CHAPTER 2. <strong>C++</strong> BASICS<br />

To make sure that our vector is not changed (and not copied either), we pass it as constant<br />

reference:<br />

double two norm(const vector& v) { ... }<br />

If we would change v in this function the compiler would emit an error. Both call-by-value and<br />

constant references ascertain that the argument is not altered but by different means:<br />

• Arguments that are passed by value can be changed in the function since the function<br />

works with a copy. 17<br />

• With const references one works on the passed argument directly but all operations that<br />

might change the argument are <strong>for</strong>bidden. In particular, const-referred arguments cannot<br />

appear on the left side of an assignment or passed as non-const references to other functions<br />

(in fact the LHS of an assignment is also a non-const reference).<br />

In contrast to mutable references, constant ones allow <strong>for</strong> passing temporaries:<br />

alpha= two norm(v + w);<br />

This is admittedly not entirely consequent on the language design side, but it makes the life of<br />

programmers much easier.<br />

Values that are quite frequent as argument might be declared as default. Say we implement a<br />

function the computes the n nt root and mostly the square root then we can write:<br />

double root(double x, int degree= 2) { ... }<br />

This function can be called with one or two arguments:<br />

x= root(3.5, 3);<br />

y= root(7.0);<br />

One can declare multiple default arguments but only at the end. In other words, after an<br />

argument with a default value one cannot have one without.<br />

2.6.3 Returning Results<br />

In the examples be<strong>for</strong>e, we only returned double or int. These are the nice ones. Functions that<br />

compute new values of large data structures are more difficult.<br />

Default arguments<br />

Sometimes functions have arguments that are used very infrequently. To address this, you can<br />

give a parameter a default value that is automatically used when no argument corresponding<br />

to that parameter is specified. In this way the caller only needs to specify those arguments that<br />

are meaningful at a particular instance. Consider the following example:<br />

void foo( int a = 5, char ch =’A’ )<br />

{ std::cout ≪ a ≪ ” ” ≪ ch ≪ std::endl ;}<br />

17 This assumes that the argument is properly copied. For user-defined types one can implement its own copy<br />

operation with aliasing effect (on purpose or by accident). Then modifications of the copy also affect the original<br />

object.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!