Write a C Program to perform Call By Value and Call By Reference methods

By | 19.03.2017

C Program to perform Call By Value and Call By Reference methods


Write a C Program to perform Call By Value and Call By Reference methods. Here’s a Simple Program to perform Call By Value and Call By Reference methods in C Programming Language.


Call by Value


The call by value method of passing arguments to a function copies the actual value of an argument into the formal parameter of the function. In this case, changes made to the parameter inside the function have no effect on the argument.

By default, C programming uses call by value to pass arguments. In general, it means the code within a function cannot alter the arguments used to call the function.


Below is the source code for C Program to perform Call By Value method which is successfully compiled and run on Windows System to produce desired output as shown below :


SOURCE CODE : :


/*  Call by value*/

#include<stdio.h>
void value(int x, int y);
int main( )
{
        int a=5, b=8;
        printf("Before calling the function, a = %d and b = %d\n", a, b);
        value(a, b);
        printf("After calling the function, a = %d and b = %d\n", a, b);

    return 0;
}
void value(int x, int y)
{
        x++;
        y++;
        printf("Inside function x = %d , y = %d\n",x,y);
}

OUTPUT : :


Before calling the function, a = 5 and b = 8
Inside function x = 6 , y = 9
After calling the function, a = 5 and b = 8


Call by Reference


  • If data is passed by reference, a pointer to the data is copied instead of the actual variable as is done in a call by value. Because a pointer is copied, if the value at that pointers address is changed in the function, the value is also changed in main().
  • In call by reference, original value is modified because we pass reference (address).
  • Here, address of the value is passed in the function, so actual and formal arguments shares the same address space.Hence, value changed inside the function, is reflected inside as well as outside the function.

 


Below is the source code for C Program to perform Call By Reference method which is successfully compiled and run on Windows System to produce desired output as shown below :


SOURCE CODE : :


/* Call by reference*/

#include<stdio.h>
void ref(int *p, int *q);
int main()
{
        int a = 5;
        int b = 8;
        printf("Before calling the function, a = %d and b = %d\n", a, b);
        ref(&a, &b);
        printf("After calling the function, a = %d and b = %d\n", a, b);
        
        return 0;
}
void ref(int *p, int *q)
{
        (*p)++;
        (*q)++;
        printf("Inside function *p = %d, *q = %d\n", *p, *q);
}

OUTPUT : :


Before calling the function, a = 5 and b = 8
Inside function *p = 6, *q = 9
After calling the function, a = 6 and b = 9

Conclusion of Call By Value and Call By Reference :


Point Call by Value Call by Reference
Copy Duplicate Copy of Original Parameter is Passed Actual Copy of Original Parameter is Passed
Modification No effect on Original Parameter after modifying parameter in function Original Parameter gets affected if value of parameter changed inside function

If you found any error or any queries related to the above program or any questions or reviews , you wanna to ask from us ,you may Contact Us through our contact Page or you can also comment below in the comment section.We will try our best to reach upto you in the short interval.


Thanks for reading the post.

4.3 4 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments