Computer ForumsComputers  

Go Back   Computer Forums > Programmers Lounge > Programming Discussions

Reply
 
LinkBack Thread Tools Display Modes
Old 11-03-2004, 11:37 AM   #1 (permalink)
Newb Techie
 
Join Date: Oct 2004
Posts: 14
Default Parameter passing methods problem

Could someone help me with this. Passed by value would be the result i get from actually running this program right? But I am confused about the passed by reference. Can anyone solve this and explain how you got your answers. Thanks


void foo(int x, int y)
{
i = y;
y += 3;
}

void main()
{
i = 2;
j = 3;
foo(i, j);
printf("%d %d\n", i, j);
}

what would the output be if you:
passed by value:
passed by reference:
and passed by value-result:

thanks
LauraP is offline   Reply With Quote
Old 11-03-2004, 11:58 AM   #2 (permalink)
Ultra Techie
 
Join Date: Jun 2004
Posts: 973
Send a message via Yahoo to intercodes
Default

Yup, the above program is pass by value. If you want to make a 'pass by refernce call'' pass the address of variable 'i' and 'j' [ foo(&i,&j) ] and the arguments in the function should be declared as pointers [ void foo{int *x,int *y) ] and manipulate the variables inside the function 'foo' accordingly.

Sign
Intercodes
intercodes is offline   Reply With Quote
Old 11-03-2004, 12:11 PM   #3 (permalink)
Newb Techie
 
Join Date: Oct 2004
Posts: 14
Default

so for passed by reference it would be like this:

void foo(int *x, int *y)
{
i = *y;
*y += 3;
}

void main()
{
i = 2;
j = 3;
foo(&i, &j);
printf("%d %d\n", i, j);
}

Is this what you mean?
LauraP is offline   Reply With Quote
Old 11-03-2004, 12:25 PM   #4 (permalink)
Newb Techie
 
Join Date: Oct 2004
Posts: 14
Default

so for passed by reference it would be like this:

void foo(int *x, int *y)
{
i = *y;
*y += 3;
}

void main()
{
i = 2;
j = 3;
foo(&i, &j);
printf("%d %d\n", i, j);
}

Is this what you mean?
LauraP is offline   Reply With Quote
Old 11-03-2004, 01:43 PM   #5 (permalink)
True Techie
 
Join Date: Apr 2004
Posts: 168
Default

The difference is that if you pass by value only the value of i changes as y is a private variable in the function foo. However if you pass by reference both values change as the function is working with the variables i and j.

To be strict C is always pass by value, the use of the & symbol sort of simulates pass by reference as the compiler uses pointers to memory locations, but that is sort of pedantic.

Pointers should be used though because they help conserve memory space, ie. the pass by reference method only uses half the memory allocation of the pass by value for it's variables. They are also vital for the implementation of dynamic memory allocation.
__________________
--------------------

Nak

Is it just me, or does something smell suspicious about all this?
nak-1 is offline   Reply With Quote
Old 11-03-2004, 02:01 PM   #6 (permalink)
Newb Techie
 
Join Date: Oct 2004
Posts: 14
Default

so the code that i wrote for pass by reference is that the right way to go about it?
LauraP is offline   Reply With Quote
Old 11-03-2004, 02:25 PM   #7 (permalink)
True Techie
 
Join Date: Apr 2004
Posts: 168
Default

errr, quick brush up on my 'C' later....

yes it is, though I can't remember my printf formatting too well so I am assuming that bit is right. But the pointer stuff is.

& passes the address of the variable

hence the statement, p = &c, assigns the address of 'c' to the variable 'p'

* is called the indirecton operator and when applied to a pointer it accesses the variable the pointer points to. Hence d = *p would assign the value of 'c' in the above example to 'd'

You code is totally consistent with this therefore it's right.
__________________
--------------------

Nak

Is it just me, or does something smell suspicious about all this?
nak-1 is offline   Reply With Quote
Old 11-03-2004, 02:28 PM   #8 (permalink)
True Techie
 
Join Date: Apr 2004
Posts: 168
Default

Sorry, I should also add that you have declared the fuction foo properly by indicating that the values passed are pointers. Too easy to forget that one...
__________________
--------------------

Nak

Is it just me, or does something smell suspicious about all this?
nak-1 is offline   Reply With Quote
Old 11-04-2004, 03:11 AM   #9 (permalink)
Ultra Techie
 
Join Date: Jun 2004
Posts: 973
Send a message via Yahoo to intercodes
Default

yup, the code is perfect, except for few initializations . One is the prototype of the function and the second is the declaration of the two variables i and j.

Sign
Intercodes
intercodes is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -5. The time now is 10:02 PM.


Powered by vBulletin® Version 3.7.1
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.1.0