C#. Passing parameters to a method. Arguments and formal parameters. Examples of passing parameters to methods. Passing a reference to object of class into a method




Passing parameters to a method. Arguments and formal parameters. Examples of passing parameters to methods. Passing a reference to object of class into a method


Contents


Search other websites:

1. What are there arguments and formal parameters of the method? Example

Arguments are values that are passed to the method when it is called. Formal parameters are values that get the value of the arguments in the method. Formal parameters are used in a method to perform calculations, operations, or actions in a method.

When the method is called, the value of the arguments are copied (passed) to the formal parameters of the method. There are several ways to pass arguments to a method.

Example. Demonstration of passing an argument of an integer type to a method. The Sqr() method is given. This method takes an integer and returns the square of that number.

public int Sqr(int x) // x - formal parameter
{
    return x * x;
}

When calling a method from another code, the argument is copied to the formal parameter.

int a, b;
a = 5;

// call the Sqr() method
b = Sqr(a); // a - argument, b = 25
b = Sqr(15); // 15 - argument, b = 225

In the above code, the Sqr() method is called twice. The first time the value of the argument a = 5 is copied into the formal parameter x. The calculation is performed in the Sqr() method and the result (25) is returned from the method by the ‘return’ operation.

In the second call, in the same way, the value of the number 15, which is an argument, is copied into the formal parameter x. Then the calculation is performed and the result (225) is returned by the operator ‘return’.

 

2. What are the ways to pass an argument to a method?

In C#, there are two basic ways to pass an argument to a method:

  • passing by value. In this case, the argument value is copied into the formal parameter of the method, as described in the previous paragraph. With this method, all changes over the formal parameter that occur in the method do not affect the argument that was used in the call;
  • passing by reference. In this case a reference to the argument is passed to the method. Thus, if you change the formal parameter of the method, then the argument will change.


 

3. What way of passing the argument to the method is used by default in C#?

By default, passing an argument by value is used. That is, the value of the argument is copied to the formal parameter of the method and used there. Changing the parameter in the body of the method does not change the argument that was passed to the method.

 

4. An example that demonstrates passing by value the arguments of base types

The example demonstrates passing of arguments of such basic types as int, double, char.

// class that demonstrates passing by value
class PassingValue
{
    // passing by value of int type
    public void IntValue(int t) // t - formal parameter
    {
        t = 100; // This change has no effect on the argument
        return;
    }

    // passing by value of char type
    public void CharValue(char c)
    {
        c = 'F'; // a change of c does not affect the argument
    }

    // passing by value of double type
    public void DoubleValue(double d)
    {
        d = 12.333; // changing d in the method does not affect the argument
    }
}

class Program
{
    static void Main(string[] args)
    {
        PassingValue pv = new PassingValue();

        // call the methods in which argument values are changed
        int x = 15;
        pv.IntValue(x); // x = 15 after returning from method, that is does not change
        Console.WriteLine("x = {0}", x);

        pv.CharValue('M'); // a copy is made from the value 'M'

        double z = 3.88;
        pv.DoubleValue(z);
        // after returning from method z = 3.88, that is does not change
        Console.WriteLine("z = {0}", z);
        Console.ReadLine();

        return;
    }
}

In the example, the methods IntValue(), CharValue(), DoubleValue() change the values of the formal parameters within the methods. This change does not affect the arguments that are passed to these methods.

 

5. How the reference to the class object is passed to the method. Example

The class object itself is not passed directly to the method. However, a reference to the object is passed to the method. As in the case of base types, when a reference to an object is passed to a method, this reference is copied to the formal parameter of method. This means that the formal parameter in the method contains exactly the same value as the argument that was passed to the method.

Example. An object of the Triangle class is passed to the method. The Triangle class implements a triangle, which is given by the coordinates of points. In the Perimeter method of the Triangle class, the perimeter of the triangle is calculated.

// class that implements a triangle
class Triangle
{
    public double x1, y1, x2, y2, x3, y3;

    // constructor of class
    public Triangle(double _x1, double _y1, double _x2, double _y2, double _x3, double _y3)
    {
        x1 = _x1; y1 = _y1;
        x2 = _x2; y2 = _y2;
        x3 = _x3; y3 = _y3;
    }

    // a method that receives as input parameter a reference to an object of the Triangle class,
    // the method calculates the perimeter
    public double Perimeter(Triangle T)
    {
        double a, b, c;
        a = Math.Sqrt(Math.Pow(T.x1 - T.x2, 2) + Math.Pow(T.y1 - T.y2, 2));
        b = Math.Sqrt(Math.Pow(T.x1 - T.x3, 2) + Math.Pow(T.y1 - T.y3, 2));
        c = Math.Sqrt(Math.Pow(T.x2 - T.x3, 2) + Math.Pow(T.y2 - T.y3, 2));
        return a + b + c;
    }
}

As can be seen from the above code, the Perimeter() method receives as an input parameter a reference T to an object of the Triangle class. The reference itself is passed by value. Using the T reference, you can access the methods and data (fields) of the Triangle class.

Using the Triangle class and calling the Perimeter() method from another program code can be as follows:

...

// create the class object
Triangle tr = new Triangle(1, 3, 4, 5, 0, 7);

double p;

// an object (instance) of the tr class is passed to the Perimeter() method
p = tr.Perimeter(tr); // p = 12.20079

...

In the above code, a reference with the name tr to an object of class Triangle is declared. This reference is then passed to the Perimeter() method to calculate the perimeter.

 

6. Is it possible to change the internal data of the class object in the method to which the reference to this object is passed?

Yes it is. If an reference to the object is passed to the method, then this reference (argument) is copied into the formal parameter. After all, both the argument and the formal parameter point to the same object. Therefore, using the formal parameter of the method (reference), you can change the data of the object that was passed to the method.

For example. A DemoInt class is declared, in which there is:

  • internal variable t;
  • the Change100() method, which receives a reference to the DemoInt class. In the Change100() method, the value of the internal variable t of the input formal parameter is changed to 100.

For the purpose of demonstration, the following actions are performed in the Main() function of the Program class:

  • the object di of class DemoInt is created;
  • the value of the internal variable t of the object di becomes equal to 25;
  • the method Change100() is called, in which di.t becomes equal to 100;
  • check using the variable res is performed. The variable res = 100 instead of 25. This means that when a class object is passed using reference to a method, the data of this object can be changed in this method.
// class declaration that contains 1 internal data member
class DemoInt
{
    public int t; // internal variable

    // method gets a reference to an object of the class DemoInt
    public void Change100(DemoInt di)
    {
        di.t = 100; // change the data member of object di
    }
}

class Program
{
    static void Main(string[] args)
    {
        // creating a class object
        DemoInt d = new DemoInt();

        // change value in class object
        d.t = 25;

        // passing a reference to a class object
        d.Change100(d); // the method changes the value of d.t

        int res = d.t; // res = 100, a change occurred: d.t = 100

        return;
    }
}

 


Related topics