Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between function arguments declared with & and * in C++

I typed the following example:

#include <iostream> double f(double* x, double* y) {     std::cout << "val x: " << *x << "\n";     std::cout << "val y: " << *y << "\n";     return *x * *y; } double f2(double &x, double &y) {     std::cout << "val x: " << x << "\n";     std::cout << "val y: " << y << "\n";     return x * y; } int main() {     double a, b;     a = 2;     b = 3;      std::cout << f(&a, &b) << "\n";     std::cout << f2(a, b) << "\n";     return 0; }    

In the function f I declare x and y as pointers of which I can get the value by using *x. When calling f I need to pass the address of my passed arguments, that is why I pass &a, &b. f2 is the same except the definition is different.

Now my question is: Are they both really the same concerning memory management? Both not making any copy of the passed value but instead passing a reference? I wonder about f2 because I couldn't read out the address of x in f2 so I know more about x and y in f (there I know address AND value).

Thanks in advance!

Edit: Okay thanks, after doing some more research, I found a quite useful topic:

Pointer vs. Reference There's also a link to google coding guidelines http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml#Reference_Arguments which is quite useful I feel (as I understood now, it's a form of subject taste) to make more clear

like image 251
tim Avatar asked Apr 28 '11 09:04

tim


People also ask

What is the difference between a function and an argument?

When a function is called, the values that are passed during the call are called as arguments. The values which are defined at the time of the function prototype or definition of the function are called as parameters.

What is the difference between a function argument and a function parameter?

Note the difference between parameters and arguments: Function parameters are the names listed in the function's definition. Function arguments are the real values passed to the function. Parameters are initialized to the values of the arguments supplied.

What are the different function arguments?

5 Types of Arguments in Python Function Definition:keyword arguments. positional arguments. arbitrary positional arguments. arbitrary keyword arguments.

What is the difference between parameters and arguments explain with example?

A parameter is the variable listed inside the parentheses in the function definition. An argument is the value that is sent to the function when it is called.


2 Answers

f2 is taking it's arguments by reference, which is essentially an alias for the arguments you pass. The difference between pointer and reference is that a reference cannot be NULL. With the f you need to pass the address (using & operator) of the parameters you're passing to the pointer, where when you pass by reference you just pass the parameters and the alias is created.

Passing by const reference (const double& ref) is preferred when you are not going to change the arguments inside the function, and when you are going to change them, use non-const reference.

Pointers are mostly used when you need to be able to pass NULL to your parameters, obviously you'd need to check then inside your function if the pointer was not NULL before using it.

like image 194
Tony The Lion Avatar answered Sep 27 '22 18:09

Tony The Lion


This is just syntactic sugar to avoid having to use * everytime you reference the argument. You still can use & to have the address of x in f2.

like image 45
philfr Avatar answered Sep 27 '22 18:09

philfr