Arguments in function.

Aug 21, 2011 at 6:39am
Hi . Friends i am new in programming. I want to know that what is the main difference btwen arguments by value and by reference. And where i use which one .
Aug 21, 2011 at 6:44am
When you pass a parameter by value, the parameter will be a copy of the value you passed.
When you pass by reference, the parameter refers to the original value you passed.
Aug 21, 2011 at 2:09pm
Thank you but will you please tell me with an example.
Aug 21, 2011 at 2:19pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void increase(int copy)
{
    copy++; // Does absolutely nothing :D
}

void decrease(int &reference)
{
   reference++; // increases the number passed, not the copy
}

int main()
{
    int a = 5, b = 7;
    increase(a); // doesnt increase a, just a copy of it which will not get stored.
    decrease(b); // b is now 6.
    std::cout << a << " - " << b << std::endl;
    return 0;
}
Aug 21, 2011 at 2:21pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
using namespace std;

void func(int num)
{
    num += 45;
}

void func(int* num)
{
    *num += 45;
}

int main()
{
    int num = 45;

    cout << num << endl;

    func(num); //wont change this copy of num, just its own copy
    cout << num << endl;

    func(&num); //will change this copy because it doesn't have its own
    cout << num;

    return 0;
}


You can also use '&' for passing by reference, it is essentially a shortcut for pointers (i.e. it automatically dereferences itself when accessed, no need for '*' every time).

1
2
3
4
void func(int& num)
{
    num += 45;
}


This function will do the same thing as the one in the previous example, but without us having to manually dereference it.
Aug 21, 2011 at 2:51pm
closed account (1vRz3TCk)
Both of these are pass by value:
1
2
3
4
5
6
7
8
9
void func(int num)
{
    num += 45;
}

void func(int* num)
{
    *num += 45;
}


this is pass by reference:
1
2
3
4
void func(int& num)
{
    num += 45;
}
Aug 21, 2011 at 6:48pm
All of you thank u very much.
Aug 21, 2011 at 8:47pm
CodeMonkey wrote:

Both of these are pass by value:
1
2
3
4
5
6
7
8
9
void func(int num)
{
    num += 45;
}

void func(int* num)
{
    *num += 45;
}

this is pass by reference:
1
2
3
4
void func(int& num)
{
    num += 45;
}



The 2nd example (passing a pointer to an int) is a pass by reference. In C, there's no such thing as reference types, so the only way to pass by reference is to pass variables' addresses.

Technically, you are still partly correct. When you pass a pointer to a function, there is still a copy made -the pointer variable is still being copied, but not the value in memory location it points to. Consider the following:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int varA = 11;
int varB = 22;

void func(int* funcPtr)
{
    // Suppose that mainPtr is pass to this function, doing the following does not change it's pointed address
    funcPtr = &varB;
    // mainPtr still points to varA
}

int main(void)
{
    int* mainPtr = &varA;
    func(mainPtr); // makes a copy of the mainPtr variable.
    // at this line, mainPtr still points to varA
    return (0);
}
Aug 21, 2011 at 9:11pm
closed account (1vRz3TCk)
The 2nd example (passing a pointer to an int) is a pass by reference. In C, there's no such thing as reference types, so the only way to pass by reference is to pass variables' addresses.

No, the object that is passed into the function is a pointer and the value of the pointer is copied. A pointer may be said to reference an object but passing a pointer is not passed by reference. If it were to be passed by reference then the external pointer could be modified from inside the function.
Aug 21, 2011 at 9:17pm

No, the object that is passed into the function is a pointer and the value of the pointer is copied. A pointer may be said to reference an object but passing a pointer is not passed by reference. If it were to be passed by reference then the external pointer could be modified from inside the function.


I know the proper semantics of "passing by reference" (you should have obviously figured that out by the example I gave). I just want to point out what many forums, blogs, textbooks refer to as "pass by reference".

Depending on the level of discussion (i.e. machine code level), even these C++ reference types can still be considered as passing by value (you just can't modify it like pointers on high level languages like C++).
Last edited on Aug 21, 2011 at 9:20pm
Aug 21, 2011 at 10:18pm
closed account (1vRz3TCk)
I know the proper semantics of "passing by reference"

so how can you say a pointer is passed by reference?
Aug 22, 2011 at 2:16am
so how can you say a pointer is passed by reference?

I worded my answer incorrectly (sorry). What I was trying to say is that it shows the effect of passing by reference. I agree that it is a pass by value (my example as proof).

If you wanted to show that passing by pointer is a call by value, then the example should've been something like this:
1
2
3
4
void func(int* num)
{
    num = (int*) 0x567FF4;
}

The one you provided:
1
2
3
4
void func(int* num)
{
    *num += 45;
}

is indeed a call by value (in technical respect), but is doing a call by reference as an effect of the code logic.
Aug 22, 2011 at 3:20am
The "Does using pointers qualify as pass-by-reference?" debate is akin to the question "Is a tomato a fruit or vegetable?" By the cooking definition, a tomato is a vegetable and by the scientific definition, a tomato is a fruit. From the semantical perspective, passing a pointer is passing the object pointed to by reference. From the perspective of the actual calling mechanism, a pointer is passed by value.
Topic archived. No new replies allowed.