Saturday, March 26, 2016

C# - Working with Functions


Functions are a mandatory part of every basic application, this article will cover the main practical and theoretical aspects that you need when creating and using code functions


The Function structure

[Security type], Returned value, Function Name (Parameters)

Security Type – The security level of the method (Public, Private…)

Return Type – The return value of the function (variable, Array and no value(Void)).

Function Name – The function name that we need to use when calling it.

Parameters – The parameters that will be used in this function.

Note!
The function local parameters are relevant only to this function , therefore you can use the same variable names in other parts of your code.

How can we declare a new function...? 

Definition No'1:
The first option is to define a static method (we can call it via the class level, there is no need to create an object from the class).


namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
            //Calling a static 'Void' function:
            math.def1();

            //Calling a static function that will return a value
            int methoedresult = math.def2(3,4);
            Console.WriteLine(methoedresult);
        }
    }

    class math
    {
        public static void def1()
        {
            Console.WriteLine("Math Class");       
        }

        public static int def2(int num1 , int num2)
        {
            int result;
            return result= num1 * num2;
        }
    }

}

Definition No'2:
The second option is to define a ‘Non-Static’ method (in this scenario you will need to create a class instance to get access).

class Program
    {
        static void Main(string[] args)
        {
            //Creating a class instance : 

            math ClassInstance = new math();

            //Calling the functions using a class instance:
            ClassInstance.def1();

            //Calling a methoed that return value:
            int methoedresult = ClassInstance.def2(3, 4);
            Console.WriteLine(methoedresult);
        } 
    }

    class math
    {
        public  void def1()
        {
            Console.WriteLine("Math Class");        
        }

        public int def2(int num1 , int num2)
        {
            int result;
            return result= num1 * num2;
        }
    }
}



Working with the Function Parameters 

Option No'1: The ‘Value’ parameter
By default, parameters are sent to a method as “Value”, that’s means that any manipulation on this parameter in the function will not affect the original variable value (When using the default value we actually send a copy of the variable).

Code Example:
In the following code, you will see that I sent to my function a single parameter that already initialized outside the function , in the function I changed the variable value but as you can see this change does not affect the original value.

class Program
    {
        static void Main(string[] args)
        {
            int v1 = 10;
            Example1(v1);
            Console.WriteLine("Value after function manipulation: " + v1);    
        }

        private static void Example1(int a)
        {
            a = 90;
            Console.WriteLine("Value in function : " + a);
        }
    }


Result: 







Option No'2: The ‘Reference’ parameter
This option will allow us to initialize a variable prior to sending it to the function, the difference from the ‘Value’ type is that every change that made inside the function will affect the original variable.

Note!
The reference parameter is moved to the function as it is (the original value) and not a copy

Code Example:
In this code you can see that the manipulation made in the function body affects the original value located outside the function.

class Program
    {
        static void Main(string[] args)
        {
            int v1 = 10;
            Example1(ref v1);
            Console.WriteLine("The Value after function manipulation: " + v1);
        }

        private static void Example1(ref int a)
        {
            a = 90;
            Console.WriteLine("Value in function : " + a);
        }

Result: 







Option No'3: Send parameters by ‘Outer’:
This option allows us to send a variable to a function without the need to initialize it (The initialization performed inside the function body).

Code Example :

Class Program
    {
        static void Main(string[] args)
        {
            int v1 ;
            Example1(out v1);
            Console.WriteLine("Value after function manipulation: " + v1);
        }

        private static void Example1(out int a)
        {
            a = 90;
            Console.WriteLine("Value in function : " + a);
        }
    }

Result:

Same result as shown in the ‘Ref’ part.

No comments:

Post a Comment

My Presentations