consider a standard c# 'function'
public void foo() { //some code }
In c or c++ this is called a 'function' - even if taking no parameters and returning no value. In another language maybe it would be a 'procedure'. In object orientation speak it would be called a 'method' if a class member. What would be the correct term to use in c#?
A method, like a function, is a set of instructions that perform a task. The difference is that a method is associated with an object, while a function is not.
Function and method are two commonly confused words. While every method is a function, not every function is a method. There is also another word that is erroneously used interchangeably: a procedure.
A procedure is a function that doesn't return a value. In particular, this means that a procedure can only cause side effects. (That might include mutating an input parameter!) A method is a function that closes over a set of variables, that is, a closure.
Java chose to call them "methods" because they fit the already-existing meaning of that word. Had they called them "functions" they would be introduced confusion because that word already has a different meaning.
Make your choice =)
Edit : Just to be more precise, Method, function and procedure are OOP words to describe a subroutine. Some languages have their own vocabulary such as "predicate" in Prolog, or "constructor" in C++/C#/Java, or even "property" in C#.
Just to confuse the issue futher: (from C# Language Specification)
7.4 Function members
Function members are members that contain executable statements. Function members are always members of types and cannot be members of namespaces. C# defines the following categories of function members:
* Methods
* Properties
* Events
* Indexers
* User-defined operators
* Instance constructors
* Static constructors
* Destructors
And
10. Classes
A class is a data structure that may contain data members (constants and fields), function members (methods, properties, events, indexers, operators, instance constructors, destructors and static constructors), and nested types. Class types support inheritance, a mechanism whereby a derived class can extend and specialize a base class.
So "function member" or "method" would be correct for C#.
Method is OOP abstraction term. They describe behaviour(a verb) of an object. They are equivalent to some of the procedural programming's functions and procedures. (the properties etc are also function and procedures).
So, function is a program within program that returns some values. Procedure is a program within program that does something. Methods, Properties etc, etc are a next level of abstraction(used in OOP). They are wrapped around functions and procedures.
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