Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

passing by reference

Status
Not open for further replies.

Essendry

IS-IT--Management
Nov 13, 2002
4
0
0
GB
Can someone give me a bit more info on passing by reference? I need to write a function that uses this mechanism.
 
Passing by reference means passing a pointer. The reference or the address of a variable is passed to a function.

void f1(int a)
{
a=5+a;
}

void f2(int *a)
{
*a=5+(*a);
}
void main()
{
int m=5;
f1(m);//pass by value
printf("%d",m);//will print 5
f2(&m);//pass by reference
printf("%d",m);//will print 10
}
 
then what is difference between passing by reference and pointer in C++
 
i was talking abt passing by reference in C.
in C++ passing by reference means passing the alias of a variable using an '&'.
 
Hi,

C and C++, nothing is like passing parameters by refrence. In C and C++ always passing parameters are by value.

int main(void)
{
int x = 2;
int *ptr = NULL;

/* Passing the variable with the value int 2 */
func1(x);

/* Passing the variable contains address of x
here you are not passing the variable x
func2(&x);

ptr = &x;
/* Passing the variable again contains address of x */
func2(ptr); /* This statement is same as func2(&x). */
}

It means every time you are passing the value.
 
in ur program &x is the addresss or the reference of x, thats y it is called passing by address or passing by reference
 
here &x is the temparary variable which contains the address off variable x. That's why it's called always passing by value.
 
I think that this is a very trivial discussion going on abt passing variables. I'm sure that passing by address (or reference) can be checked out in any C book.
 
Hi,

U can find this in "Kernighan & Ritchie".
 
Passing by reference is a Algol 68/C++ concept. It does not exist in C. Algol 68 brought out passing by reference because PL/1 gave pointers a bad name. Then they found that they could do some useful things with it, like returning the results of operators.

The nearest you can get to it in C is to use a pointer. Basically

int& x = y[5];

is the same as

int* x = &y[5];

 
That's odd.

I thought that passing by reference came with FORTRAN in the fifties.

rgds
Zeit.
 
Passing by location came from FORTRAN in the fifties. There is call by name, reference, location and value. We still use location in C but you pass a pointer to the location by value. It is the same in Coral 66.

This flaw was quite common in the early fortran compilers. You could define a routine

subroutine fred(x)
x = 50
return
end


To use it,

y = 10
call fred (0)
y = y + 0
write (5, 'F10.1') y

And you would get 60. Nowadays, you'd probably get 'attempt to write to null pointer'.
 
It's a really important concept though, and worth differentiating in a more meaningful way than pedantics about whether a pointer is itself just a value. Can you imagine the consequences of passing a large array by value? Also, things passed by value cannot be changed by the function they're passed to. Things passed by address can.
 
Things passed by value can be changed by the function they're being passed into: the only difference is that the caller doesn't see the modified result because the called function uses a copy and not the original. That's the new thing about using const. You could read it as 'I promise not to change this' but then again you could always cast away the constness.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top