I just decided to integrate my MATLAB programming skills with some more consistent and rigorous Java coding. Therefore I hope it's not gonna be a too naive question.
I'd like knowing if there is any real reason why Java refers to functions as methods
and not as functions
, as many other program languages do.
Is it because of the inner OOP
Java's nature compared with procedural languages such as C/C++
? or are there any other important (or subtle) reasons?
Thanks in advance.
Method and a function are the same, with different terms. A method is a procedure or function in object-oriented programming. A function is a group of reusable code which can be called anywhere in your program. This eliminates the need for writing the same code again and again.
Functions can be called only by its name, as it is defined independently. But methods can't be called by its name only, we need to invoke the class by a reference of that class in which it is defined, i.e. method is defined within a class and hence they are dependent on that class.
Functions are the properties of structured languages. Methods are the properties of Object-oriented language. It is a self-describing piece of code. It is used to manipulate the instance variable of a class.
In languages such as C++, functions are bits of code that will perform a particular action - but are not associated with an object. functions that are to do with an object are called methods. in java all functions are methods as they are all to do with objects.
Well there is a little difference between a method and a function.
A function is just a code that you can call anytime by its name and you can pass arguments also known as parameters to it and you can also get the result from any function i.e. return value of the function.
But a method is a code that is called by its name but it is associated to any object. You can pass parameters to methods also and you can also get some return value from methods but thing is they will always be associated with some objects.
EDITED
Java is object oriented, you cannot have Java code to run without classes in most cases however in C++ you can get your code run without classes. So in Java there will be classes and code will be written in classes so they are called methods instead of functions, as they will be associated with objects.
But in C++ you can have some function that can be called by passing values explicitly.
In simple terms you can say, a method is a function that is related to an object.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With