Question:
In C++, what does it mean to 'pass' something?
C J
2010-06-10 02:18:52 UTC
Hello. Very basic C++ question here, though I am just starting out. What does it mean to pass a reference... or an array... or a variable ... or anything else?
Five answers:
JoelKatz
2010-06-10 02:31:47 UTC
When you pass a "reference", you are telling something else where a value is. Normally you pass by "value", which tells something else what a value is.



Say I have a blue ball. If I pass it to you by value, I'm telling you that I have a blue ball. If you change the ball to green, my ball is still blue.



Say I have a blue ball. If I pass it to you by reference, I'm telling you where my blue ball is. If you change the ball to green, my ball is now green.



The advantage to passing by reference is that it is very efficient. In the example, there is only one ball involved. In addition, passing by reference permits the thing called to modify the caller's object, which may be an advantage or a disadvantage, depending on what you're doing.



Ideally, arrays should be passed by reference. A reference to an array is tiny. The value of an array is huge.



For simple, native types, it makes no difference. For example, an 'int' is typically the same size as a reference (maybe even smaller on some platforms). So there's no benefit to passing it by reference except that the caller's value can be modified.
?
2010-06-10 03:00:40 UTC
Most programming languages uses the passing of the parameters to the functions. The parameter can be a variable or the array. In passing the parameters, one should know what the resultant output will be and when to use the corresponding method.



There are basically two types of parameter passing:

1. Pass by value

2. pass by reference

3. pass by const-reference





1. Pass by Value

------------------------------------

When a parameter is passed by value, a copy of the parameter is made. Therefore, changes made to the passed parameter by the called function have no effect on the corresponding actual parameter. Eg.

void fl(int n) {

n++;

}



int main() {

int x = 2;

fl(x);

cout << x;

return 0;

}



In this example, fl's parameter is passed by value. Therefore, although f increments its formal parameter n, that has no effect on the actual parameter x. The value output by the program is 2 (not 3).



2. Reference Parameters

------------------------------------------

When a parameter is passed by reference, the actual parameter itself is passed (and just given a new name -- the name of the corresponding formal parameter). Therefore, any changes made to the formal parameter affect the actual parameter.



Eg.



void fl(int &n) {

n++;

}



int main() {

int x = 2;

fl(x);

cout << x;

}

In this example, fl's parameter is passed by reference. Therefore, n in fl is actually changing variable x, so the output of this program is 3.



If the function whose purpose is to compute two or more values,we can use reference parameters.

For example, if you want to read a list of integers from a file, and you want to know both how many integers were read, as well as the average value that was read, you might use a function like the following:



void f(istream &input, int &numRead, double &average) {

int k, sum = 0;

numRead = 0;



while (intput >> k) {

numRead++;

sum += k;

}

average = (double)sum/numRead;

}



Another common use of reference parameters is for a function that swaps two values:

void swap( int &j, int &k ) {

int tmp = j;

j = k;

j = tmp;

}



This is useful, for example, in sorting an array, when it is often necessary to swap two array elements. The following code swaps the jth and kth elements of array A:

swap(A[j], A[k]);



3. Const-Reference Parameters

---------------------------------------------------

Another reason to use reference parameters is when you don't want the function to modify an actual parameter, but the actual parameter is very large, and you want to avoid the overhead of creating a copy. Of course, this only works if the function does not modify its formal parameter. To be sure that the actual parameter is not "accidentally" modified, you should use a const-reference parameter. Declaring the parameter to be const tells the compiler that it should not be changed.



Eg.

void f(const IntList &L) {

-- the code here cannot modify L --

}



Because L is a const-reference parameter, it is the compiler's job to be sure that L is not modified by fl (and that means that no data members of L are modified). The compiler doesn't know how the Print function is implemented; it only knows how it was declared, so if it is not declared const, it assumes the worst, and complains that function fl modifies its const-reference parameter L.



4. Array Parameters

------------------------------------------------------

Another unfortunate thing about C++ arrays is that they are always passed by reference (even though you don't declare them to be reference parameters). For example:



void fl(int A[]) {

A[0] = 5;

}



int main() {

int B[10];

B[0] = 2;

fl(B);

cout << B[0] << endl; // the output is 5

}

Although fl's parameter looks like it is passed by value (there is no &), since it is an array it is actually passed by reference, so the assignment to A[0] is really assigning to B[0], and the program prints 5 (not 2).
D Bügger™
2010-06-10 02:34:09 UTC
pass a variable,array etc. by reference or by value to means to send the variables to a function for processing.Ex.

void fun(int a)

{

printf("%d",a);

}

main()

{

int x;

fun(x); x is passed to function named fun.



If you have to pass some variable x of int type to a function fun(int) the syntax is fun(x).



When you pass the value to function the variable or array passed is replicated and all the processing is done on the replicated value.The original variables are maintained.



On passing the values by reference you pass the address of the values to the function and the function and thus any change caused by the function is visible on original value.
2016-12-16 12:02:44 UTC
you will nonetheless get credit for the type, definite. the only ingredient that ought to matter is that in case you have a professor that demands you pass the purely genuine to pass the type, yet whilst your very final grade says C you have a C.
?
2010-06-10 02:25:45 UTC
It is found in library under the section "Computers" Programming Language.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...