Tutorials Home   >   Programming Basics   >   What is a Method?

What is a Method?

What Is a Method in Programming?

Introduction

As you learn programming, you may hear the words function and method used, sometimes in similar ways. While they are closely related, a method has a specific meaning, especially in object-oriented programming (OOP).

Methods help programs perform actions related to objects. They allow data and behavior to work together in an organized way. Understanding methods is important for learning languages such as Java, Python, and C++.

In this explanation, you will learn:

  • What a method is

  • How methods are different from functions

  • Why methods are important

  • Types of methods

  • Examples of methods in different programming languages


1. Definition of a Method

A method is a block of code that is defined inside a class and is used to perform an action on an object.

Simple Definition:

A method is a function that belongs to a class and operates on objects of that class.

In simple terms, methods describe what an object can do.


2. Methods and Object-Oriented Programming

In object-oriented programming:

  • A class is a blueprint

  • An object is an instance of a class

  • A method defines the behavior of the object

Example:

  • Class: Car

  • Object: myCar

  • Methods: start(), stop(), accelerate()


3. Why Are Methods Important?

Methods are important because they:

3.1 Combine Data and Behavior

Methods work with the data (variables) inside a class.


3.2 Improve Code Organization

By using methods, code becomes structured and easier to understand.


3.3 Support Reusability

A method can be used by many objects of the same class.


4. Parts of a Method

A method usually includes:

  1. Method name

  2. Parameters (inputs)

  3. Method body (code)

  4. Return type or value (optional)


5. Defining a Method

Example (Java):

class Calculator {
int add(int a, int b) {
return a + b;
}
}

Here:

  • add is the method name

  • a and b are parameters

  • The method returns a value


6. Calling a Method

Methods are called using an object.

Example:

Calculator calc = new Calculator();
int result = calc.add(5, 3);

7. Methods in Python

In Python, methods are functions defined inside a class.

Example:

class Student:
def greet(self):
print("Hello Student")

Calling the method:

s = Student()
s.greet()

The keyword self refers to the current object.


8. Methods vs Functions

Feature Function Method
Belongs to Program Class
Called using Function name Object
Used in All programs Object-oriented programs
Example print() object.method()

9. Types of Methods

9.1 Instance Methods

  • Work on object data

  • Use object variables

Example:

void displayName() {
System.out.println(name);
}

9.2 Static Methods

  • Belong to the class, not an object

  • Called using class name

Example:

static void showMessage() {
System.out.println("Welcome");
}

9.3 Built-in Methods

Methods already provided by a language or class.

Examples:

  • length() in Java

  • upper() in Python strings


10. Real-World Examples of Methods

  • A BankAccount class with methods like deposit() and withdraw()

  • A Student class with a method calculateGrade()

  • A Phone class with methods call() and sendMessage()


11. Common Mistakes Learners Make

  1. Forgetting to create an object before calling a method

  2. Confusing methods with functions

  3. Forgetting to use self in Python methods

  4. Using wrong parameters


12. Best Practices for Using Methods

  • Give methods clear and meaningful names

  • Keep methods short and focused

  • Avoid repeating code

  • Use methods to represent actions


Conclusion

A method is a function that belongs to a class and defines what an object can do. Methods are a key part of object-oriented programming and help organize code by combining data and behavior.

By understanding methods, learners can build well-structured programs that are easier to read, maintain, and expand.