Question

What is the difference between passing an argument by value and passing it by reference? Provide...

What is the difference between passing an argument by value and passing it by reference? Provide for examples to support your answer.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Passing an argument by value means just a copy of the variable is passed as formal argument from the calling function to called function as actual argument.Whenever we change the value of variable in called function the actual value of variable in the calling function will not be effected.

Source code:

#include<stdio.h>
void called(int a)
{
   printf("the value of variable %d \n",a); // value of a before it's value has been changed
   a=10; // value of a has been changed
   printf("the value of variable %d\n",a);
}
int main()
{
   int n;
   printf("enter the value of variable\n");
   scanf("%d",&n);
   called(n); // the value of n is passed as formal parameter to called function;
   printf("the value of variable after the called function has been called %d",n);
   return 0;
}

Output:

C:\Users\SURAJNDesktop\prime.exe enter the value of variable 5 the value of variable 5 the value of variable 10 the value of

Passing an argument by reference  means just address  of the variable is passed as formal argument from the calling function to called function which is accept by a pointer as actual argument in called function.Whenever we change the value of variable in called function the actual value of variable in the calling function will also be changed as we accessing the actual variable by its address.

.Source code:

#include<stdio.h>
void called(int *a)
{
   printf("the value of variable %d \n",*a); // value of a before it's value has been changed
   *a=10; // value of a has been changed
   printf("the value of variable %d\n",*a);
}
int main()
{
   int n;
   printf("enter the value of variable\n");
   scanf("%d",&n);
   called(&n); // the value of n is passed as formal parameter to called function;
   printf("the value of variable after the called function has been called %d",n); // the value of n is changed after called function
   return 0;
}

Output:

CAUsers\SURAADesktop\prime.exe enter the value of variable the value of variable 5 the value of variable 10 the value of vari

Add a comment
Know the answer?
Add Answer to:
What is the difference between passing an argument by value and passing it by reference? Provide...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT