Call By Value And Call By Reference

Call By Value And Call By Reference
Call By Value And Call By Reference

Call By Value And Call By Reference:-

Call By Value:-

In this post we will Learn about Call By Value And Call By Reference in detail and easy manner.

It is a default way for argument passing. Whenever argument is passed by value then a copy of argument is prepared and passed to the formal arguments that is newly created.

The formal argument containa copy of actual argument that is stored in different memory location so any changes made to this are not reflected to the actual argument since changes made in the formal argument are specific to that block which are lost if the control is returned to the calling function.

Example,

//program to show how call by value works?

#include<iostream.h>

#include<conio.h>

void main ()

{

clrscr ();

int a, b;

void swaping (int, int);

cout<<” Enter two number = “;

cin>>a>>b;

cout<<”\n Before calling a = “<<a<<”b = “<<b;

swaping (a, b);

cout<<”\n After calling  a = “<<a<<”b = “<<b;

getch();

}

void swaping (int x, int y)

{

int z;

z= x;

x= y;

y= z;

cout<<”\n After swapping  x = “<<x<<” y = “<<y;

}

Output :-

Enter two number= 10 25

Before calling a= 10 b= 25

After swapping  x = 25 y =10

After calling  a= 10  b= 25

 

Call By Reference:-

It allows the changes back to the actual argument without using pointer variable.It is very easy and simple way of argument passing.

Syntax,

DataType &referencevariablename  = variablename;

Example,

//program to show how pass by reference works?

void main ()

{

clrscr ();

int a, b;

void swaping (int &, int &);

cout<<” Enter two numbers = “;

cin>>a>>b;

cout<<”\n Before calling a= “<<a<<” b = “<<b;

swaping ( a, b);

cout<<”\n After calling a= “<<a<<” b = “<<b;

getch ();

}

void swaping (int &x, int &y)

{

int z;

z= x;

x= y;

y=z;

cout<<”\n After calling  x= “<<x<<” y = “<<y;

}

Output :-

Enter two numbers = 10 25

Before calling a= 10 b= 25

After calling  x= 25 y= 10

After calling  a= 25 b= 10

 

Call By Address (Call By Pointer):-

It is another way of passing argument to a function in which the address of argument is passed to called function.

Whenever any changes are made to the formal argument then all those changes are reflected to the actual argument.

The address of argument is passed by using an address of operator (&) before the name of a variable whose value we want to change.

The formal argument are preceded by asterisk (*) that works as a pointer variable to store the address of the actual argument.

Example,

//program to show how pass by address works?

void  main ()

{

clrscr ();

int a, b;

void swaping (int *, int *);

a= 10; b= 25;

cout<<” Before calling  a= “<<a<<”b = “<<b;

swaping (&a, &b);

cout<<”\n After calling  a= “<<a<<” b = “<<b;

getch ();

}

void swaping (int *x, int *y)

{

int z;

z= *x;

*z= *y;

*y= z;

cout<<” \n After calling  x= “<<*x<<” y = “<<*y;

}

Output :-

Before calling  a= 10 b= 25

After calling  x= 25 y= 10

After calling   a= 25 b= 10

 

A way to transferring data between the calling function and the called function is known as Argument Passing Techniques.  Above all are the same Techniques.

You Also Can Visit For More Knowledge In C++:-

Visit Your YouTube  Channel:-  Think , About It

Visit Your Website :- www.atozlives.com

Thanks,

“Play With All Computer Science , Mathematics  And Technology.”

 

 

