bg287

csc110: Passing by Value or Reference

Posted in csc110 by bnmng on 2010 03/30

You’re a teacher, and you tested your students, but you gave each student a different test.

To one student, you provided a paragraph filled with mistakes, like “The Declaration of Independence was signed on April 1st, 1987”. You told that student to provide a clean, error-free paragraph on a new piece of paper. She asked, “What should I do with the paper you gave me?” and you replied, “It doesn’t matter; I have a copy.”

You also wrote some paragraphs on the board, and to one student, you provided a paper which said “Paragraph #2 on the board”. That student went up to the board and fixed the the paragraph right on the board.

To the first student, you passed a value. You gave the student a paragraph to fix. But you didn’t give that student you’re only copy, instead you gave that student something that had the same value as your original. And you didn’t want your paper back because it was just a copy. Instead you wanted a fresh, clean paper.

To the second student, you passed a reference. You didn’t actually give the student a paragraph; what you gave was the location of a paragraph. That student had to find the paragraph and fix it. When the student fixed the paragraph, he did so by altering the original. He didn’t have to give anything back to you because the only thing he had was a location.

Back to functions. You either pass the function a value or a reference. The value is a copy of the original; it has the same value. The reference is the location of the original; you’re telling the function to go get the original.

Passing by value is the default. To pass by reference, you have to write the prototype and the header slightly differently. This is a partial program with a function that takes a parameter by value:

int doublethis ( int ); 

void main () {
	int number=1;
	int doubled;
	doubled=doublethis(number);
	cout << doubled << endl;
}

int doublethis(int somenumber) {
	somenumber=somenumber*2;
	return somenumber;
}

This is a partial program with a function that takes a parameter by reference:

void doublethis ( int& ); 

void main () {
	int number=1;
	doublethis(number);
	cout << number << endl;
}

void doublethis(int& somenumber) {
	somenumber=somenumber*2;
}


The major difference is the “&” in the prototype and the header. This tells the function that the parameters which it will receive are references. So “int&” doesn’t mean that the function is getting an integer; it means that the function is getting an address to where an integer is stored.

Besides the differences in the prototype and header, the code is a little different. The function in the first program returns a value but one in the second doesn’t.

You can have many different combinations of references and values. Going back to the teacher/student example, you can tell you’re students to work on the board AND turn in a paper. You can even tell the student to fix the paragraph on the board AND turn in a paper with the number of problems that he fixed. The catch is you can only get one paper back ( and in my example, there is only one answer on a paper ). If you want more than one answer, you have to send the student to the board. But you can send him to the board as often as you like.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: