Sunday, July 12, 2009

Pls help me with C programming.what is meant by call by value and call vy reference of functions?

please explain in as simple and lucid a language as possible with examples and programmes.

Pls help me with C programming.what is meant by call by value and call vy reference of functions?
In C language, the default function call mechanism is "call by value". If you send an object to a function, the function takes the value of the object you sent, not the object itself.


{


int x = 20;


func(x);


}





In the above code, the foo function called, takes the value of x. After the function call, it is sure that the value of x remains unchanged.


in "Call by reference" mechanism a function can take the object itself, so a function can change an object it receives. In "C" language, "call by reference" is not the default case. There is no language syntax to send an object itself to a function. Instead, the address of the object is sent to a function. In this case, the related parameter of the function must be a pointer to object sent. see the following example:





void func_by_value(int a)


{


a = 20;


}





void func_by_reference(int *a)


{


*a = 20;


}





int main()


{


int x = 100;





func_by_value(x);


printf("x = %d\n", x);


func_by_reference(x);


printf("x = %d\n", x);





return 0;


}
Reply:If I pass a pointer instead of the actual object then it is pass by reference. Pass by reference allows the called function to modify the value of the object which is being pointed to by the pointer.


In the pass by value the value of the object is passed to the function. The function can use the value but not modify the original instance of it.
Reply:I seldom do C (doing Foxpro most of the time), but here's what I remember:





call by value: the variable is passed to a function that could not alter its original value. By default, C passes parameters to functions by value





call by reference: the variable is passed to a function that could modify it. You need to use pointer declaration "*" in the parameter declaration for the function.
Reply:to call this function:


void func(int x, char y);


you would have to use this command:


func(a,b);


this call is called call by VALUE





to call this function:


void func(int* x, char* y);


you would have to use this command:


func(%26amp;a,%26amp;b);


this call is called call by REFERENCE





call by value is used when you want to use the value of the


variable sent without changing thier value in the MAIN program





call by reference is used when you want to actually change the value in the MAIN program
Reply:check these


http://www.webmaster-talk.com/php-forum/...


http://www.thunderstone.com/texis/site/t...
Reply:Call by Value:


This is a procedure of passing value to other functions using normal variables like the following:





int add(int val1, int val2)


{


return val1+val2;


}





void main ()


{


int a, b;


a=4;


b=8;


printf ("The sum of %d and %d is %d", a, b, add(a,b)); //This is "call by value" means calling a function by passing the values as parameter.


}





Call by Reference:


This is a procedure of passing reference or address (of a variable) to other functions using pointers like the following:





int add(int *val1, int *val2) %26lt;- accepting address of a and b


{


return *val1+*val2;


}





void main ()


{


int a, b;


a=4;


b=8;


printf ("The sum of %d and %d is %d", a, b, add(%26amp;a,%26amp;b)); //This is "call by reference". means calling a function by passing the addresses of the variables as parameter.


}
Reply:C does not support true "call by reference" semantics. Passing a pointer does not qualify as call by reference. All that is passed to the function is the "value" of the pointer. It still is "pass by value". Read this link for a very good discussion on this:





http://groups.google.com/group/comp.lang...
Reply:call by value : the function is called by the value(variable) in its parameters


call by reference:the function is called by the address of the variable(reference (%26amp;) its parameters by having a pointer to the variable
Reply:thank you for asking question


call by reference


we call function using pointers (accessing the address)


in this way they are accessing the value directly example


int add(int *a,int *b)


return a+b;





call by value


we call function indirectly ,means that we don't access through address as wa do in call by reference but the value


whice we access is first copied somewhere and then accessed


int add(int a,int b)


return a+b;


No comments:

Post a Comment