C Sharp Function



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

. 

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.

About the author

Admin
Donec non enim in turpis pulvinar facilisis. Ut felis. Praesent dapibus, neque id cursus faucibus. Aenean fermentum, eget tincidunt.

0 comments:

Copyright © 2016 AspDotNet link .