I’m Learning C# – Part 6

Today we will study about methods or functions. We already know one method which is main (). In the statement, static void main (string [] args), main is the name of the function that returns nothing or void. 

How does a method work? First we need to define a method with the return type if any and also mention the arguments that will be passed to that method. Then we need to call the method from our program. The advantage of this is we can call the method any number of times without having to actually repeat the lines of code over and over.

The following is a code that makes use of method ‘area’ to calculate and print the area of a circle when the radius is inputted.

static double area (double x)
{
return 3.14 * x * x;

}
static void Main (string [] args)
{
Console.WriteLine(“enter the number”);
double num = Convert.ToDouble(Console.ReadLine());
Console.WriteLine(“The area is {0}”, area(num));
}

If you have issues with remembering the order in which the arguments are defined in the methods, during passing you can name the arguments in the style given below:

static int volume (int x, int y, int z)
{
return x*y*z;

}
static void Main (string [] args)
{
int x, y, z;
Console.WriteLine(“The volume is {0}”, volume(x:4, z:5, y:2));
}

In these examples even though the parameters where passed to method and operations were performed on them, the actual values of the parameters remain the same. This is called ‘ call by value’.

static void num (ref int y)
{
y /= 2; ;

}
static void Main (string [] args)
{
int x = 10;
num(ref x);
Console.WriteLine(“x is {0} – call by reference”, x);
}

In call by reference, you are actually not making a copy of the parameter being passed but passing the memory location which means any operation you do in the method on the parameter will be stored in the same memory location itself. In the above case, the output will not be 10 but 5. Remember the ‘ref’ keyword is required in the method and while calling.

static void num (out int y)
{
y = 2;

}
static void Main (string [] args)
{
int x;
num(out x);
Console.WriteLine(“x is {0} – call by output”, x);
}

You use the out keyword when you want to pass value from the method to the main function. In this case, output is 2 which is nothing but the value of y passed into x through the out.

In the following code we use method overloading which means methods that have same name but only differ in the arguments.

static int sum (int x, int y)
{
return x+y;

}
static int sum(ref int x, ref int y)
{
x += 5;
y *= 2;
return x + y;

}
static int sum1(out int x, out int y)
{
x = 9;
y = 10;
return x + y;

}
static void Main (string [] args)
{
int x = 1, y = 2;
Console.WriteLine(“x is {0} y is {1} and returned values is {2}”, x,y,sum(y:3,x:5)); // x is 1 y is 2 and returned values is 8
sum(ref y, ref x);
Console.WriteLine(“x is {0} y is {1}”, x, y); //x is 2 y is 7
sum1(out y, out x);
Console.WriteLine(“x is {0} y is {1}”, x, y);//x is 10 y is 9
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s