views:

122

answers:

5

I'm a java programmer and am trying to understand the difference between a method (java methods) and a function (such as in c++). I used to think that they are the same, just different naming conventions for different programming languages. But now that I know they are not, I am having trouble understanding the difference.

I know that a method relates to an instance of a class and has access to class data (member variables), while a function does not (?). So is a function kind of like a static method?

See here for explanations I read which led me to think this.

+4  A: 

A function is simply a generic name for a portion of code within a program. The word "method" is a synonym for function. So are "subroutines" and "procedures," etc.

Java and C++ functions are for the most part exactly the same thing.

The word "method" tends to be used for subroutines associated with an instance, while "function" tends to be used for those that are global/static.

But even then, "methods" are generated by the compiler as though they were "functions."

Consider this C++ code:

class Foo
{
public:
    void DoFoo(int param)
    {
        printf("%d, %d\n", param, member);
    }
private:
    int member;
};

int main()
{
    Foo f;
    f.DoFoo(42);
    return 0;
}

The compiler generates code to something equivalent to this:

struct Foo
{
    int member;
};

void Foo_DoFoo(Foo* this, int param)
{
    printf("%d, %d\n", param, this->member);
}

int main()
{
    Foo f;
    Foo_DoFoo(&f, 42);
    return 0;
}

So the distinction between "method" and "function" is merely a convention.

In silico
+1  A: 

In C all "functions" are "top level" in the sense that they were not associated with a type. If they were in your scope (e.g., via an include), you could refer to them and they could be linked.

In C++ you can create classes and place methods in them. Methods marked as static are invoked via a particular class but are not associated with an instance of the class. In that sense they are like functions. However, they are allowed some privileges associated with the class (e.g., they can be made private and can access private static members). However, you can still use C-style functions, for example for library functions.

In Java every method is associated with a class, so there are static methods but no C-style functions.

Uri
This makes sense - so the bottom line is that methods have an association with a class (either static or not), while function are not associated with a class (and can live outside of a class too). Correct?
lkm
+2  A: 

This is strictly a vocabulary difference. Some consider a method an operation that belongs to an object or a class, and a function an operation that doesn't. Others, like the C++ crowd, call them both functions but refer to free functions or non-member functions when the function doesn't belong to a class or an object. I personally use the two interchangeably.

All in all, in the C++ jargon when you wish to refer to specific kinds of functions (non-members, members, returning no value, ...) you add an adjective to the noun function: a friend function, a void function, a member function and so on.

wilhelmtell
A: 

Lets put it really simple a function is the same as a Java static method, however a function does not require a class to exist, which turns it as Uri says in a top level piece of code. One of the advantages of this is that it won't require an object instantiation to be called.

Coredumped
A: 

Is a function kind of like a static method?

Kind of. But I'd rather say that a static method is like a function that has been enslaved and shackled to an object. For an elaboration of this point of view see Execution in the Kingdom of Nouns.

Norman Ramsey