Latest updates

[5]

C# Function or Method

Method is the building block of object-oriented programming.  It combines  related code together and makes program easier. In C# method declaration,

you can declare method by following way:

< Access Specifier> < Return Type> < Method Name> (Parameter list)
 {
    Body
 }


Example:



 public void add()
 {
      Body
 }



In the preceding example, the public is an access specifier,  void  is a return data type that return nothing and add() is a method name. There is no parameter define in add() method.

If the function returns interger value, then you can define function as follow:




public void add()
 {
      Body
 }


If the function is returning string value, then you can define function as follow:


 public string printname()
 {
      Body
 }


You must remember, whenever use return data type with method, must return value using return keyword from body. If you don’t want to return any value, then you can use void data type.


let's understand how functions are called and created.




Here we are calling a function abc().


class xyz

{

static void Main()

{

abc();

}

}


On executing this program you will get, an error. Compiler Error (5,1): error CS0103: The name 'abc' does not exist in the class or namespace 'xyz'
                                                                                                                                               
In any case, the error says that abc does not exist. Here we are calling a function called abc(), but where is abc() defined or created ? It is not a function that has been provided by C# to us free of charge. It is our own homegrown function that we are calling. The lesson here is that we cannot call a function without first creating it. So, to rectify this error we will first create a function abc. Our next example demonstrates this.

class xyz
{
static void Main()
{
abc();
}


static void  abc()

{
System.Console.WriteLine ("Suhail");
}


}


Output
Suhail

In the function abc, we have included only one statement- WriteLine within the curly braces. The '{' and '}' braces indicate the beginning and the end of this function. Alternatively, a function can contain millions of lines of code that will be executed when the function is called. Since everything is contained in a class, our function abc is also created within the class xyz but outside Main. But the function is called from Main. On executing the program, 'Suhail' is displayed. This is because we have included the code for 'Suhail' to be displayed in the function abc. Thus, when the control reaches the line abc(); it searches for that function and executes the code within that function. We will explain static and void later as promised. 

You can call as many functions as you like from your program. But you must remember to separate each one with a semi-colon. The next program illustrates this.


class xyz
{

static void Main()

{
abc();

pqr();

abc();
}
                                                                                                                                           
static void  abc()

{
System.Console.WriteLine ("I am ABC");
}

static void  pqr()

{
System.Console.WriteLine ("I am PQR ");
}

}

Output
I am ABC
I am PQR
I am ABC

At first the function abc is called, then pqr and then again we are calling abc. On executing this program  'I am ABC', 'I am PQR' and 'I am ABC' will be displayed.  This is because we have included the code for these lines to be displayed in the respective functions.

In the following program we are calling the function pqr from abc and not from Main.


class xyz
{
static void Main()
{
abc();
}
static void  abc()
{
pqr();
System.Console.WriteLine ("I am ABC");
}
static void  pqr()
{
System.Console.WriteLine ("I am PQR ");
}
}

Output
I am PQR
I am ABC

In the function abc, we are first calling pqr and then displaying 'I am ABC' using the WriteLine function. Hence, first 'I am PQR' is displayed and then 'I am ABC'. Thus, this program demonstrates how functions can be called from other functions.

Now that we have created our own functions abc and pqr, we have an intuitive understanding of how C# created the function System.Console.WriteLine.

The next program uses the printing or formatting capabilities of the WriteLine function.


class xyz
{
static void Main()
{
System.Console.WriteLine("100 {0}",100);
}
}

Output
100 100

The zero in the curly braces means that after the first comma there is some value and that it should display this value. You cannot differentiate between the two 100's. The {0} is replaced with 100, the number, which follows the first comma. If you don't like the number 100, use the number 420 instead. We won't mind - at least it's something that some of you can easily identify with!

The program below is simply an extension of the above.


class xyz
{
static void Main()
{
System.Console.WriteLine("100 {0},{1}",100,200);
}
}

Output
100  100,200

Here the {0} is replaced with 100 and {1} is replaced with 200. The comma (,) separates the two numbers. Thus {0} means the first number and {1} means the second. C# likes to count from zero and not one.
C# Function or Method C# Function or Method Reviewed by Admin on 03:31:00 Rating: 5

No comments:

Sora Templates