Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does ampersand in the return type of a function declaration work? [duplicate]

Tags:

In this piece of code, why f() is declared as "double & f(..."? What does it mean and how does it work? I don't even know what to google to find the answer to my question. Please help.

double a = 1, b = 2; double & f (double & d) {     d = 4;     return b; } 

I know ampersand sign means the address of a variable or a function, but I don't see why it would make sense to write it when you are declaring a function.

like image 557
Azad Salahli Avatar asked Feb 27 '13 07:02

Azad Salahli


People also ask

What does ampersand return in C?

The Address Operator in C also called a pointer. This address operator is denoted by “&”. This & symbol is called an ampersand. This & is used in a unary operator. The purpose of this address operator or pointer is used to return the address of the variable.

What does an ampersand mean in a function C++?

In this case, the ampersand does not mean taking an address, but it denotes a reference. Here, f is a function that takes a reference to double as parameter and returns a reference to double. You might want to read about C++'s references in your textbook of choice, since they are a very basic part of the language.

What does & do in C++ functions?

The bitwise AND operator (&) compares each bit of the first operand to that bit of the second operand. If both bits are 1, the bit is set to 1. Otherwise, the bit is set to 0. Both operands to the bitwise AND operator must be of integral types.

What is the return type of the function with declaration?

The result of a function is called its return value and the data type of the return value is called the return type. If a function declaration does not specify a return type, the compiler assumes an implicit return type of int .


2 Answers

Consider these two functions: power2() and add():

void power2 (double& res, double x) {     res = x * x; }  double& add (double& x) {     return ++x; } 

The first computes the power of x and stores the result in the first argument, res, – it does not need to return it.

The second returns a reference, which means this reference can later be assigned a new value.

Example:

    double res = 0;      power2(res, 5);     printf("%f\n", res);       printf("%f\n", ++add(res)); 

Output:

25.000000 27.000000 

Please note that the second output is 27, not 26 – it's because of the use of ++ inside the printf() call.

like image 193
kamituel Avatar answered Sep 19 '22 12:09

kamituel


When the & operator is used in a declaration form, preceded by a type it doesn't mean "the address of" but a "reference to" which is essentially an automatically dereferenced pointer with disabled pointer arithmetic.

There are no references in C, so if you want to pass or return by reference, you had to pass a const pointer and dereference it to access the pointed to value. C++ added references to make this concept easier, to prevent accidental walking off the address with pointer arithmetic, and to save the need to dereference the pointer. This makes working with it much easier and resulting in cleaner and more readable syntax.

like image 33
dtech Avatar answered Sep 19 '22 12:09

dtech