C Sharp/Functions

From Wikiversity
Jump to navigation Jump to search

Lesson Goals[edit]

This lesson will focus on functions in the flow of programming in C# via the console. At the end of this lesson you should be able to:

  • Understand the OOPS point of view regarding the functions.
  • Understand the basic construct of functions.
  • Explain the different scope and use of functions.
  • Understand the relation between funcitons and other C# programming elements.
  • Create different types of functions.

What is a Function?[edit]

This is the smallest section of code in functional programming constructs in C# 3.0. Fuctions are very important sections from OOPS standpoint. In more simpler terms, a function encapsulates a block of code that can be called from other parts of the program. introduction of function increases code reusability. Literally you can tag a particular section of code with a name (funciton name) and just call that name from anywhere of your program. In C# a funciton may or may not have a return type. Functions more often are called as Methods. A funciton must be contained in a class. The basic structure of a funciton is as follows, -

<access-modifier> <return type> <name> (<parameter>)
{
  <execution step 1>
  <execution step 2>
  ...
  <execution step n>
}

Conversly to call a function you simply call it by its name with parameters, if any, within paranthesis as follows, -

DoSomething();

Passing Parameters in Function[edit]

The input parameters can be of two types, -

  • Value type
  • Reference type

If parameters are passed as value type then a new copy of it will be created before passing to the function. Whereas, for reference types only the addresses of the parameters are passed to the function. Meaning, for a value type parameter if the function changes the parameter value then the original variable does not get changed; whereas in case of reference type once the value gets changed in function then the original variable's value gets changed. Here are two examples,-

(1) Value Type Function

1 using System;
2 
3 namespace FunctionApp
4 {
5     class Program
6     {
7        static int  age = 30;
8         static void Main()
9         {
10            System.Console.WriteLine("Your birth year is {0}", BirthYear( age));
11            System.Console.WriteLine("if your age is {0}", age);
12            System.Console.ReadLine();
13        }
14        //function with value type parameter
15        private static string BirthYear( int tage)
16        {
17           int _age = DateTime.Now.Year - tage;
18            //Assigning the calculated value to the parameter
19           tage = _age;
20            return Convert.ToString(_age);
21
22        }
23    }
24}

Output

Your birth year is 1980
if your age is 30

(2) Reference Type Function

1 using System;
2
3 namespace FunctionApp
4 {
5     class Program
6     {
7        static int  age = 30;
8         static void Main()
9         {
10            System.Console.WriteLine("Your birth year is {0}", BirthYear(ref age));
11            System.Console.WriteLine("if your age is {0}", age);
12            System.Console.ReadLine();
13        }
14        //function with reference type parameter. Notice the use of ref keyword.
15        private static string BirthYear(ref int tage)
16        {
17           int _age = DateTime.Now.Year - tage;
18           //Assigning the calculated value to the parameter
19           tage = _age;
20           return Convert.ToString(_age);
21        
22        }
23    }
24}

Output

Your birth year is 1980
if your age is 1980

Notice that in both the code, on line number 19 we are updating the parameter value with the calculated value. In value type example, it is not affecting the variable. But in reference type it is changing the variable value as well. Also for reference type parameter you must use the keyword ref while refereing to a parameter (Second line of output).

N.B. - In both the example we can see two keywords prefixed the function BirthYear, private and static. Private is an access-modifier and static is a keyword that specifies that the function is directly accessible from anywhere in the code. There are many other access-modifiers in C#. These define who can access that function or method. Here is a comprehensive list of them, -

Access Modifiers Description
public Access is not restricted.
protected Access is limited to the containing class or types derived from the containing class.
Internal Access is limited to the current assembly.
protected internal This is a combination of previous two keywords. Here access is limited to the current assembly or types derived from the containing class.
private Access is limited to the containing type.

Output Parameters[edit]

As you already learned that functions has a return type and that is the output for a function. What if you need to have multiple outputs?

In the parameters list if you provide out keyword in front of a parameter then that becomes output parameter. And you can add as many out parameters. Here is the structure, -

<access-modifier> void <functionName>(ref <datatype> <parameter1>,<datatype> <parameter2>, out <datatype> <parameter3>,out <datatype> <parameter4>....)
{
<Execution steps>
}

Notice that when you are using parameter list to define output parameters then the return-type of the function would be void. Remember: ref type and out type parameters are, essentially, same, only difference is that, for ref type you need to provide a variable while calling the function and for out type, it is not required.

Polymorphism[edit]

There are two types of polymorphism observed in functions.

  • Compile-time polymorphism
  • Run-time polymorphism

Compile-time Polymorphism (Method Overloading)[edit]

Function Overloading or Method Overloading is a manifastation of Compile-time polymorphism. Function overloading happens when two functions with same name but with different number of parameters or different datatypes of parameter sets or different return type of the functions are present in same scope. Here are examples of three types of Overloading, -

(1) Different Number of Parameters

using System;

namespace FunctionApp
{
    class Program
    {
        static void Main()
        {
            Console.WriteLine("Enter your taxable sum...");
            double tAmt=Convert.ToDouble(Console.ReadLine());
            Console.WriteLine("Enter your tax rate...");
            string text=Console.ReadLine();
            double tRate=0;
            if (text != "")
            {
                tRate = Convert.ToDouble(text);
            }
            //instanciating the class
            Program _prog = new Program();
            if (tRate > 0)
            {
                Console.WriteLine("Your Tax amount is : {0}", _prog.Tax(tAmt, tRate));
            }
            else
            {
                Console.WriteLine("Your Tax amount is : {0}", _prog.Tax(tAmt));
            }
            System.Console.ReadLine();
        }
        /// <summary>
        /// function 1 with two parameters
        /// </summary>
        /// <param name="taxableAmt"></param>
        /// <param name="taxRate"></param>
        /// <returns>Tax Amount</returns>
        private double Tax(double taxableAmt, double taxRate)
        {
           return (taxableAmt*taxRate)/100;
        }
        /// <summary>
        /// function 1 with one parameters
        /// </summary>
        /// <param name="taxableAmt"></param>
        /// <returns>Tax Amount</returns>
        private double Tax(double taxableAmt)
        {
            double fixRate=8.33;
           return (taxableAmt*fixRate)/100;
        }
    }
}

Output

When you are providing both the inputs,-

Enter your taxable sum...
1000
Enter your tax rate...
10.2
Your Tax amount is : 102

When you are providing only taxableAmt,-

Enter your taxable sum...
1000
Enter your tax rate...

Your Tax amount is : 83.3

(2) Different datatypes of parameters

using System;

namespace FunctionApp
{
    class Program
    {
        static void Main()
        {
            Console.WriteLine("Enter first Value");
            string firstValue=Console.ReadLine();
            Console.WriteLine("Enter second Value");
            string secondValue=Console.ReadLine();
            int fVal, sVal;
            Int32.TryParse(firstValue, out fVal);
            Int32.TryParse(secondValue,out sVal);
            //instanciating the class
            Program _prog = new Program();
            //when integers entered
            if (Convert.ToString(fVal)==firstValue && Convert.ToString(sVal)==secondValue)
            {
                Console.WriteLine("The summation is = {0}", _prog.Add(fVal,sVal));
            }
            //When blank entered
            else if (firstValue == "" || secondValue == "")
            {
                Console.WriteLine("Type mismatch! Operation aborted!");
            }
            //When strings entered
            else if (Convert.ToString(fVal) != firstValue && Convert.ToString(sVal) != secondValue)
            {
                Console.WriteLine("The concatenation is : {0}", _prog.Add(firstValue, secondValue));
            }
            //For others
            else
            {
                Console.WriteLine("Type mismatch! Operation aborted!");
            }
            System.Console.ReadLine();
        }
        /// <summary>
        /// function with two parameters with integer datatype
        /// to add them
        /// </summary>
        /// <param name="param1"></param>
        /// <param name="param2"></param>
        /// <returns>summation</returns>
        private int Add(int param1, int param2)
        {
           return (param1+param2);
        }
        /// <summary>
        /// function with two parameters with string datatype
        /// to concatenate strings
        /// </summary>
        /// <param name="param1"></param>
        /// <param name="param2"></param>
        /// <returns>append</returns>
        private string Add(string param1,string param2)
        {
           return (param1+param2);
        }
    }
}

Output

When both the inputs are integers,-

Enter first Value
12
Enter second Value
23
The summation is = 35

When both the inputs are strings, -

Enter first Value
Wiki
Enter second Value
versity
The concatenation is : Wikiversity

When data is not correct,-

Enter first Value
12
Enter second Value
Wikipedia
Type mismatch! Operation aborted!

N.B. - C# determines which function to call based upon the method's signature. If you are defining two methods with the same name and the same number and type of passed arguments, you would get a compile-time error. That is why Overloading is an example of compile-time polymorphism.

Run-time Polymorphism (Method Overriding)[edit]

In many occasions you might face a situation where you need to modify a function of a parent class in a derived or child class. To do this .NET provides us the functionality called Function Overriding. This is more frequently used for the implementation of abstruct classes and interfaces. Function Overriding requires us to take note of the following keywords, -

  • virtual - Virtual function must have its implementation in the base class and may optionally be overridden in the child class if some additional functionality is required.
  • abstract - Abstruct function will only have its signature in the base class and must be overridden in the child class.
  • override - Override function is the function in the child class which overrides the same signatured function from its parent.
  • sealed - Only a overridden function can be declared sealed to stop it from any further overriding.

Remember: For overriding only two access-modifiers are allowed, public or internal and both the methods in parent as well as child should have that same.

Let's consider the following example, -

using System;

namespace FunctionApp
{
    abstract class ProgramBase
    {
        // Calculates the area of a square
        public virtual double AreaCalc(double length)
        {
            return (length * length);
        }
        // Signature of an abstruct method
        internal abstract string PrintText(string text);
        // For 'sealed' use test
        public virtual string Writer(string text)
        {
            return text;
        }
    }
    class ProgramChild:ProgramBase
    {
        // Calculates area of a circle
        public override double AreaCalc(double radius)
        {
            double pi = 3.142;
            //Uses the function from the base class to calculate the square
            return base.AreaCalc(radius) * pi;
        }
        // Implementation of the abstruct method.
        internal override string PrintText(string text)
        {
            return text;
        }
        // Overriding of the Writer method
        // and making that sealed
        public override sealed string Writer(string text)
        {
            return "child class " + text;
        }
        static void Main()
        {
            //Instantiating self
            ProgramChild _prog = new ProgramChild();
            Console.WriteLine(_prog.AreaCalc(10));
            Console.WriteLine(_prog.PrintText("Abstruct to Override Example."));
            Console.WriteLine(_prog.Writer("Example of Sealed!"));
            Console.ReadLine();
        }
    }
    //Uncomment the following section and build to check the use of sealed.
    //class ProgramGrandChild: ProgramChild
    //{
    //    public override string Writer(string s1)
    //    {
    //        return "Further child" + s1;
    //    }
    //}
}

Output

314.2
Abstruct to Override Example.
child class Example of Sealed!

Notice that here we have three classes (One class is commented.). the first class ProgramBase is the base class and like all base classes, it is abstruct. It has three methods of which one is abstruct, meaning has only signature. ProgramChild is the child class to the programBase. It is overriding all the methods of the base class and making one of them as sealed. In the main method it is using the methods to give us the output. Notice that Overriding only changes the inner code block of the method in the overrided section, not the signature. Also notice that, in AreaCalc method in ProgramChild we are calling the same method from the base class with the base prefix. One more thing to notice here is that a public method in the base class is a public method in the child class and same for internal. Try to change it and you will get a compile time error. Now to the check the sealed method, just uncomment the ProgramGrandChild class and try to compile. You will get the following error, -

'FunctionApp.ProgramGrandChild.Writer(string)': cannot override inherited member 'FunctionApp.ProgramChild.Writer(string)' because it is sealed

Function Hiding[edit]

Function Hiding or Function Shadowing is a variant of overriding. Functions of base classes, by default, are available to the derived class. In case there is a need to not to use the function from base class then you can subscribe to Hiding. This can be achieved using new keyword. Here is how, -

using System;

namespace FunctionApp
{
     class ProgramBase
    {
        // Calculates the area of a square
        public double AreaCalc(double length)
        {
            return (length * length);
        }
         // Overridable functions
        public virtual string Writer()
        {
            return "base class";
        }
    }
    class ProgramChild:ProgramBase
    {
        // Calculates area of a circle by hiding the 
        // function with same sigmature from base
        public new double AreaCalc(double radius)
        {
            double pi = 3.142;
            //Uses the function from the base class to calculate the square
            return base.AreaCalc(radius) * pi;
        }
        // Overriding of the Writer method
        // and making that sealed
        public override string Writer()
        {
            return "child class";
        }
        static void Main()
        {
            //Instantiating Child
            ProgramChild _prog = new ProgramChild();
            Console.WriteLine(_prog.AreaCalc(10));
            Console.WriteLine(_prog.Writer());

            // Casting Child to Base class
            ProgramBase _prog1 = _prog;
            Console.WriteLine(_prog1.AreaCalc(10));
            Console.WriteLine(_prog1.Writer());
            
            //Instantiating Base class
            ProgramBase _prog2 = new ProgramBase();
            Console.WriteLine(_prog2.AreaCalc(10));
            Console.WriteLine(_prog2.Writer());
            Console.ReadLine();
        }
    }
}

Output

314.2
child class
100
child class
100
base class

Here we have two functions AreaCalc and Writer. The first one gets hidden in the child class, whereas, the second function gets overridden in the child. In the Main function we first called both the functions through an instance of the child class and as you can see, it executed the implementation from the child. Till here both overriding and hiding behaved same. Now If we cast the child class instance to the parent class then things bocome interesting. You can see that the overridden function got executed from the child; but the hidden parent class got exposed here. As if you are executing the functions from an instance of the parent class, which is the case for the third scenario.

Meaning, hiding does not follow inheritance rather it just creates a new version. This becomes useful when you want to use your base class throughout your application, not the usual use of child class. N.B.- You can do away with the new keyword and the program will still run; but be ready to get a compiler warning as, -

'FunctionApp.ProgramChild.AreaCalc(double)' hides inherited member 'FunctionApp.ProgramBase.AreaCalc(double)'. Use the new keyword if hiding was intended.

Try to reproduce the same with the above code.

Property[edit]

This is a special type of function, which mimics the behavior of a variable.

<''access-modifier''> <''datatype''> <''property-name''>{  get;  set;}

This is the simplest structure. The get or set is optional. get returns the value and set stores the value. The beauti of it is its simplicity to use.

Further Studies[edit]

Following are some high-end stuffs related to functions. Hence, out of scope of this article.

  • Annonymous functions[1]
  • Closures[2]
  • Currying[3]
  • Lambda Expressions[4]

Practice Exercises[edit]

  • Create a program for Tower of Hanoi. Take the input of number of rings. Print out the steps.
  • Create a program that would take two inputs and if they are texts then concatenate, but if they are numeric, it will show the summation as result
  • Create a Tax Calculator that will calculate a person's state and federal tax based by taking inputs as follows,-

FirstName

LastName

Address

Taxable amount

And will return the tax amount. (Take predetermined values for various state tax rates).

References[edit]

  1. Anonymous function
  2. Closure (computer_science)
  3. Currying
  4. Lesson 1:Lambda Expressions

-- Pasaban 06:50, 23 February 2010 (UTC)

Where To Go Next[edit]

Topics in C#
Beginners Intermediate Advanced
Part of the School of Computer Science