While defining method we pass parameter to these methods to make the method dynamic. Parameters of methods are of two types.
- Input parameters.
- Output parameters.
By default every parameter of a method is an input parameter. So to declare a parameter as output we need to prefix the parameters either with the ref or out keyword.
public void Test (int x , ref int y)
OR
public void Test (int x , out int y)
Input parameters are used for sending values into methods for execution whereas output parameters are used for sending results out of the method of the execution.
Note: By using return types of a method also we can send result of a method outside the method. But return type of a method is capable of sending only one result outside of that method whereas with the help of output parameters it is possible to send more than one result out of that method.
Example
Passing default values to parameters of a method:
While defining methods it is possible to pass default values to parameters of a method.
public void Addnums (int x, int y=50, int z=25)
If parameters is define with default value then it is considered as optional parameters and a parameter without default value is a mandatory parameters.
Note: Mandatory parameter of a method must be in the first 1st place of the parameter of the list followed by optional parameter.
If a method is define with default value parameters while calling a method if the value is not sent to those parameters then it will make use of the default value of that parameter to execute the method.
No comments:
Post a Comment