Hi all,
There are four parameter passing mechanisms:
call by value
call by value-result
call by reference
call by name
i'm quite confused about them. could someone give me an explanation? examples would be more appreciated.
Thanks in advance.
Printable View
Hi all,
There are four parameter passing mechanisms:
call by value
call by value-result
call by reference
call by name
i'm quite confused about them. could someone give me an explanation? examples would be more appreciated.
Thanks in advance.
If someone had taught you that then they should be doing some revision. There are two ways two pass data to functions, nl. by value and by reference.
Whether the function returns a value from the function is irrelevant.
Pass by value :
This is when you pass a variable to a function like this :
A copy of 'x' is made and used in the function, so that you cannot change the original value of 'x'.Code:#include <stdio.h>
void function( int param )
{
param = 10;
}
int main()
{
int x = 5;
function ( x );
printf( "%d", x);
return 0;
}
Pass by reference:
This is when you pass the address of a variable to a function like this :
This time you pass the address of 'x' to the function so that you can change the original value of 'x'. By using the '*', on param in the function, we access the data that param points to, in this case 'x'.Code:#include <stdio.h>
void function( int *param )
{
*param = 10;
}
int main()
{
int x = 5;
function ( &x ); //The '&' signifies an address
printf( "%d", x);
return 0;
}
afaik, it's all passing by reference. when you get down to the assembly language underlying the program, you have something like this:
assuming it hasn't been optimized out by your compiler...Code://c program
int func() {
return 3;
}
eax is a 4 byte register. any value returned must be at most 4 bytes. (if you're compiling for a non-intel architechture, this may be different... but not too much different). even void functions return something because the register eax always exists. it's just that the c language won't let you work with void functions that way.Code:;assembly program
enter 0,0
mov eax,3
leave
ret
when you return a char, an int, a long, etc..., you're returning a value through eax. any value bigger than that is returned by reference (ie: by pointer).
i hope i cleared some things up... but i've never heard of passing by name or value-result before.
The website you provided is very helpful. But I'm still quite confused about call by name. I think an example might illustrate this question better.Quote:
Originally posted by Salem
http://wombat.doc.ic.ac.uk/foldoc/fo...i?call-by-name
You should be able to figure out the rest
Here is a procedure definition:
def q(a,b,c)
t = a
a = b
b = a+c
c = t
and the following program:
i = 1; j=2;
q(i,j,i);
print i, j;
what is printed when parameters are passed using
i. call by value
ii. call by value-result.
iii. call by reference
iv. call by name?
i. The answer is 1,2. Because i and j dont ever change.
ii. The answer is also 1,2. But it is just a coincidence.
iii. The answer is 1, 3.
are they correct so far?
iv. no idea yet.