Movatterモバイル変換


[0]ホーム

URL:


Jump to content
Wikiversity
Search

C Sharp/Functions

From Wikiversity
<C Sharp

Lesson Goals

[edit |edit source]

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 |edit source]

This is the smallest section of code in functional programming constructs in C# 3.0. Fuctions are very important sections fromOOPS 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 asMethods. 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 itsname withparameters, if any, within paranthesis as follows, -

DoSomething();

Passing Parameters in Function

[edit |edit source]

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

1usingSystem;23namespaceFunctionApp4{5classProgram6{7staticintage=30;8staticvoidMain()9{10System.Console.WriteLine("Your birth year is {0}",BirthYear(age));11System.Console.WriteLine("if your age is {0}",age);12System.Console.ReadLine();13}14//function with value type parameter15privatestaticstringBirthYear(inttage)16{17int_age=DateTime.Now.Year-tage;18//Assigning the calculated value to the parameter19tage=_age;20returnConvert.ToString(_age);2122}23}24}

Output

Your birth year is 1980if your age is 30

(2) Reference Type Function

1usingSystem;23namespaceFunctionApp4{5classProgram6{7staticintage=30;8staticvoidMain()9{10System.Console.WriteLine("Your birth year is {0}",BirthYear(refage));11System.Console.WriteLine("if your age is {0}",age);12System.Console.ReadLine();13}14//function with reference type parameter. Notice the use of ref keyword.15privatestaticstringBirthYear(refinttage)16{17int_age=DateTime.Now.Year-tage;18//Assigning the calculated value to the parameter19tage=_age;20returnConvert.ToString(_age);2122}23}24}

Output

Your birth year is 1980if your age is 1980

Notice that in both the code, on line number 19 we are updating the parameter value with the calculated value. Invalue type example, it is not affecting the variable. But inreference 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 functionBirthYear,private andstatic.Private is an access-modifier andstatic 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 ModifiersDescription
publicAccess is not restricted.
protectedAccess is limited to the containing class or types derived from the containing class.
InternalAccess is limited to the current assembly.
protected internalThis is a combination of previous two keywords. Here access is limited to the current assembly or types derived from the containing class.
privateAccess is limited to the containing type.

Output Parameters

[edit |edit source]

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 provideout keyword in front of a parameter then that becomes output parameter. And you can add as manyout 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 thereturn-type of the function would bevoid.Remember:ref type andout type parameters are, essentially, same, only difference is that, forref type you need to provide a variable while calling the function and forout type, it is not required.

Polymorphism

[edit |edit source]

There are two types of polymorphism observed in functions.

  • Compile-time polymorphism
  • Run-time polymorphism

Compile-time Polymorphism (Method Overloading)

[edit |edit source]

Function Overloading or Method Overloading is a manifastation ofCompile-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

usingSystem;namespaceFunctionApp{classProgram{staticvoidMain(){Console.WriteLine("Enter your taxable sum...");doubletAmt=Convert.ToDouble(Console.ReadLine());Console.WriteLine("Enter your tax rate...");stringtext=Console.ReadLine();doubletRate=0;if(text!=""){tRate=Convert.ToDouble(text);}//instanciating the classProgram_prog=newProgram();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>privatedoubleTax(doubletaxableAmt,doubletaxRate){return(taxableAmt*taxRate)/100;}/// <summary>/// function 1 with one parameters/// </summary>/// <param name="taxableAmt"></param>/// <returns>Tax Amount</returns>privatedoubleTax(doubletaxableAmt){doublefixRate=8.33;return(taxableAmt*fixRate)/100;}}}

Output

When you are providing both the inputs,-

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

When you are providing only taxableAmt,-

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

(2) Different datatypes of parameters

usingSystem;namespaceFunctionApp{classProgram{staticvoidMain(){Console.WriteLine("Enter first Value");stringfirstValue=Console.ReadLine();Console.WriteLine("Enter second Value");stringsecondValue=Console.ReadLine();intfVal,sVal;Int32.TryParse(firstValue,outfVal);Int32.TryParse(secondValue,outsVal);//instanciating the classProgram_prog=newProgram();//when integers enteredif(Convert.ToString(fVal)==firstValue&&Convert.ToString(sVal)==secondValue){Console.WriteLine("The summation is = {0}",_prog.Add(fVal,sVal));}//When blank enteredelseif(firstValue==""||secondValue==""){Console.WriteLine("Type mismatch! Operation aborted!");}//When strings enteredelseif(Convert.ToString(fVal)!=firstValue&&Convert.ToString(sVal)!=secondValue){Console.WriteLine("The concatenation is : {0}",_prog.Add(firstValue,secondValue));}//For otherselse{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>privateintAdd(intparam1,intparam2){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>privatestringAdd(stringparam1,stringparam2){return(param1+param2);}}}

Output

When both the inputs are integers,-

Enter first Value12Enter second Value23The summation is = 35

When both the inputs are strings, -

Enter first ValueWikiEnter second ValueversityThe concatenation is : Wikiversity

When data is not correct,-

Enter first Value12Enter second ValueWikipediaType 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 ofcompile-time polymorphism.

Run-time Polymorphism (Method Overriding)

[edit |edit source]

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 calledFunction 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 declaredsealed to stop it from any further overriding.

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

Let's consider the following example, -

usingSystem;namespaceFunctionApp{abstractclassProgramBase{// Calculates the area of a squarepublicvirtualdoubleAreaCalc(doublelength){return(length*length);}// Signature of an abstruct methodinternalabstractstringPrintText(stringtext);// For 'sealed' use testpublicvirtualstringWriter(stringtext){returntext;}}classProgramChild:ProgramBase{// Calculates area of a circlepublicoverridedoubleAreaCalc(doubleradius){doublepi=3.142;//Uses the function from the base class to calculate the squarereturnbase.AreaCalc(radius)*pi;}// Implementation of the abstruct method.internaloverridestringPrintText(stringtext){returntext;}// Overriding of the Writer method// and making that sealedpublicoverridesealedstringWriter(stringtext){return"child class "+text;}staticvoidMain(){//Instantiating selfProgramChild_prog=newProgramChild();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.2Abstruct to Override Example.child class Example of Sealed!

Notice that here we have three classes (One class is commented.). the first classProgramBase is the base class and like all base classes, it isabstruct. It has three methods of which one isabstruct, meaning has only signature.ProgramChild is the child class to theprogramBase. It is overriding all the methods of the base class and making one of them assealed. 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, inAreaCalc method inProgramChild we are calling the same method from the base class with thebase prefix. One more thing to notice here is that apublic method in the base class is a public method in the child class and same forinternal. Try to change it and you will get a compile time error.Now to the check thesealed method, just uncomment theProgramGrandChild 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 |edit source]

Function Hiding orFunction 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 toHiding. This can be achieved usingnew keyword. Here is how, -

usingSystem;namespaceFunctionApp{classProgramBase{// Calculates the area of a squarepublicdoubleAreaCalc(doublelength){return(length*length);}// Overridable functionspublicvirtualstringWriter(){return"base class";}}classProgramChild:ProgramBase{// Calculates area of a circle by hiding the// function with same sigmature from basepublicnewdoubleAreaCalc(doubleradius){doublepi=3.142;//Uses the function from the base class to calculate the squarereturnbase.AreaCalc(radius)*pi;}// Overriding of the Writer method// and making that sealedpublicoverridestringWriter(){return"child class";}staticvoidMain(){//Instantiating ChildProgramChild_prog=newProgramChild();Console.WriteLine(_prog.AreaCalc(10));Console.WriteLine(_prog.Writer());// Casting Child to Base classProgramBase_prog1=_prog;Console.WriteLine(_prog1.AreaCalc(10));Console.WriteLine(_prog1.Writer());//Instantiating Base classProgramBase_prog2=newProgramBase();Console.WriteLine(_prog2.AreaCalc(10));Console.WriteLine(_prog2.Writer());Console.ReadLine();}}}

Output

314.2child class100child class100base class

Here we have two functionsAreaCalc andWriter. The first one gets hidden in the child class, whereas, the second function gets overridden in the child. In theMain 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 bothoverriding andhiding 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 thenew 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 |edit source]

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. Theget orset is optional.get returns the value andset stores the value.The beauti of it is its simplicity to use.

Further Studies

[edit |edit source]

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 |edit source]
  • Create a program forTower 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 |edit source]
  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 |edit source]
Topics inC#
BeginnersIntermediateAdvanced
Part of theSchool of Computer Science
Retrieved from "https://en.wikiversity.org/w/index.php?title=C_Sharp/Functions&oldid=2306065"
Category:

[8]ページ先頭

©2009-2026 Movatter.jp