Is pass by value and call by reference the same? I mean i know theres no call by reference in C but there is similarity int he output of the two isn't it?

why do we use call by value and pass by value? what are its uses?

Is pass by value and call by reference the same? I mean i know theres no call by reference in C but there is similarity int he output of the two isn't it?

why do we use call by value and pass by value? what are its uses?

Do you mean a C++ reference? Because a C++ reference is just a special case pointer..

Your second question...we use pass by value to protect the integrity of the original value

c doesn't have call by refrence since the value if it's not a ptr it won't change only it's copy will in the function if u want it to change u need to pass the address

Is pass by value and call by reference the same?

No. Pass/Call by value makes an independent copy of the value for use in a function, but pass/call by reference uses the same object that was passed as an argument. Everyone likes to keep saying it, but C does not have pass by reference. It can only be faked passing pointers by value and using indirection them to get the original object.

why do we use call by value and pass by value? what are its uses?

Here is a 10,000 foot overview. Use pass by reference when:

  • The object is very large and passing it by value would be costly for memory. Make it const if it should not be changed.
  • The original object needs to be changed within a function and cannot be used as a return value.

Use pass by value when:

  • The value is small and the original object does not need to be changed within a function.

In the context of C, 'pass by reference' really means 'pass a pointer by value'. The whole thing is kind of a solved problem because C is such a small language. You either pass a pointer or not, depending on your needs. I think a more interesting question is whether or not value parameters should be made const:

void Function1(int x, int y);
void Function2(int const x, int const y);

But that is too far off topic for the thread. ;)

C does not have pass by reference. It can only be faked passing pointers by value and using indirection them to get the original object.

Just curious, what do you think C++ does when it passes by reference. You said pass by reference doesn't copy the value or the address of the data. So how does the function locate the values or data?

I think you'll find its just a special case pointer that's handled by the compiler. I could be wrong...C++ isn't really my thing.

Edited 7 Years Ago by gerard4143: n/a

Just curious, what do you think C++ does when it passes by reference.

I think the easier implementation would pass by pointer and hide the pointer mechanics on the compiler side. A more "pure" implementation of references might somehow mark the reference and replace it with the same address as the original variable when generating machine code. That is neither here nor there as this is the C forum. :)

You said pass by reference doesn't copy the value or the address of the data.

Not so. I said something more general because C does not have native pass by reference semantics:

pass/call by reference uses the same object that was passed as an argument

How the same object is used is an implementation detail, and the detail does not matter as long as the effect of pass by reference is achieved without programmer intervention.

The programmer intervention part is why I do not believe C supports pass by reference. If I need to do something special beyond declaring a reference, the effect of pass by reference is not fully achieved. With pass by pointer I need to assign the address of the object before calling the function and I need to dereference the pointer to get to the object inside the function.

This question has already been answered. Start a new discussion instead.