What is Method, Property and Function?

Hans picture Hans · Nov 22, 2010 · Viewed 43.6k times · Source

Yeah, I'm struggling with that. I cannot distinguish among them because every explanation I read is so unclear and philosophical enough. Can someone clear up these definitions for me ? Thanks guys.

These definitions apply as much to procedural-programming as oop ? Thanks.

Answer

Jeff Sternal picture Jeff Sternal · Nov 22, 2010

Over time, the way people use each of these terms has changed (and will likely keep changing), but here's what they probably mean if you're reading articles written in the last decade or so:

Functions (aka subroutines) are relatively self-contained, relatively independent pieces of code that make up a larger program.

Methods are functions attached to specific classes (or instances) in object-oriented programming.

Properties are an object-oriented idiom. The term describes a one or two functions (depending on the desired program behavior) - a 'getter' that retrieves a value and a 'setter' that sets a value. By convention, properties usually don't have many side-effects. (And the side-effects they do have usually have limited scope: they may validate the item being set, notify listeners of a change, or convert an object's private data to or from a publicly-declared type.)