Oct 5, 2011

Functions in C # programming language


  • The first lesson:                                The second lesson>>



Introduction:


As is well known in any language programming if we are faced with any great problem, the best way to solve them is divided into a group of small parts or what is known as module or functionso that each lead a particular function ... This tactic is usually known as divide and conquer.
This module or function in  C# known as Method and Classes, a programmer could write Method To define certain functions inviting (calling) them from any point of the program once mentioning its name to be implemented at that point.



Therefore, "Methods" function in the C #
 is at the centre of our talk in this lesson:

programmers to write either of the method for himself, then called (user-defined method), or the use of algorithmic functions defined in the same language (build in) and all that is to call here when needed for use in its programme. And we'll look in today's lesson how we can establish these user-defined method and then summoning of the Programme..

Details:




And will be completed as talk that the reader has experience in dealing with a few even # C. consider now look at the general way to write any method at # C:




access_ Modifier return-value-type Method-Name( parameter-list )



declarations and statements
}



The first line of the definition of the method so-called "method header" and containing:

access modifier  :

Method-name: Is the name of proving that we want to Definition, and the rules of writing the name here follows the same conditions in writing the names of the variables, and of course would be optional name.


Return-value-type: Is the kind of result, which will return by function , and if they return to the value of the function, it is back in one of the value.... for example, if there are function to return to the value of integer, we will write in this column "int", and so on. In the case of when they do not return, proving the value, we write here void.




Parameter-list: Are the values ​​that we send to the function......and you'll learn more when it will explain the types of functions.

As for the declarations and statements: they arethe commands that you implement this function.

And functions in any language with four types, I shall put each type with an application example of it:

1 - the function that does not return a value and not receiving any value"parameter"

And meaning will become clear through the following example:


static void firstMethod()



{

Console.WriteLine("FirstMethod was called ");

}


Here is the access Modifier is static ...


Because the function does not return a value, we will write "void" when thereturn-value-type ...


Because the function does not receive any value, we will write a blank square ...!


Here, the function result in a very easy position simply because they print the message "!! FirstMethod was called" .. So as soon as its advocacy must callthe function "calling" to type the message .. But how is call any function??


Once the name of the function only, it will be implemented .... As follows:


 firstMethod();


And the full program as follows:



namespace ConsoleApplication1
{
class Program
    {
     static void firstMethod()
      {
        Console.WriteLine("firstMethod was called !!");
      }
     static void Main(string[] args)
      {
            firstMethod();
      }
    }
}


Note that we are in the program we have created a function "FirstMethod" andcalled from inside the Main as the starting point of the program is directly after the Main ....


/ * Keep in mind that there is no meaning of a function without being called * / ..


The remaining types of functions in the next lessons

0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | Premium Wordpress Themes