BlaqICE
BlaqICE

Reputation: 309

C++ argument passing

Okay, so I just started learning C++ and I'm finding the ins and outs of argument passing to be a little confusing. I came across the following line in my C++ book;

In C++, passing by reference is accomplished in two ways: using pointers and using references.

It then goes on to show a program that switches the values of two int values, x and y (shown below). I was wondering, are the arguments to the swap() function really passed by reference in this case? I thought they were being passed by address, which I've been told is just a special case of pass by value.

//Listing 9.6 Demonstrates passing by reference 

#include <iostream.h>

void swap(int *x, int *y);

int main()
{
    int x = 5, y = 10;

    cout << "Main.  Before swap, x:  " << x
         << " y: " << y << "\n";
    swap(&x,&y);
    cout << "Main. After swap, x:  " << x
         << " y: " << y << "\n";
    return 0;
}
void swap(int *px, int *py) 
{
    int temp;

    cout << "Swap.  Before swap, *px: "
         << *px << " *py: " << *py << "\n";

    temp = *px;
    *px = *py;
    *py = temp;

    cout << "Swap. After swap, *px: " << *px
         << " *py: " << *py << "\n";

}

Upvotes: 0

Views: 2345

Answers (4)

Cheers and hth. - Alf
Cheers and hth. - Alf

Reputation: 145457

Nice conflation of abstraction levels.

Lowest level: when you pass a C++ pointer by value the pointer value is copied.

Higher level: usually the pointer points to something, and that something is logically passed by reference.

The most direct way to pass by reference in C++, is to use a reference argument. Under the hood a C++ reference can (in some given case) be a pointer, but there's no way to access or determine anything about that pointer value. So it doesn't make sense to say that the reference is passed by value or copied, and instead we talk about binding the reference or binding to the reference.

In a correct program a reference can't be a null-reference, and this is a distinct advantage for references as formal argument type.

Another advantage is that a reference to const can be bound to a temporary, so that for example

void foo( const std::string& s ) ...

can be called like

foo( "Blah!" )

which isn't possible when instead of a reference one uses a pointer to “implement” logical pass-by-reference.

Upvotes: 3

5gon12eder
5gon12eder

Reputation: 25459

You are right. The function

void swap(int *x, int *y);

is taking its arguments by-value. Because the arguments are pointers, it can dereference them and thus modify the memory they point to. The pointers themselves (the addresses) are passed by-value, nevertheless. You can see that if swap assigns a different value (address) to one of its arguments, the change isn't reflected in the caller.

int a = 1;
int b = 2;
int * pa = &a;
int * pb = &b;
swap(pa, pb);
assert((pa == &a) && (pb == &b));

Try adding a stuff like

px += 12;
py += 17;

at the end of swap. The assertion will still hold.

In C++, a pointer is an object in its own right. It has a value (the address it points to) and can be copied or assigned a different value. It can also be passed by-reference or by-value just like other objects.

A function that would take a pointer by-reference would look like this.

#include <iostream>

void
pointer_by_reference(int * & p)
{
  p = nullptr;
}

int
main()
{
  int a;
  int * pa = &a;
  pointer_by_reference(pa);
  std::cout << "&a = " << &a << ", pa = " << pa << "\n";
}

Possible output:

&a = 0x7ffcfdf2e00c, pa = 0

As you can see, pointer_by_reference has changed the value of pa.

Upvotes: 1

Kamaldeep singh Bhatia
Kamaldeep singh Bhatia

Reputation: 732

Here its Call by Address as you are catching address of variables. In case of Call by Ref we uses void swap(int &x, int &y); as prototype.

Details check Diff Between Call By Reference And Call By Pointer

also see for knowledge What are the differences between a pointer variable and a reference variable in C++?

Upvotes: 0

itsols
itsols

Reputation: 5582

There are basically two techniques of parameter passing.

  1. Call-by-value
  2. Call-by-reference (also known as call-by-address)

Your code is indeed passing parameters by reference.

With a call-by-value, when data is passed to a parameter in a procedure, only the value is made available to the procedure's parameter. So any change you make to the parameter within the procedure has no effect on the calling routine's variable.

But with a call-by-reference, the address of the variable is made available to the parameter. In other words, the parameter is actually pointing to the same location as the variable. So changing the parameter within the procedure actually changes the variable passed to it.

Upvotes: -2

Related Questions