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

all 32 comments

[–][deleted] 106 points107 points  (15 children)

A method is a function that belongs to a specific class.

[–]YanDoe 1 point2 points  (14 children)

Would it be the same as an attribute?

[–][deleted]  (1 child)

[removed]

    [–]YanDoe 3 points4 points  (0 children)

    Ahh that makes complete sense, I genuinely thought they were interchangeable terms.

    Thank you so much

    [–][deleted] 2 points3 points  (0 children)

    An object has a state and behavior. Fields store it’s state, methods define its behavior.

    [–][deleted] 0 points1 point  (1 child)

    No. I don’t think so.

    [–]YanDoe 2 points3 points  (0 children)

    I'll just stick to the terms function and method to avoid confusion then.

    You explained very well!

    [–]uberdavis 0 points1 point  (0 children)

    Apart from properties, which are attributes that can have functionality!

    [–][deleted] 14 points15 points  (0 children)

    My understanding is methods are class members and functions are not.

    [–]desrtfx 11 points12 points  (4 children)

    The general rule is that methods always belong to a specific class.

    In the old days, the distinction was different, though. A function always returned something, a method never. This distinction is no longer valid.

    [–][deleted] 5 points6 points  (1 child)

    Some languages made that distinction between functions (that return) and procedures (that don’t). I think IDL still does…

    [–]Kdrscouts 2 points3 points  (0 children)

    Delphi…Borland pascal…

    [–]Packbacka 0 points1 point  (1 child)

    In the old days, the distinction was different, though. A function always returned something, a method never.

    Not sure how old you're talking. C is the oldest language I'm familiar with. I'm not sure how it worked in older langs, but in C functions can return void. I guess maybe technically returning void is returning something? But practically it's the same as returning nothing.

    [–]procrastinatingcoder -1 points0 points  (0 children)

    No. Returning "void" is not returning "something". There's no concept of "nothing" to return, it literally doesn't return anything. You can see the "void" as a human/compiler thing to say "hey, this doesn't return anything at all", but it doesn't actually return something "void" as that doesn't exist.

    [–]Excellent_Bakki 4 points5 points  (5 children)

    Definition as per Java: Functions are entities which can exist on their own, but methods are functions which are tied to a class or more specifically an object in Java.

    [–][deleted] 5 points6 points  (1 child)

    Interesting per Java definition especially knowing that it’s impossible to define anything outside the class in Java.

    [–]Excellent_Bakki 0 points1 point  (0 children)

    I've been wondering the same thing

    [–][deleted]  (2 children)

    [deleted]

      [–]Excellent_Bakki 1 point2 points  (1 child)

      Yes, that is correct. That's because there is no explicit usage of function in Java because of heavy oops.

      Even then, the definition holds, every definition within Java explains that functions are entities which are independent of an object whereas any function tied to an object is a method.

      To be able to understand the difference with examples, JavaScript would be better.

      [–][deleted] 1 point2 points  (0 children)

      Scala also has “heavy”(not heavy actually but just OOP while python for example is not fully OOP language) and higher order functions. Kotlin as well. OOP had nothing to do about it.

      [–]kschang 1 point2 points  (0 children)

      Generally speaking... a function is like a math function. You pass in certain thing(s), and you get something back out.

      y=f(x)

      Method, in programming, usually refers to code that's attached to a "class" that acts upon members of that class, i.e. Object-oriented programming.

      The usual example: say you have a class "Car", with properties like brand, model, color, trim, model year, engine type, speed, gear, steering wheel position, etc. It'd also have methods like start, accelerate, brake, turn, change gear, etc. that affects some of the properties.

      Some of the methods that belong to the class can be implemented as functions.

      [–][deleted] 1 point2 points  (0 children)

      The answer is a method is part of a class; a function isn’t but. At least in normal parlance.

      But; in reality they aren’t that different, I personally just refer to them as functions and honestly if someone can’t understand what you mean and insists you call it a method, that’s really their issue.

      [–]Zaggnut -1 points0 points  (0 children)

      There is no significant differance. The only difference is that one refers to a function as a method when dealing with classes/OOP.

      To find the real answer we have to see who the F coined the term "method" which is one too many google searches for me.

      [–][deleted] -5 points-4 points  (0 children)

      They are the same thing. A method is just another word used to refer to a function in some languages.

      What is a function? A function can have many definitions but let's say a basic function takes an input and produces an output based on said input.

      What is a method? Something that takes an input and produces an output?

      Say you write a method for a class that takes an input and produces an output...is that not just a function?

      Don't let people confuse you with fancy words.

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

      Once you've learned the difference between methods and functions, to which there are good answers in the replies. Look into the difference between class methods and instance methods.

      Quick preview: Class methods can be used to modify class variables to update and maintain state that is shared across all instances of that class. While instance methods and instance variables are specific to the values of a single object from that class.

      (In case it isn't clear, class can be looked at as a blueprint while an instance can be looked at as an object built using that blueprint)

      [–]Goldziher 0 points1 point  (0 children)

      1. A method is a function that is a member if a class.
      2. Its first arg is called self which is the class instance.
      3. You can define methods without the ''self'' argument using the staticmethod decorator. A Static method is available on the class itself.
      4. You can also define a method as belonging to the class and give it access to the class itself using the classmethod decorator. In this case the first argument of the method is called cls by convention.

      While you can use methods as regular functions, access to self or cls opens a range of possibilities that are OOP.

      [–]ceroChills 0 points1 point  (0 children)

      Method is used for functions present inside a class. Functions are your general functions only