Click here to Skip to main content
11,437,713 members (39,434 online)
Click here to Skip to main content

Tagged as

Parameter passing technique

, 22 Oct 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
Passing parameters to a function by Value and by Reference.

Pass by Value

When we pass the parameter to a function by value, the parameter value of the caller is copied to the function parameter. Consider the below Example:

//The Function
private void PassByVal_X(int x)
{
    //Increment the value passed in
    x++;
}
//Calling Code
int testPassByVal;
testPassByVal = 10;
PassByVal_X(testPassByVal)

In the example, the caller passed the testPassByVal by value to the function PassByVal_X. The value of 10 from testPassByValue is copied to the parameter x.

Hence, the changes done on the variable x, inside the function not reflected back to the caller on the variable testPassByVal


Pass by Reference

When we pass the parameter to a function by reference, the reference to the actual value of the caller is copied to the function parameter. So, at the end there are two references pointing to the same value, one from the caller, and one from the function.

Have a look at the below example:

private void PassByRef_X(ref int x)
{
    //increment the value passed in
    x++;
}
 
//Calling Code
int testPassByRef;
testPassByRef = 10;
testPP.PassByRef_X(ref testPassByRef);

1) In this Example, the variable x and the variable testPassByRef pointing to the same location where the value 10 is stored.

2) Note the usage of the ref keyword. We are indicating the compiler that value is passed by reference.

3) Since the value is passed by reference, the increment of value x in side the function is reflected back to the caller in the variable testPassByRef.

4) When we are passing by reference, the variable should be initialized before passing it to a function. Otherwise, we do get compilation error.


Pass by Reference with OUT

When we pass the parameter by reference, the compiler expects that we should initialize it. If there is situation that value is declared outside the function, and the same variable will be assigned some value inside the function. In this case, we should go for Pass by reference, but, not with ref. Because, ref expects that the value should be initialized. The Out keyword is used in this situation.

Both ref and out are meaning that we are passing by reference. The only difference is for out, the variable is not required to be initialized before passing it as parameter.

Look at the below example:

private void PassByRefOut_X(out int x, int y)
{
    //Assign some value by multiplying y with y
    // x is assigned first here, after the assignment
    x = y * y;
}
 
//calling code
int testPassByRefOut;
testPP.PassByRefOut_X(out testPassByRefOut, 10);

Note the usage out keyword. It implies that the parameter is still treated as Pass By Reference. But, now we no need to initialize it and it must get assigned inside the function.

The behaviour is same as the previous exmple. That is eventhough x is calculated inside the function PassByRefOut_X the calculated value reflected back on the variable testPassByRefOut

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Sivaraman Dhamodharan
Software Developer iSOFT
India India
I am working as software engineer in iSOFT R&D. I have been come accross C++,MFC, .net technologies. I do like playing video games, reading books.

Web: www.mstecharticles.com


Comments and Discussions

 
GeneralMy vote of 1 Pin
maheshbisht28-Apr-12 10:08
membermaheshbisht28-Apr-12 10:08 
GeneralReason for my vote of 5 Easily understandable Pin
vijaymmer13-Dec-10 19:27
membervijaymmer13-Dec-10 19:27 
GeneralGood Point. Thanks. Pin
sirama200427-Oct-10 16:59
membersirama200427-Oct-10 16:59 
GeneralReason for my vote of 3 You have forgotten to mention that c... Pin
ArchElf27-Oct-10 10:38
memberArchElf27-Oct-10 10:38 
GeneralReason for my vote of 4 Nice Tip Pin
krishnaraj4026-Oct-10 6:14
memberkrishnaraj4026-Oct-10 6:14 
GeneralReason for my vote of 4 I have used ref often... hardly used... Pin
Blubbo26-Oct-10 3:51
memberBlubbo26-Oct-10 3:51 
GeneralReason for my vote of 2 Not much value added. This should be... Pin
ddboarm26-Oct-10 3:33
memberddboarm26-Oct-10 3:33 
GeneralReason for my vote of 5 Now I get it Pin
StefanHam25-Oct-10 21:17
memberStefanHam25-Oct-10 21:17 
GeneralReason for my vote of 2 Not much value added. Should never u... Pin
Crawfis25-Oct-10 16:40
memberCrawfis25-Oct-10 16:40 
GeneralReason for my vote of 3 Excelente tip Pin
AlexZaga25-Oct-10 13:12
memberAlexZaga25-Oct-10 13:12 
GeneralReason for my vote of 3 I already know about it. The last pa... Pin
Sivaraman Dhamodaran22-Oct-10 22:23
memberSivaraman Dhamodaran22-Oct-10 22:23 
GeneralThanks for the information Silic0re09. Pin
sirama200422-Oct-10 7:00
membersirama200422-Oct-10 7:00 
GeneralSmall correction to the last part.... int x = default(int); Pin
Silic0re0922-Oct-10 4:14
memberSilic0re0922-Oct-10 4:14 
GeneralYou need to be really careful here, your tip/trick is valid ... Pin
Silic0re0922-Oct-10 4:11
memberSilic0re0922-Oct-10 4:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150506.1 | Last Updated 22 Oct 2010
Article Copyright 2010 by Sivaraman Dhamodharan
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid