answersLogoWhite

0


Best Answer

You cannot arbitrarily determine what is passed to a function from outside of the function. If the function expects a reference, then the memory address of the variable will be automatically passed to the function. If the function expects a value, then a copy of the variable's value is automatically passed instead. In other words, it is the function signature that determines what is passed, not how you call the function.

User Avatar

Wiki User

11y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: When a reference variable is passed to any function than whether it pass address or a copy of a variable?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Suppose an external variable is defined outside of function A and accessed within the function Does it matter whether the external variable is defined before or after the function. Explain?

No the two work together REALLY


How do you return by reference in C plus plus?

Returning by reference implies that the reference will not fall from scope when the function returns (because references must never be NULL). Thus you cannot return a reference to a variable that was declared local to the function. int& foo() { int local; return(( int & ) local); // Error: cannot return a reference to a local variable. } However, you can return a reference to a parameter that was itself passed by reference, since that reference is guaranteed to exist (it can't be NULL). const int& GetMax( const int& byRef1, const int& byRef2 ) { return( byRef1 > byRef2 ? byRef1 : byRef2 ); // this is fine. } Generally, the only time you will return by reference is when returning a class instance member or a reference to the class instance itself, since they always remain in scope after the function call. class bar { public: bar():m_num(0){} const int& GetIntRef()const{return((const int&)m_num);} // constant reference to instance member. int& GetIntRef(){return((int&)m_num);} // non-constant reference to instance member. const bar& AsRef()const( return((const bar& )*this );} // constant reference to this instance. bar& AsRef()( return((bar&)*this );} // non-constant reference to this instance. private: int m_num; }; Note that use of const is not obligatory when returning references. However, when values must not be changed, it's good policy to enlist the help of the compiler wherever possible. The previous example returns both constant and non-constant references. The function that makes a call to GetIntRef() or AsRef() will determine whether the returned reference should be constant or not. By implementing both methods, we cater for both scenarios.


What is reference?

A reference is a named memory address, whether a variable or constant. That is, the address-of a named variable is its reference. A pointer variable is a reference that can store a reference. That is, the value of a pointer is a reference. However, while pointers may store a reference to NULL (the value zero, meaning no reference), references can never be NULL. More to the point, a NULL reference will invalidate your program.Just as you can declare multiple pointer variables to the same reference, you may also declare multiple references to the same name. However, references are not variables in their own right so even though each has its own name they do not require any additional storage beyond the memory they actually refer to, unlike pointers which each require a separate reference of their own in order to store the reference. That is, no matter how many references you declare to the same memory address, there really is only one reference. And because references are not variables, once they've been assigned (always at the point of declaration) they cannot subsequently refer to anything else while they remain in scope. Whereas pointer variables can point to any reference at any time, including NULL.


What is difference between call by value and pass by value?

When you pass by value, the function's parameter is assigned the value of the argument that was passed. When you pass by reference, the function's reference parameter is assigned the address of the argument. In other words, pass by value copies the value, pass by reference passes the variable itself.Pass by reference is always the preferred method of passing arguments to functions when those arguments are complex objects. If the argument is a primitive data type then the cost in copying the value is minimal, but copying a complex object is expensive in terms of performance and memory consumption. If the function parameter is declare constant, you can be assured the object's immutable members will not be affected by the function. If it is non-constant and you do not wish your object to be altered, you can either copy the object and pass the copy, or pass the object by value if the function is overloaded to cater for this eventuality (good code will provide both options).


How are pointer different from other variables?

A pointer variable is a variable that contains the memory location of another variable or an array (or anything else in memory). Effectively, it points to another memory location. For standard variables, you define a type, assign a value to that variable or read the value from it, and you can also read the memory location (&n = memory location of n) of the variable. For pointers, you can point them to any variable, even another pointer, and you can get the value of the variable it points to (*p), the location of that variable in memory (p), or the address in memory of the pointer itself (&p). Consider: long n = 65; long *p; p = &n; Results: Type | Name | Location | Value long n 0xA3FF 65 long * p 0x31DF 0xA3FF So p points to n. Now, n = 65 &n = 0xA3FF p = 0xA3FF *p = 65 &p = 0x31DF You may find yourself having to use typecasts frequently when using pointers. Pointers are useful when passing data to functions. For instance, consider the following function: void add(int a, int b, int c) { c = a + b; } The problem here is that the computer copies each variable into a new memory location before passing them to the function as local variables. This function effectively does nothing. However, if you change the function to: void add(int a, int b, int *c) { c = a + b; } and call the function by passing in the location of the variable to the function: add(a,b,&c); then you can modify the variable itself. Pointers are also good for working with arrays: char *c = "Hello World"; int i=0; while (c[i] != 0x00) { cout << c[i]; c++ } //print one letter at a time.

Related questions

How can you change values in call by reference?

We don't call by reference, we call functions. The arguments passed to the function are passed (not called) either by value or by reference, depending upon the function signature (the prototype). When you pass by reference you are passing the actual variable, not a copy of the variable, thus the function can modify that variable's value directly. The only exception is when the parameter is declared a constant reference. Passing a pointer is essentially the same as passing by reference, however the pointer itself is passed by value. To pass a pointer by reference you must pass a pointer-to-pointer instead. Passing by value always copies the value, whether it is declared constant or not. But if it is declared constant, the function might as well accept a constant reference. Passing objects (instances of a class) by constant value will incur a performance penalty in making an unnecessary copy. If it is constant, there is little point in copying the object.


Suppose an external variable is defined outside of function A and accessed within the function Does it matter whether the external variable is defined before or after the function. Explain?

No the two work together REALLY


How do you know whether a relation is a function?

Use the definition of a function. If, for any value of one variable, there is only a single possible value of the second variable, then the second variable is a function of the first variable. The second variable is often called the "dependent variable". If you can solve an equation explicitly for the dependent variable, then it is a function. If you can NOT solve it for a variable, it may or may not be a function - it turns out that some equations are hard or impossible to solve explicitly for one of the variables.


What is pass by value in functions?

Pass by value describes the way in which an argument is passed to a function by copying its value. For example: void f (int b) { // ... } int main (void) { int a = 42; f (a); } The main() function calls the f() function passing the local variable named a. However, the f() function uses a local variable named b. The variables a and b are local to the functions in which they are declared; they each have their own memory address (storage) and are therefore completely separate variables. Moreover, a is outwith the scope of f() while b is outwith the scope of main(). The variable named a is also known as an actual argument because that is the actual argument we are passing to the function. The variable named b is known as a formal argument because that is the argument received by the function. When we call the f() function, we need some mechanism that allows the function to refer to the variable a via the local variable b. This is achieved by (automatically) copying the value of a and assigning that value to the variable b. Now both variables refer to the same value. However, because b is a copy of a, changing the value of b does not affect a. This is what is meant by pass by value. If we want the f() function to operate on the variable a itself rather than just a copy of a, we must use the pass by reference semantic. That is, we must pass the address of the variable rather its value. In languages that support a native pointer type, the function can use a pointer variable to hold that address: void f (int* b) { // ... } This changes the calling convention because now we must pass the address of a rather than its value: int main (void) { int a = 42; f (&a); } Note that a and b are still separate variables, thus we are still passing by value. However, the value we pass is a memory address, thus the variable b now holds a copy of that address, the address of a. Given that the pointer variable, b, now refers to the address of a, we call this the pass by reference semantic. Any changes we make to the value being pointed at by b will also be reflected in a. Some high-level languages, such as C++, also support a native reference type. A reference is simply an alias, a means of referring to an existing object by an alternative name (much like the way we can refer to a person named William as Bill -- they are one and the same person). Thus in C++ we can write the f() function with a reference argument: void f (int& b) { // ... } This changes the calling convention: int main (void) { int a = 42; f (a); } Note that we do not need to take the address of a when passing by reference because a is itself a reference; it is the name we use to refer to the memory holding the value 42. The argument b is simply an alternative name for that same address. This can often lead to confusion because the calling convention is the same as it was for pass by value. However, it is the function being called that determines whether a value or a reference is passed, not the caller. In general, the caller does not actually have to know how the actual argument is being passed. References in C++ are actually implemented as pointers at the machine-level. They are nothing more than eye-candy for an actual pointer variable so, behind the scenes, pass by value is still being used (albeit with the pass by reference semantic). The main difference is that a reference must always refer to something that actually exists in memory; a pointer need not (a pointer may be null). As such, references are generally much easier to use than pointers.


How do you solve for variable fx in statistics?

It depends on whether fx denotes frequency times variable value or the probability generating function for the variable x.


How do you return by reference in C plus plus?

Returning by reference implies that the reference will not fall from scope when the function returns (because references must never be NULL). Thus you cannot return a reference to a variable that was declared local to the function. int& foo() { int local; return(( int & ) local); // Error: cannot return a reference to a local variable. } However, you can return a reference to a parameter that was itself passed by reference, since that reference is guaranteed to exist (it can't be NULL). const int& GetMax( const int& byRef1, const int& byRef2 ) { return( byRef1 > byRef2 ? byRef1 : byRef2 ); // this is fine. } Generally, the only time you will return by reference is when returning a class instance member or a reference to the class instance itself, since they always remain in scope after the function call. class bar { public: bar():m_num(0){} const int& GetIntRef()const{return((const int&)m_num);} // constant reference to instance member. int& GetIntRef(){return((int&)m_num);} // non-constant reference to instance member. const bar& AsRef()const( return((const bar& )*this );} // constant reference to this instance. bar& AsRef()( return((bar&)*this );} // non-constant reference to this instance. private: int m_num; }; Note that use of const is not obligatory when returning references. However, when values must not be changed, it's good policy to enlist the help of the compiler wherever possible. The previous example returns both constant and non-constant references. The function that makes a call to GetIntRef() or AsRef() will determine whether the returned reference should be constant or not. By implementing both methods, we cater for both scenarios.


What is reference?

A reference is a named memory address, whether a variable or constant. That is, the address-of a named variable is its reference. A pointer variable is a reference that can store a reference. That is, the value of a pointer is a reference. However, while pointers may store a reference to NULL (the value zero, meaning no reference), references can never be NULL. More to the point, a NULL reference will invalidate your program.Just as you can declare multiple pointer variables to the same reference, you may also declare multiple references to the same name. However, references are not variables in their own right so even though each has its own name they do not require any additional storage beyond the memory they actually refer to, unlike pointers which each require a separate reference of their own in order to store the reference. That is, no matter how many references you declare to the same memory address, there really is only one reference. And because references are not variables, once they've been assigned (always at the point of declaration) they cannot subsequently refer to anything else while they remain in scope. Whereas pointer variables can point to any reference at any time, including NULL.


How do I answer references name and address?

You have to ask someone (a previous employer for a work reference or someone who knows you well for a personal reference) if they are willing to supply a reference for you (this is the polite and courteous thing to do). If they are willing then you write their name and address on your application where this question is asked (stating whether the reference is personal or work related).


What are call by value and call by reference?

Call by value is where the argument value is copied to the formal parameter, which is then passed to the function. While the function is executing, it can see the copy of the argument, and it can modify it, if desired, but since it is a copy, it cannot modify the original argument.Call by reference is where the argument's address (or some kind of reference to it, see the clarification below) is copied to the formal parameter, which is then passed to the function. While the function is executing, it can see the original argument, and it can modify it, if desired.Note that, formally, C and C++ are always call by value. When we use so-called call by reference semantics, whether it is explicit like in C, or implicit like in C++, we are simply treating the address of the argument as the value that is copied, but when you get into the nitty gritty details of the calling sequence, it is always call by value.As a clarification, because terminology is critical here, what we do in C and C++ is actually call by value or call by address, not call by reference. The distinction is important when you get into managed heap languages like Java and .NET, where the formal parameter is actually a reference handle to some object in the heap, and not actually a value nor an address.


What is difference between call by value and pass by value?

When you pass by value, the function's parameter is assigned the value of the argument that was passed. When you pass by reference, the function's reference parameter is assigned the address of the argument. In other words, pass by value copies the value, pass by reference passes the variable itself.Pass by reference is always the preferred method of passing arguments to functions when those arguments are complex objects. If the argument is a primitive data type then the cost in copying the value is minimal, but copying a complex object is expensive in terms of performance and memory consumption. If the function parameter is declare constant, you can be assured the object's immutable members will not be affected by the function. If it is non-constant and you do not wish your object to be altered, you can either copy the object and pass the copy, or pass the object by value if the function is overloaded to cater for this eventuality (good code will provide both options).


What is the output of C programming of binary searching?

The output of a binary search routine is either (usually) the address of the element that matched the search term, or, if there was no match, the address of where the new element should be placed. Of course, this means that there are two outputs, one, an address, and two, whether or not the search term was found; which means that a single valued function will not suffice - it is necessary that one of the parameters be an address, perhaps of the flag variable.


What do you mean by call value and call by reference?

Call by value and call by reference relate to the way in which arguments are passed into functions. The function itself determines which method is employed. When a function expects an argument by value, the function makes a copy of the argument you pass. That is, you are passing the argument's value, not the argument itself. The value you pass is subsequently assigned to the function argument. If the function argument is an object (an instance of a class), then that class' copy constructor is automatically invoked and the copy is assigned to the function argument. The function argument remains in scope until the function returns, which then invokes the class' destructor. Since the function works with a copy of the argument that you passed, the function has no direct effect on the argument you passed. Passing large and complex objects by value is clearly costly in terms of both performance and memory, but if you want the function to actually make changes to your object, then pass by value will not work unless you also return a copy of the altered object and assign this copy to your original object. That's two copy constructors, two destructors, and one assignment. This is where pass by reference comes in. When you pass by reference you are passing the address of your argument to the function, and that address is assigned to the function argument. So unless the function argument is declared const, any changes made to the function argument will be reflected in the argument you passed. This is clearly more efficient than passing by value, since no copies need to be made. And when the function argument is declared const, you can be assured that your object's immutable members will not be altered by the function since the function can only call const member functions. Passing by reference is clearly the preferred method of passing arguments into functions, particularly when those arguments are complex objects. In order to achieve pass by reference, the function argument needs to accept an address. Assigning the address to a pointer argument seems the obvious way of doing this, but in C++ there is a better way: use a reference! C programmers often get confused when we speak of C++ references because a reference in C is simply another name for a pointer. But in C++, a reference is not a pointer at all. Keep in mind that a pointer is a variable (or a constant), and therefore a pointer has an address of its own because it needs somewhere to store the address that it is pointing at. The stored address is the pointer's value, and that value can be dereferenced provided it is not NULL. That is, dereferencing a pointer gives us the indirect value stored in the address that the pointer is referring to. Moreover, pointer variables can be assigned any address at any time; they can point anywhere, or they can point to NULL. In C++, however, references behave differently to pointers. Once assigned, you cannot reassign a reference and they can never be NULL. Therefore you must assign a reference to an exiting object at the point of instantiation; you cannot declare and assign separately as you can with a pointer. This is because references do not store the address they refer to, they are simply aliases for the address itself. In other words, the address of a reference is the same as the reference itself. In many ways, a reference is not unlike a constant pointer, since it always refers to the same object, but even a constant pointer has its own address, a reference does not. Now that we understand what a reference is, we can see how it can be applied to functions that expect an address. When we pass our argument to a pass by reference function, we are assigning the address of the argument to the function argument, just as we would if the function argument were a pointer. The difference is that we don't need to use the address-of operator to obtain the address since the function already knows to expect a reference. That address is then assigned to the function argument. While the argument remains in scope, we have two guarantees: the reference cannot be NULL (so we don't need to test for NULL as we would with a pointer) and the reference cannot be reassigned (so its effectively the same as a const pointer). Moreover, since it is not a pointer of any kind, there is no redirection involved. The reference can be treated just as if it were the argument we passed; they are one and the same after all, just different names. To conclude, here are some examples of functions that employ pass by value and pass by reference: Example 1: pass by value MyObject foo(MyClass val) { val.set_data(42); return(val); } In this example we are passing a complex object of type MyClass that has a set_data() method. The object we pass is left unaffected because val is a copy of the object we passed. However, if we placed trace code in the class copy constructor and the destructor, we'd find two copies are constructed and destroyed during this call, once to create val, and again to create the return value. val is destroyed first and the return value second. The return value is an automatic variable which we can immediately assign to another object (including the original argument we passed to the function if we wished) but beyond that the return value will cease to exist. To call this function and assign the return value to the original object, we'd use the following: MyClass x; x = foo(x); Example 2: pass by reference (via pointer) void foo(MyClass* const ptr) { if( ptr ) { ptr->set_data(42); // (*ptr).set_data(42); } } Note that the points-to operator (->) is simply sugar-coating for the dereference operator, as shown in the commented line. Note also that the const keyword applies to the pointer itself, not what it points to. Thus *ptr is variable, but ptr is not. Here we're passing a reference by const pointer. Since pointers may be NULL we must ensure the pointer is non-NULL before we attempt to dereference it. Note that we do not need to return anything since ptr is assigned the address of the argument we passed, thus any changes made to the pointer's indirect value are reflected in the argument. In other words, ptr is an output argument. In order to call this function, we must employ the address-of operator. E.g., MyClass x; foo(&x); Example 4: pass by reference void foo(MyObject& ref) { ref.set_data(42); } Here we can see the C++ way of passing by reference. Unlike pointers we don't need to test for NULL, nor do we need to dereference. ref is guaranteed to always refer to a valid object. Moreover, the function call itself is simplified because we don't need to specify the address-of operator. MyClass x; foo(x); Note that ref is simply an alias for x. If we examine the address-of ref (&ref) we will find it is exactly the same as the address-of x (&x). If we look at the address-of ptr (&ptr) in the previous example, we'd find that it was not the same as &x, but its value would be &x, proving that references are neither pointers nor variables of any kind. Note that although these examples use complex objects, the same principal applies to primitives. The only difference is that there is no penalty in passing primitives by value as it's just as quick to pass by value as it is to pass by reference. It's just a question of whether you want changes reflected in the original argument or not. As a final note, if a function expects pass by reference, but you do not wish changes to be reflected in your object, copy the object first and pass the copy instead. This is really no different to pass by value. However, if the function argument is a constant reference, you don't need to make a copy since the function cannot alter the object's immutable members. You can also overload functions to pass by value and by reference, giving the best of both options. However, to achieve this, you need to use the pointer version of pass by reference. This is because the compiler cannot differentiate pass by value from pass by reference any other way.