Click here to hide categories Click here to show left categories

User: Home          welcome : Guest          Log In / Register here     

Function - Call by reference

In this method the reference variables of the actual arguments are created as formal arguments. Reference variables are aliases of the original variables and refer to the same memory locations as the original variables do. Since the formal arguments are aliases of the actual arguments, if we do any changes in the formal arguments they will affect the actual arguments.

For example (C++):

void swap(int &,int &);

void main ( )
int a,b;
clrscr ( );
cout<<"Before calling swap function: a="<<a<<" b="<<b;
swap (a,b);
cout<<"\nAfter calling swap function: a="<<a<<" b="<<b;
getch ( );

void swap(int &x,int &y)
int temp;
cout<<"\nInside swap function: x="<<x<<" y="<<y;

Here in the main ( ) function we have declared two variables a and b with values 10 and 20 respectively. So we have in memory:

When we call the swap ( ) function by address, two reference variables x and y are created and they are alias names given to a and b respectively. So we have in memory now:

Inside the swap ( ) function we have interchanged the values of x and y. Since x and y are aliases of a and b, any changes made to x and y will affect a and b. So after swap function we have in memory:

Share this article   |    Print    |    Article read by 2452 times
Rohit kakria
I am software developer, moderator of
Related Articles: Exception: System.IO.DirectoryNotFoundException: Could not find a part of the path 'G:\PleskVhosts\\\CommonFiles\words.txt'. at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy) at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options) at System.IO.StreamReader..ctor(String path, Encoding encoding, Boolean detectEncodingFromByteOrderMarks, Int32 bufferSize) at System.IO.StreamReader..ctor(String path) at usercontrols_relatedInterviewQuestions.BindRelatedArticles() in g:\pleskvhosts\\\usercontrols\RelatedArticleInterviewQuest.ascx.cs:line 37
Related Interview Questions: