Introduction To Function

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Introduction to Function

In C#, a function is a way of packaging code that does something and


then returns the value. Unlike in C, C++ and some other languages,
functions do not exist by themselves. They are part of an object-oriented
approach to programming.
A program to manage spreadsheets might include a sum() function as part
of an object, for example.
In C#, a function can be called a member function — it is a member of a
class — but that terminology is left over from C++. The usual name for
it is a method.
The return type is the data type of the value the method returns. If the
method is not returning any values, then the return type is void. Method
name − Method name is a unique identifier and it is case sensitive. It
cannot be same as any other identifier declared in the class.
Parameters are used to pass values or variable references to methods.
The parameters of a method get their actual values from the arguments
that are specified when the method is invoked.
A function call is made somewhere in the program. A function call causes
the execution of the statements in the body of the function.

The static method


In C# is a method that keeps only one copy of the method at the Type
level, not the object level. That means, all instances of the class share
the same copy of the method and its data. The last updated value of the
method is shared among all objects of that Type

The Instance Method


There are two types of methods: instance method and static method. This
introduction covers the instance method.
The example below defines a simple class and calls it Test. This example
is a simple console program, so this is allowed. Usually, the first class
defined in the C# file must be the form class.
It's possible to have an empty class like this class Test { }, but it
isn't useful. Although it looks empty, it—like all C# classes—inherits
from the Object that contains it and includes a default constructor in
the main program.

var t = new Test();


This code works, but it won't do anything when run except create an
instance t of the empty test class. The code below adds a function, a
method that outputs the word "Hello."
using System;
namespace funcex1
{
class Test
{
public void SayHello()
{
Console.WriteLine("Hello") ;
}
}
class Program
{
static void Main(string[] args)
{
var t = new Test() ;
t.SayHello() ;
Console.ReadKey() ;
}
}
}
The function SayHello is about as simple a function as you can have.
It's a public function, which means the function is visible from outside
the class.
If you remove the word public and try to compile the code, it fails with
a compilation error "funcex1.test.SayHello()' is inaccessible due to its
protection level." If you add the word "private" where the word public
was and recompile, you get the same compile error. Just change it back
to "public."
The word void in the function means that the function does not return
any values.

Typical Function Definition Characteristics


Access level: public, private plus some others
Return value: void or any type such as int
Method Name: SayHello
Any method parameters: none for now. These are defined in the brackets
() after the method name

The code for the definition of another function, MyAge(), is:

public int MyAge()


{
return 53;
}

Add that right after the SayHello() method in the first example and add
these two lines before Console.ReadKey().

var age = t.MyAge();


Console.WriteLine($"David is {0} years old",age);
Running the program now outputs this:
-------------------------------------------------------
Hello
David is 53 years old,

The var age = t.MyAge(); call to the method returned the value 53.

You might also like