Call by value and call by reference in C-Important topics in C-computer science/Information technology
Call by value and Call by Reference in C
->Watch this for FUN - Funny job interview
Are you getting bored with online lectures?
->Watch this - When online lectures are boring
There are two ways to call out a function in C and those are Call by Value and Call by Reference. So, what they are, let's find out.
<><><><><><><>
Call by value:
Here the values in the function get copies of the original values but they don't get a revert to the modified value.
For example:
void swap(int x, int y)
{
int temp=x;
x=y;
y=temp;
}
int main()
{
int a=5,b=7;
swap(a,b);
printf("%d %d\n",a,b);// Would print 5 7 although the values got modified in the function.
}
This was Call by value.
<><><><><><><>
Call by Reference:
Here the values which get modified in the function would get reflected back in the main function by using the address and the pointers concepts in C programming language.
Taking the same example above:
void swap(int *x,int *y) // Using pointers.
{
int temp=*x;
*x=*y;
*y=temp;
}
int main()
{
int a=5,b=7;
swap(&a,&b); // Using Addresses.
printf("%d %d\n",a,b);// Would print 7 5 as the values got modified in the swap function.
}
This was Call by Reference.
Are you getting bored with online lectures?
Watch this - When online lectures are boring
Watch this for FUN - Funny job interview
<><><><><><><>
Hope you have learned
something new
today.
Visit other posts
by clicking on
LABELS.
<><><><><><><>
Do follow and comment
and don't forget to
SUBSCRIBE.
<><><><><><><>
Comments
Post a Comment