3.1.4.7 Lists - collections of data | Functions and methods
Functions vs. methods
A method is a specific kind of function - it behaves like a function and looks like a function, but differs in the way in which it acts, and in its invocation style.
A function doesn't belong to any data - it gets data, it may create new data and it (generally) produces a result.
A method does all these things, but is also able to change the state of a selected entity.
A method is owned by the data it works for, while a function is owned by the whole code.
This also means that invoking a method requires some specification of the data from which the method is invoked.
It may sound puzzling here, but we'll deal with it in depth when we delve into object-oriented programming.
In general, a typical function invocation may look like this:
result = function(arg)
The function takes an argument, does something, and returns a result.
A typical method invocation usually looks like this:
result = data.method(arg)
Note: the name of the method is preceded by the name of the data which owns the method. Next, you add a dot, followed by the method name, and a pair of parenthesis enclosing the arguments.
The method will behave like a function, but can do something more - it can change the internal state of the data from which it has been invoked.
You may ask: why are we talking about methods, not about lists?
This is an essential issue right now, as we're going to show you how to add new elements to an existing list. This can be done with methods owned by all the lists, not by functions.
Comments
Post a Comment