A method is a function that belongs to a class. It defines what an object can do.
Where a plain function stands on its own, a method is always attached to a class. When you call a method, you call it on a specific object — and the method can work with that object’s data.
Defining a Method#
A method is defined inside the class body. It has a name, a return type, and a body.
Calling a Method#
You call a method on an object using dot notation: object.MethodName().
player.Greet()
│ └── method name
└── objectThe method runs in the context of that specific object. If it uses any of the object’s data, it uses that object’s data — not another instance’s.
Methods vs Functions#
A function and a method do the same job — they group code under a name so it can be reused. The difference is where they live. A function is standalone. A method is defined inside a class and tied to an object.
Common Mistakes#
Calling a method on the class instead of an object
Methods belong to objects, not classes. Player.Greet() is wrong if Greet is a non-static method — you need an instance: player.Greet().
Forgetting the parentheses
Writing player.Greet without () doesn’t call the method — it references it. Always include () when calling a method, even if it takes no arguments.
Defining a method outside the class body A method defined outside the class braces is not part of that class. Check your indentation and brace placement carefully.
Resources#
- Method (computer programming) (external link) — Wikipedia