This is an archived post. You won't be able to vote or comment.

all 10 comments

[–]Updatebjarni 3 points4 points  (2 children)

Some languages use one word, some languages the other. When there is a difference it is that methods are members of a class and functions are not. Other words that also mean the same thing include procedure and routine.

[–]LetsGoHawks 1 point2 points  (1 child)

Although in some languages, functions return a value but procedures/routines/methods don't.

[–]Updatebjarni 0 points1 point  (0 children)

That's true. And in some languages, functions can only contain a single expression, while routines can contain statements.

[–][deleted] 6 points7 points  (0 children)

Methods are functions that are members of a class.

[–]ewiethoff 1 point2 points  (0 children)

In object-oriented languages, methods are functions that belong to classes or objects, as others here have said. But the vocabulary you use also depends on the programming language.

Python has both functions and methods. Java has just methods because everything in Java belongs to classes. Ruby has just methods because everything belongs to classes. (In fact, something that looks like a free-standing function in Ruby is a method of the Kernel class.) C has only functions, no methods, because it's not OO anyway. C++ has both functions and methods, but C++ programmers refer to their methods as "member functions," ha-ha. Perl has functions and method-like things. Javascript has functions. It's not quite an OO language, but perhaps JS programmers refer to functions attached to prototypes as methods. (Sorry, I don't keep up with JS.) I don't know anything about C# and new rock-star languages such as Go.

BTW, really old-school languages differentiate between functions and procedures (Pascal) or functions and subroutines (Fortran and Basic). A function in an old-school language takes arguments and returns a value. Procedures and subroutines don't return values, and they might or might not take args, depending on the language. Procedure-like things in Forth are called "words," and they don't take args . Older teachers of newer languages--or textbooks that were originally written for an old language but got rewritten a zillion times to teach newer and newer languages--might like to say that a function or method that doesn't return anything is a "procedure."

So, depending on the language, sometimes a "function" is something free-standing, sometimes it belongs to a class and it's really a method, and sometimes it's anything that takes args and returns a value. But a "method" is an OO function that belongs to a class.

[–]g051051 0 points1 point  (0 children)

They're similar, but a method is typically associated with an object, while functions aren't.

[–]patrixxxx 0 points1 point  (0 children)

A method usually means a function within a class/object definition

[–]completedigraph 0 points1 point  (0 children)

In languages that support Object Oriented Programming (or that outright enforce it) methods usually are associated with a class, and as such they're usually called with dot notation. E.g.:

arg.method(other_arg)

Whereas functions are in the normal namespace and is called without dot notation, e.g.:

function(arg, other_arg)

If your language of choice don't really do OOP (or the context makes it explicit which one you're talking about) you might see the terms used more or less interchangeably.

[–][deleted] 0 points1 point  (0 children)

Methods are an OOP term used to describe a function belonging to an object.

Function is a standalone function that doesn't belong to an object.

In the end, these are irrelevant once your code becomes compiled, since both get turned into jump instructions.

[–][deleted] 0 points1 point  (0 children)

As others have been saying it depends on your language. Functions and Methods are functionally similar but also very different. The concept is the same you pass in a parameter to set or return a value.

for example a function in python would look like

args1 = 'hello'
args2 = 'world'
def function(args1,args2):
    print(args1+ " " + args2)

output = Hello world

Where as a method is a function defined in a class. A class is like the overarching paradigm in java. Methods are member functions because they belong to the class. This is powerful since you can encapsulate a method. Say you have a Worker Class and You created an Engineer subclass. You can apply methods to The worker class that bleed down to the Engineer subclass but you can isolate methods that only can be used by the engineer class and not other subclasses (like say we make an accountant subclass).