Tuesday 11 July 2017

Java Tutorial : Functions and Function Overloading

This is our Java tutorial series in which you can learn core java with easy tutorials. In this post i am gonna show you what is function and how to declare them. Function is nothing but the collection of java statements which are responsible for doing particular task again and again. The purpose of creating functions is to reduce the code and reuse the code whenever we need. Well the another name of function is method. Lets understand this with an example : You have a situation where you have to create a program which accepts two number and adds them and return the result. One way is to write the code like this: int a = 3, b=4; int result=a+b; this is beginners method where you write these two line wherever you need the sum of this two number. Another method is using functions like below: public int sum(int a, int b){ int result = a+b; return result } you just create this function and call it whenever you need two number's sum. It doesn't matter how many times you call it. you just have to write one line to call it like this : int res = sum(3,4); it will give you the sum of 3 and 4 in res variable. To understand more clearly watch below video. Its in depth tutorial about function and method overloading .

No comments:

Post a Comment