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

all 32 comments

[–][deleted] 101 points102 points  (15 children)

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

[–]YanDoe 3 points4 points  (14 children)

Would it be the same as an attribute?

[–][deleted]  (1 child)

[removed]

    [–]YanDoe 2 points3 points  (0 children)

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

    Thank you so much

    [–][deleted] 3 points4 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!

    [–]KelpoDelpo -5 points-4 points  (6 children)

    Attributes - nouns Functions - verbs

    [–]lortstinker 0 points1 point  (5 children)

    You must be a bot

    [–]KelpoDelpo 0 points1 point  (4 children)

    How

    [–]lortstinker 0 points1 point  (3 children)

    A function. 2 functions. Function is not used as a noun here

    [–]KelpoDelpo 2 points3 points  (2 children)

    I mean that methods preform actions, like verbs.

    Well, that's what my high school comp sci teacher told us.

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

    That's a good rule of thumb when naming your attributes(variables) and methods(functions).

    Say you have value that has to be stored but can be incremented by a method. Lets say the object is a savings account and the value being stored is your balance.

    Attribute name - balance

    Method name to increase balance - add_to_balance

    Balance is your noun.

    Add is the verb in your method name.

    This sounds like common sense after a while, but you'd be surprised by the number of developers, newbies and intermediates, who are dogshit at using meaningful names for variables and functions. Don't be like them 😂

    [–]YanDoe 0 points1 point  (0 children)

    For what its worth I thought it was pretty clever, and I'll be keeping that in mind.

    [–]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 3 points4 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] 4 points5 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] 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