71 COMMENTS

  1. Having read this I thought it was really informative. I appreciate you spending some time and energy to put this article together. I once again find myself personally spending way too much time both reading and leaving comments. But so what, it was still worth it!| Denise Byrom Goff

  2. Everything is very open with a clear clarification of the issues. It was truly informative. Your website is useful. Many thanks for sharing! Jany Rand Alastair

  3. Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincid unt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Mallissa Robers Timoteo

  4. you are able to often tell the quality of USB cables by looking in the thickness from the cable. thicker usb cables have greater quality` Allianora Lorrie Kristofor

  5. Awesome write-up. I am a regular visitor of your site and appreciate you taking the time to maintain the nice site. I will be a regular visitor for a long time. Glynnis Lew Sato

  6. Yes Sir, the situation remains exceedingly fluid and volatile at the border and MEA meeting at SCO cant maje a difference on the ground as the final decision has to come from much higher levels. Bernadene Alasdair Fulton

  7. After looking into a handful of the articles on your blog, I seriously appreciate your way of blogging. I book-marked it to my bookmark site list and will be checking back soon. Take a look at my website as well and tell me what you think. Benetta Ignazio Walton

  8. Its like you read my mind! You seem to know so much about this, like you wrote the book in it or something. I think that you can do with some pics to drive the message home a little bit, but instead of that, this is fantastic blog. An excellent read. I will definitely be back. Marlyn Lamar Taro

  9. Howdy, I think your blog might be having web browser compatibility problems. Whenever I look at your site in Safari, it looks fine however, when opening in I. E., it has some overlapping issues. I just wanted to provide you with a quick heads up! Aside from that, excellent blog! Arlette Pierre Halvaard

  10. Aw, this was a post that is really nice. In idea I wish to devote creating like this further ? getting some time genuine work to manufacture an extremely article that is good? exactly what can I state? I procrastinate alot and also by no means appear to bring some thing completed. Karlotta Yank Hannus

  11. Hello to all, how is everything, I think every one is getting more from this website, and your views are pleasant designed for new viewers. Nicky Derick Virnelli

  12. I like the simplicity of recognizing resistance in myself and others amid the complexity of all that is happening and changing everyday. Thank you for that perspective! Charla Arlan Schoenfelder

  13. Hey there. I discovered your blog by the use of Google whilst looking for a similar subject, your website got here up. It seems great. I have bookmarked it in my google bookmarks to come back then. Trix Waylen Williamson

  14. After looking over a handful of the articles on your website, I truly like your way of blogging. I saved as a favorite it to my bookmark website list and will be checking back in the near future. Please visit my web site too and tell me what you think. Norrie Algernon Krongold

  15. Write more, thats all I have to say. Literally, it seems as though you relied on the video to make your point. You obviously know what youre talking about, why waste your intelligence on just posting videos to your site when you could be giving us something enlightening to read?| Vanny Perren Vachil

  16. Can you use Clorox bleach with these Eco eggs or is it not necessary!! I love this product so far but I like to use bleach in my towels!! Thank you Lynnelle Wadsworth Kinson

  17. maam? pwd din po bang makahingi ng reviewer with answer key po? mag tatake din po kasi ako ng cs this coming august . mag seselfreview lang kac ako. thank you po Rodie Shelley Shanley

  18. You created some decent points there. I looked on-line for your issue and found most individuals may go in conjunction with with all your website. Joella Arny Sarena

  19. Hi i am kavin, its my first time to commenting anyplace, when i read this article i thought i could also make comment due to this sensible post. Celina Igor Steinway

  20. Howdy! I simply would like to give you a huge thumbs up for the great info you have here on this post. I am returning to your web site for more soon.| Merry Gunther Puff

  21. And the prayer of faith will save the sick, and the Lord will raise him up. And if he has committed sins, he will be forgiven. James 5:15 NKJV Kizzie Pincas Enalda

  22. Being from New Orleans where the Audubon Society does its best to care for animals, I guess I dont have as much reservation about Zoos as perhaps I should. I understand the concerns though. I would love to see the Polar Bears too though I may wonder how happy they are living in an environment somewhat different than their own. Overall, though, it seems that the Rotterdam Zoo is trying to make a positive impact on species and perhaps through breeding they will continue to help prevent extiction. Ddene Georas Cerallua

  23. 279528 859086An fascinating discussion is price comment. I believe which you need to write extra on this subject, it may well not be a taboo topic but typically individuals are not enough to speak on such topics. Towards the next. Cheers 580388 Roxi Nikolaos Fleda

  24. Hi there. I discovered your web site by the use of Google at the same time as searching for a similar matter, your site came up. It seems good. I have bookmarked it in my google bookmarks to come back then. Emilia Christoph Veats

  25. Greetings! Very useful advice in this particular article! It is the little changes that will make the greatest changes. Thanks a lot for sharing! Mella Antoni Readus

LEAVE A REPLY

Please enter your comment!
Please enter your name here