Function vs Method

A function is a piece of code that is called by name. In this case, name of the function. It can be passed data to operate on (ie. the parameters) and can optionally return data (the return value).

All data that is passed to a function is explicitly passed, because there is no ‘self’ or ‘this’, or any identification of an instantiation of a class (object) to operate from.

In this case, if we are to pass any data into myFunction, we can only do so through parameter variable int i. There is no other way around it.

A method is a piece of code that is called by name that is associated with an object. This object is instantiated of a class.

In most respects, a method is identical to a function except for two key differences.

1) It is implicitly passed for the object for which it was called.
2) It is able to operate on data that is contained within the class (remembering that an object is an instance of a class – the class is the definition, the object is an instance of that data).

In the example below, length method is used to get the length of a string. But what string? The string of the object instantiation data1. The ‘implied’ use of this object member variable myName is called implicit parameter.

definition: