Method Calling:-
When the methods are declared with parameters, they should be called with parameters. The methods with parameters are called by passing the value using the following mechanism:- Value
- Reference
- Output
Value: The parameters passed by value creates a separate copy in the memory. The following example shows the parameters passed by value:
void CalculateSum( int num1, int num2)
{
//…
}
void Accept()
{
int val1=10;
int val2=2;
CalculateSum(val1,val2);
}
Reference: The parameters passed by reference does not creates a separate copy of the variable in the memory. A reference parameter stores the memory address of the data member passed. The following example shows the parameters passed by reference:
void CalculateSum( ref int num1,ref int num2)
{
//…
}
void Accept()
{
int val1=10;
int val2=2;
CalculateSum( ref val1,ref val2);
}
Output: The output parameters are used to pass the value out of the method. The following example shows the parameters passed by reference:
void CalculateSum( ref int num1,ref int num2, out int result)
{
result=num1+num2;
}
void Accept()
{
int val1=10;
int val2=2;
int recieveVal;
CalculateSum( ref val1,ref val2,out recieveVal);
}
0 comments:
Post a Comment