Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.
Trending News
5 Answers
- varunMLv 62 decades agoFavorite Answer
classic example
Suppose you want to write a function to exchange the values of two integer variables. It seems like that would be fairly trivial. For example, to swap x and y, you could just create an extra int variable, t, and write code that looks like this:
t = x;
x = y;
y = t;
That would certainly work. (Try giving x and y initial values & trace through the code if you don't see why the code works. Note that this approach is similar to what you did for the mandel() function in that programming assignment.)
So, you would probably suppose that you could write a function like this:
void swap (int x, int y) {
int t = x;
x = y;
y = t;
}
Now, suppose your main() looks like this:
int main(void)
{
int a = 5;
int b = 7;
swap( a, b );
printf("a is %d and b is %d\n", a, b);
}
What will print? If swap works, we expect "a is 7 and b is 5" to print. But what actually happens? When the function swap is called, the values of a and b are passed into the functions. When swap begins, x is initialized to 5 and y is initialized to 7. The values of x and y are swapped...but the values of a and b (in main) are unaffected. The swap function does not work as written above because the arguments were passed 'by value.'
With pointers, we can pass the values 'by reference.' Passing by reference means that we'll pass the addresses of the variables instead of the values. Here's how swap and main should be written:
void swap ( int *px, int *py) {
int t = *px;
*px = *py;
*py = t;
}
int main(void)
{
int a = 5;
int b = 7;
swap( &a, &b );
printf("a is %d and b is %d\n", a, b);
}
Now, the addresses of the variables a and b are passed into swap. In swap, t is set to the value of 'the data that px points to', which is just the value stored at the address of a, which is 5. Then the value stored at address px is changed to the value stored at address py. This sets a to the value of b. Finally, b is set to the value t, which was the value originally stored at a. So, the values of a and b were swapped.
- 2 decades ago
These r two method of function calling in any programming language. In Call by value we pass value which creates a duplicate value for the function but in case of call by reference we pass address of variable.
Source(s): my study - 2 decades ago
Simply pass by value is passing values alone to methods or functions while in pass by reference, address of the value is passed.
- 2 decades ago
o my god.i m fed up with this q. it always come in my exams.anyways i think varun's answer help u.
- How do you think about the answers? You can sign in to vote the answer.
- Anonymous4 years ago
one way: <enter id="in1" ... /> <enter id="in2" ... /> as quickly as you click a button/link, you call a javascript function: function foo() { var in1 = checklist.getElementById("in1").value; var in2 = checklist.getElementById("in2").value; window.section = "myphpfile.very own residing homestead website?a=" + in1 + "b=" + in2; }