answersLogoWhite

0


Best Answer

Whenever you pass a value to a function, that value must be copied. If the value is large or complex, this can hinder performance, particularly when the function does not need to alter the value. To improve performance, we use the pass by reference semantic. Rather than passing the value itself, we pass the address of the value. That is, the address is copied, not the value. The function can then refer to the value by dereferencing the address.

Ideally, pass by reference should only be used when the function does not need to alter the value. This is achieved by declaring the function's formal argument a pointer to constant type. This makes it clear to the caller the value of the actual argument will not be altered by the function.

In some cases, we do require the function to alter the value. In these cases the argument is regarded as being an output parameter because it allows the function to return another value besides the return value. Typically, the caller will allocate some memory for the function to use (perhaps initialising it with a value), and then pass the address of that memory to the function. The function's formal argument is therefore declared a pointer to non-constant type, making it clear to the caller that the function will modify the value being pointed.

User Avatar

Breanne Watsica

Lvl 10
1y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is the advantages of call by reference?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Is call by reference supported by pointer?

No, call-by-reference can be emulated with pointers.


Does java support call by reference?

No , Java does not support call by reference.


How do you combine call by value and call by reference programs in c?

Very easily: there is no call-by-reference in C.


Advantages to the theory?

Advantage of reference frame theory


What are the advantages and advantages of a phone?

You can call people.


Who do I call about a grant?

I would call your local reference librarian. Call your library and ask for the reference desk. You will get at least some starting helpers.


Can you swap two numbers by call by reference outside the function?

The using of term 'call-by-reference' implies function-call, so please rethink your question...


Explain the passing parameters as function in Call by Value and Call by Reference and then distinguish between them?

Call by value essentially passes a copy of an object's value whereas call by reference essentially passes the object itself. Pass by reference is the preferred method whenever possible as call by value will automatically invoke the object's copy constructor, which is often unnecessary, especially if the object is not affected by the function call (pass by constant reference).


What do you call a person who provides a reference?

A person who provides a reference is often referred to as a referee.


Call by reference?

A very useful thing.


What do you call someone who gives a reference?

A referee


Call by reference using pointer in c plus plus?

Example: void foo( MyClass& object ){} // function with call by reference signature MyClass* p = new MyClass(); // instantiate a pointer to MyClass foo( *p ); // call by reference using the pointer