Oct 5, 2011

Functions in C # programming language




Continuation of previous lesson we will review now the second type of types of functions:

2 - the function that receives the value of "parameter" but it does not return a value:

The following example illustrates the idea, beginning to create a function as follows:

(static void SecondMethod(int x

        {

            x = x * 10;

            Console.WriteLine( x );

        }


Here the function receives the value of parameter, then we will write in brackets the name of any variable receives the value that will enter the function, and of course we will write the type of this variable and its type will undoubtedly be of the same type of value to be passed to the function to be retained in the variable. As you can see, the function receives the value stored in x is then calculated as a result of multiplication with the number 10 and the result is stored again in the x and then print the result ..

Just call the function remained even carry out its work but it seems that it must send a value for this function in order to do its job and of course we will send the value that will be of integer type ....


                                                       SecondMethod(5);
clip_image001


As soon as the program start, "according to the starting point is at the Main" it as soon as the compiler faces phrase SecondMethod (5); it looks for the function that bear this name and pass it the value in brackets which is 5,,,, No. 5 enters into the function to replace the variable x, and the calculation is performed and the result is printed on the screen ..



Since the functions can be its appeals for more than once within the program, try to write the following sentence recall:


clip_image001 SecondMethod(6);





Here we called the same function "SecondMethod", but with other value which is 6, will be repeated the same process above but with the change of the parameter and therefore will change the result:


This is the full program:

namespace ConsoleApplication1
{
    class Program
    {
        static void SecondMethod(int x)
        {
            x = x * 10;
            Console.WriteLine( x );
        }
        static void Main(string[] args)
        {
            SecondMethod(5);
            SecondMethod(6);
        }
    }
}


The output will be as follows:

50

60

So that the first line in the output is the result of calling the function the first time, and the second line as a result of its appeals for the second time.


The final two types of functions followed up the next lesson.

"morse"


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