Saturday, January 12, 2013

5.7. Modifying parameters


We can modify the value of data received as a parameter, but the result might not be what we expect. Let's see an example:

/*---------------------------*/
/*  C# Example #53:          */
/*  example53.cs             */
/*                           */
/*  Modifying a variable     */
/*  received as a parameter  */
/*                           */
/*  Intro to C#,             */
/*    Nacho Cabanes          */
/*---------------------------*/

using System;

public class Example53
{

  public static void double(int x) {
    Console.WriteLine("  The received value is {0}", x);
    x = x * 2;
    Console.WriteLine("  and now it is {0}", x);
  }

  public static void Main()
  {
    int n = 5;
    Console.WriteLine("n is {0}", n);
    double(n);
    Console.WriteLine("Now n is {0}", n);
  }
 
}

The result of this program will be:

n is 5
  The received value is 5
  and now it is 10
Now n is 5

We see that when leaving the function, the changes we make to this variable received as a parameter are lost.

This is because, if not stated otherwise, the parameters "are passed by value", which means that the function does not receive the original data, but a copy of them. If we change something, we are changing a copy of the original data, not the real data.

If we want to retain the changes, we just need to make a small change: indicate that the variable is to be passed "by reference", using the word "ref", both in the function declaration and in the call, as follows:

/*---------------------------*/
/*  C# Example #54:          */
/*  example54.cs             */
/*                           */
/*  Modifying a variable     */
/*  received as a parameter 2*/
/*                           */
/*  Intro to C#,             */
/*    Nacho Cabanes          */
/*---------------------------*/

using System;

public class Example54
{

  public static void double(ref int x) {
    Console.WriteLine("  The received value is {0}", x);
    x = x * 2;
    Console.WriteLine("  and now it is {0}", x);
  }

  public static void Main()
  {
    int n = 5;
    Console.WriteLine("n is {0}", n);
    double(ref n);
    Console.WriteLine("Now n is {0}", n);
  }
 
}

In this case, the variable n is modified:

n is 5
  The received value is 5
  and now it is 10
Now n is 5

Being able to change values ​​received as parameters opens a possibility that we could not be achieve in any other way: now we can return more than one value. For example, we could create a function to exchange the values ​​of two variables:

public static void exchange(ref int x, ref int y)


The possibility of passing parameters by value and by reference exists in most programming languages. For C# there is also a possibility which is not available in other languages, the "output parameters". We will see it later.

Suggested exercises:
·       (5.7.1) Create a function named "swap" to exchange the value of the two integers that are indicated as parameters.
·       (5.7.2) Create a function named "initials", which accepts a string like "Nacho Cabanes" and returns the letters N and C (first letter, and letter located after the first space), using reference parameters.

No comments:

Post a Comment