Home » C# Basics » 05 - Object Oriented Programming in C#
5

What are Methods?

Defines what methods are and how it is used in OOP

Methods, also known as functions, define the actions that an object of a class can perform. They are defined inside the class. The syntax for defining a method is:

[access-modifier] [return-type] method-name ([arguments])
{
statements of the method;
}

This syntax looks complicated but we can understand it with an example:

public int CalculateSquare(int number)
{
int s;
s = number*number;
return s;
}

Here, we have defined a method named CalculateSquare. The method takes an argument called number that is of type int. Inside the method, we have declared an integer variable square. Next, the square of the number is calculated by multiplying the number to itself. The result is stored in the variable s. The last statement inside the function is returning s.

This example raises several questions. Why do we need methods? Where should we write the methods in our program and what is meant by arguments and return type? Let us understand these concepts in detail.

Methods are used to perform a processing task on values. As we have learned, we use the properties to read and write values in the fields. However, there can be several other processing tasks required apart from reading and writing values. For example, consider a class that has a date value as its field. Using properties, we can store a date into or read date from this field. Let us assume that our program uses this date for storing a user's date of birth and calculate the age based on this date. The code for age calculation should be inside a method.

Methods are used to break up a large code into several smaller units. Each function should perform a specific task.

A method is defined inside the class definition. The general syntax is -

 access-modifier class classname
{
declare fields;
define properties
define methods
}

Here is an example -

class Calculate
{
//declare fields
//define properties
//now the methods
public int CalculateSquare(int number)
{
int s;
s = number*number;
return s;
}
}

Now that the method is defined in the class, we can use this method by calling it. Calling a method is very much similar to accessing properties. First, we need to create an object of the class in our program:

Calculate c = new Calculate();

We have declared an object `c' of the class `Calculate'.

To call the method, we use the dot (.) operator -

c.CalculateSquare(4);

As shown above, to call a method of the class, we use the syntax -

Object-name.method-name(arguments);

Here, argument is the value on which the method will work. In our example, we have specified the value 4 as argument. This argument will be sent to the method definition inside the class where it will be stored in the variable `number'. Examine the first line of the method:

public int CalculateSquare(int number)
{
int s;
s = number*number;
return s;
}

The variable `number' is declared as type int. Hence, it can store the argument `4' because 4 is an integer value. However, if we call the method with some other type of argument, our program will generate an error.

Inside the method, the square of the variable `number' is calculated and stored in another integer variable called `s' i.e. variable `s' stores the value 16 in our example. Now notice the last statement inside the method:

public int CalculateSquare(int number)
{
int s;
s = number*number;
return s;
}

`return' is a keyword. It is used to send a value back to the statement from where the method was called. In our example, the value being returned is an integer because variable `s' is of integer type. That is why, the return type of the method is int (notice the first line of the method):

public int CalculateSquare(int number)

Where is this return value going? It is going back to the method call statement. We can use this return value in our application. Consider the following:

int square; 
Calculate c = new Calculate();
square= c.CalculateSquare(4);

We have called the CalculateSquare method and passed it the argument 4. The method will calculate the square of 4 and return the result. That result will be assigned to a variable named `square'.

Note that sometimes, the method may not return any value, may not take any arguments or may take multiple arguments. If the method does not return any value, its return type is `void'.

The following examples shows how to define different types of methods:

public void SomeMethod()
{
statements of the method;
}

The above method does not take any arguments and does not return any value. The keyword `void' indicates that the method does not return any value and the empty parentheses indicate that the method does not need any arguments.

Here is another example-

public int FindBigger(int a, int b)
{
if (a>b)
return a;
else
return b;
}

The above method takes two integer arguments, finds the bigger number and returns it. Note that though there are two return statements, only one value will be returned, depending on which one is the greater. A method can return only one value.