2. PARAMETERS
A parameter is an intrinsic property of the
procedure, included in its definition.
Parameter passing methods are the ways in
which parameters are transferred between
functions when one function calls another.
4. Formal Parameters
Formal parameters are written in the function prototype and
function header of the definition.
Formal parameters are local variables which are assigned values
from the arguments when the function is called.
5. Example of Formal
Parameters
Return type Function name Formal parameter
float CircleArea (float r) {
const float Pi = 3.1415;
Local object
Definition return Pi * r * r;
}
Return statement Function body
6. Actual Parameters
When a function is called, the values
(expressions) that are passed in the call are called
the arguments or actual parameters (both terms
mean the same thing).
The time of the call each actual parameter is
assigned to the corresponding formal parameter
in the function definition.
7. Example of Actual Parameter
Actual parameter
cout << CircleArea(MyRadius) << endl
To process the invocation, the function that contains
the insertion statement is suspended and
CircleArea() does its job. The insertion statement is
then completed using the value supplied by
CircleArea().
8. Parameter Passing
There are four different ways of passing
parameters to a method in C# which are as follows:
•1. Value
•2. Ref (reference)
•3. Out (reference)
•4. Params (parameter arrays)
9. Passing parameter by value
•By default, parameters are passed by value. In
this method a duplicate copy is made and sent to
the called function. There are two copies of the
variables. So if you change the value in the
called method it won't be changed in the calling
method.
•We use this process when we want to use but
don't want to change the values of the
parameters passed.
11. {
// local data members have to initialized as they are not initiated with class
constructor
int a = 10, b = 20;
XX obj = new XX();
Console.WriteLine("sum of a and b is : " + obj.sum(a, b));
Console.WriteLine("Value of a is : " + a);
Console.WriteLine("Value of b is : " + b);
Console.ReadLine();
}
}
}
12. In the above code we changed the values of data
member a and b but it is not reflected back in the
calling method. As the parameters are default
passed by value.
13. Passing parameter by reference
• Passing parameters by ref uses the address of the actual
parameters to the formal parameters. It requires ref
keyword in front of variables to identify in both actual and
formal parameters.
• The process of ref is bidirectional i.e. we have to supply
value to the formal parameters and we get back processed
value.
• We use this process when we want to use or change the
values of the parameters passed.
15. {
// local data members have to initialized as they are not initiated with
class constructor
int a=10 , b=20 ;
XX obj = new XX();
Console.WriteLine("sum of a and b is : " + obj.sum(ref a, ref b));
Console.WriteLine("Value of a is : " + a);
Console.WriteLine("Value of b is : " + b);
Console.ReadLine();
}
}
}
16. Passing parameter by out
• Like reference parameters, output parameters don't create
a new storage location and are passed by reference. It
requires out keyword in front of variables to identify in
both actual and formal parameters.
• The process of out is unidirectional i.e. we don't have to
supply value to the formal parameters but we get back
processed value.
• We use this process when we want some parameters to
bring back some processed values form the called method.
18. // local data members have to initialized as they are not initiated with class
constructor
int a = 10, b = 20;
// the out data members doesn't need to assign initial value as they are
processed and brought back
int c, d;
XX obj = new XX();
Console.WriteLine("sum of a and b is : " + obj.sum(a, b, out c, outd));
Console.WriteLine("Value of a is : " + a);
Console.WriteLine("Value of b is : " + b);
Console.WriteLine("Value of c is : " + c);
Console.WriteLine("Value of d is : " + d);
Console.ReadLine();
}
}
}
19. Passing parameter by param
(parameter arrays)
• Params parameters "params" parameter is a very useful
feature in C#. It is used when we don't know the number
of parameters will be passed to the called method. Param
can accept multiple values or "params" should be a single
dimensional or a jagged array.
• The params keyword lets you specify a method parameter
that takes an argument where the number of arguments is
variable.
• No additional parameters are permitted after the params
keyword in a method declaration, and only one params
keyword is permitted in a method declaration.
20. example
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace param_parameter
{
class Program
{
class XX
{
public void print(params int[] numbers)
{
foreach(int x in numbers)
{
Console.WriteLine(" " + x);
}
}
}
static void Main(string[] args)
{
21. int[] numbers = { 1, 2, 3, 4, 5, 6 };
int a = 10, b = 20, c = 30, d = 40;
XX obj = new XX();
obj.print(a, b, c, d);
obj.print(numbers);
Console.ReadLine();
}
}
}