In C++, Differencebetween a Method and a Function

What's the difference between a method and a function?

A function is a piece of code that is called by name. It can be passed data to operate on (i.e. the parameters) and can optionally return data (the return value). All data that is passed to a function is explicitly passed.

A method is a piece of code that is called by a name that is associated with an object. In most respects it is identical to a function except for two key differences:

  1. A method is implicitly passed the object on which it was called.
  2. A method 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).

(this is a simplified explanation, ignoring issues of scope etc.)

In C++, what is the difference between a method and a function

As far as the C++ standard is concerned, there is no such thing as a "method". This terminology is used in other OO languages (e.g. Java) to refer to member functions of a class.

In common usage, you'll find that most people will use "method" and "function" more or less interchangeably, although some people will restrict use of "method" to member functions (as opposed to "free functions" which aren't members of a class).

Difference between Method and Function?

Both are same, there is no difference its just a different term for the same thing in C#.

Method:

In object-oriented programming, a method is a subroutine (or procedure
or function) associated with a class.

With respect to Object Oriented programming the term "Method" is used, not functions.

objective c difference between functions and methods

First, I'm a beginner in Objective-C, but I can say what I know.

Functions are code blocks that are unrelated to an object / class, just inherited from c, and you call them in the way:

// declaration
int fooFunction() {
return 0;
}

// call
int a;
a = fooFunction();

While methods are attached to class / instance (object) and you have to tell the class / object to perform them:

// declaration
- (int)fooMethod {
return 0;
}

// call
int a;
a = [someObjectOfThisClass fooMethod];

Difference between methods and functions, in Python compared to C++

Needs Attention: This answer seems to be outdated. Check this

A function is a callable object in Python, i.e. can be called using the call operator (though other objects can emulate a function by implementing __call__). For example:

>>> def a(): pass
>>> a
<function a at 0x107063aa0>
>>> type(a)
<type 'function'>

A method is a special class of function, one that can be bound or unbound.

>>> class A:
... def a(self): pass
>>> A.a
<unbound method A.a>
>>> type(A.a)
<type 'instancemethod'>

>>> A().a
<bound method A.a of <__main__.A instance at 0x107070d88>>
>>> type(A().a)
<type 'instancemethod'>

Of course, an unbound method cannot be called (at least not directly without passing an instance as an argument):

>>> A.a()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unbound method a() must be called with A instance as first argument (got nothing instead)

In Python, in most cases, you won't notice the difference between a bound method, a function, or a callable object (i.e. an object that implements __call__), or a class constructor. They all look the same, they just have different naming conventions. Under the hood, the objects may look vastly different though.

This means that a bound method can be used as a function, this is one of the many small things that makes Python so powerful

>>> b = A().a
>>> b()

It also means that even though there is a fundamental difference between len(...) and str(...) (the latter is a type constructor), you won't notice the difference until you dig a little deeper:

>>> len
<built-in function len>
>>> str
<type 'str'>

What's the difference between a method and a function?

A function is a piece of code that is called by name. It can be passed data to operate on (i.e. the parameters) and can optionally return data (the return value). All data that is passed to a function is explicitly passed.

A method is a piece of code that is called by a name that is associated with an object. In most respects it is identical to a function except for two key differences:

  1. A method is implicitly passed the object on which it was called.
  2. A method 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).

(this is a simplified explanation, ignoring issues of scope etc.)

Whats the Difference between a . and -> in calling a Method in C++

The arrow operator is meant for calling a method from a pointer to an instance of an object.

The dot operator is meant for calling a method from a reference to an instance of an object, or on a locally defined object.

Your code would not compile if you reversed the operators on the two examples.

Java's methods vs. functions

In my opinion this figure http://www.jot.fm/issues/issue_2008_03/article4/images/figure2.gif

one, two and three dimensional method dispatch

from http://www.jot.fm/issues/issue_2008_03/article4/
helps understanding one of the main differences between OO and procedural programming.
Basically the idea is that

Procedural programming provides only one dimension to associate a
computational unit with a name. Here, procedure calls or names
are directly mapped to procedure implementations. In Figure a calling
m1 leaves no choice but the invocation of the only implementation of
procedure m1

while

Object-oriented programming adds another dimension for name resolution
to that of procedural programming . In addition to the method or
procedure name, message dispatch takes the message receiver into
consideration when looking up a method. In Figure 2b we see two
implementations of method m1. The selection of the appropriate method
not only depends on the the message name m1, but also the receiver of
the actual message, here Ry

the third section of the figure (c) refers to subject oriented programming, in which the behavior of an object (the called method) does not only depend on the object status but, also, on the subjects which is invoking (or observing) it. However this is actually out of the scope of your question.



Related Topics



Leave a reply



Submit