Q:

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

0

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.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C Basic Solved Programs – C Programming

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Write a C Program to display student details using... >>
<< C Program to print value and address of elements o...