What Are The Specific Meanings Of The Terms: Functions, Methods, Procedures, and Subroutines?
What’s the difference between a function and a method?
In the linked question:
These are often used interchangeably, but in some languages there’s a distinction. In Pascal, a function is a subprogram that returns a value, while a procedure is a subprogram that doesn’t. In C and related languages, every subprogram has a return type (even if it’s
void), so there’s no distinction.
method, member function
These are two names for the same thing — essentially a function that’s associated with a given class or object.
Speaking strictly, a
procedureis a subroutine that is executed purely for its side effects (like printing something to the screen) and returns no values. A
functionis a subroutine that always returns the same value given the same inputs and has no side effects. A
methodis a procedure or function that is associated with a class or object.
That being said, is it possible for a class method have the same name as an existing function? For example in Java, if in the Main class, I have a function called foo(), and I create a class called Animal, can I create a method called foo() in the Animal class?
For example in Java, if in the Main class, I have a function called foo(), and I create a class called Animal, can I create a method called foo() in the Animal class?
Java doesn’t have functions. Therefore, the question of whether or not you can have a method and a function with the same name is non-sensical.
The answer is neither Yes nor No. The question itself is ill-formed.
Speaking more generally, if the language has both functions and methods and if the language allows methods and functions to have the same name, then methods and functions can have the same name. If the language doesn’t allow methods and functions to have the same name, then methods and functions can’t have the same name.