Functions / Methods in Java and C#

When we hear a term “Function” our mind create the image of an option or technology in a program that do something, so when we use the function, a program does a particular job. A function is simply a combination of several tasks written beautifully within a single label through a proper organization rules. In programming languages, a function is used to hold several lines of code that does something, such as, adding the two integers together, displaying a text on a console screen, etc. Through the use of function, you can cut the multiple lines of codes and paste into a function with a name (i.e., a name through which you will remember it) and then call (use) it whenever you need the functionality in your program without duplicating the same code throughout the program, thus FUNCTIONS REDUCES THE SIZE OF THE CODE.

In OOP languages (i.e., C# or Java), a function is implemented as a static method, which means that you need to declare a class first in order to use it, unlike in non-OOP programming languages (like C) where a function exists by itself. In C# and Java, a function is a part of an object and is defined in a class and every executed instruction is performed in the context of a method. So, for example, when we call “System.out.prinln()” (in Java) or “Console.WriteLine()” (in C#), we are actually calling a function which simply creates a line to the console, thus a function is named as “method” in OOP.

In this tutorial, I will teach you how you can implement functions in C# and Java to make your programming life more joyful!

Static Methods

As I said earlier, In OOP languages, we define functions as static methods. They are those methods which doesn’t change the state of a class (thus they are named ‘static’). It make sense that functions are implemented as static methods, because in functions we are interested in executing some arbitrary and we are not interested in changing the object’s state.

The syntax for defining a static method in a class is same for both C# and Java. It is as follows:

Considering this syntax, we can very easily define our function. Let’s define a function for adding two numbers, we can do so as follows:

You can see that it’s trivial to define a function, but then the task that our function does is trivial as well. Let’s define another function for calculating factorial:

We can use our functions in a program as follows:

C#

Java

Static Classes

Static classes are those classes whose only one instance is available to a program. These classes don’t need to be instantiated. It is useful feature in those cases where you know that an instance of a class that doesn’t need to be passed to other classes, but can be easily used without instantiating. A static class is a class which can only contain static members, and therefore cannot be instantiated. One of its examples is “GameSettings” for storing different settings in our game. We can define such class as follows:

The following is the syntax for using static method in a class:

Now we can use our static class in a program as follows:

C#

Java

Note that you can not use non-static members in static methods.

Conclusion

As you can see, Static Class can be used as a convenient container for a sets of methods that only run on input parameters and doesn’t modify the state of an object. In the .NET Framework Class Library, the static System.Math class contains methods that perform mathematical operations like ‘cos’, ‘sin’ etc., without any requirement to store or retrieve data that is unique to a particular instance of the Math class, thus they don’t need instantiation to operate.

Functions are best suited for trivial tasks, especially when some task is duplicated several times in your program; hence you can simply create a static method in a class and call it whenever you need it. In which cases are you going to use functions?

Leave a Reply

Your email address will not be published. Required fields are marked *