Method (computer programming): Difference between revisions

Content deleted Content added
Danakil (talk | contribs)
No edit summary
Danakil (talk | contribs)
No edit summary
Line 1:
Used mainly in [[object-oriented programming]], the term '''method''' refers to a [[subprogram (programming)|piece of code]] that is exclusively associated either with a [[class (computer science)|class]] (called '''class methods''' or '''static methods''') or with an [[object (computer science)|object]] (called '''instance methods'''). Like a procedure in [[procedural programming languages]], a method usually consists of a sequence of [[statement (programming)|statements]] to perform an action, a set of input [[parameter (programming)|parameters]] to parameterize those actions, and possibly an output value (called '''return value''') of some kind. The purpose of methods is to provide a mechanism for accessing (for both reading and writing) the private data stored in an object or a class.
 
A method should preserve the [[invariant|class invariants]]s associated withof the classobject it is associated with, and should be able toalways assume that they are valid onwhen entryit to thecommences methodexecution. To this effect, [[precondition]]s are used to constrain the method's parameters, and [[postcondition]]s to constrain method's output, if it has one. If any one of either the preconditions or postconditions is not met, a method may raise an [[exception]]. If the object's state does not satisfy its class invariants on entry to or exit from any method, the program is considered to have a [[Software bug|bug]].
 
==Scope==
Whereas a [[C programming language|C]] [[programmer]] might push a value onto a [[stack_(software)|Stack]] data-structure by calling:
stackPush(&myStack, value);