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

前端 未结 30 3400
粉色の甜心
粉色の甜心 2020-11-21 05:08

Can someone provide a simple explanation of methods vs. functions in OOP context?

30条回答
  •  梦谈多话
    2020-11-21 05:32

    From my understanding a method is any operation which can be performed on a class. It is a general term used in programming.

    In many languages methods are represented by functions and subroutines. The main distinction that most languages use for these is that functions may return a value back to the caller and a subroutine may not. However many modern languages only have functions, but these can optionally not return any value.

    For example, lets say you want to describe a cat and you would like that to be able to yawn. You would create a Cat class, with a Yawn method, which would most likely be a function without any return value.

提交回复
热议问题