Articles

Function vs Method: Everything You Need to Know

The main difference between a function and a method: Functions are standalone blocks of code that can be called independently, while methods are functions that belong to objects or classes. Both perform tasks, but the methods are tied to the data they operate on.

This guide explains the key differences between functions and methods in programming with practical examples.

Method vs function: The key differences

The key difference between methods and functions lies in where they live and how they’re called. This table summarizes the key differences between a function and a method:

Feature Function Method
Where it lives On its own Inside a data structure (like an object or a class)
How it’s called Just its name Object name + dot operator + method name
Binding to data No Yes, it operates on the data it’s part of
Used for General-purpose actions Object-specific behaviors
Container needed No Yes

So, a key takeaway is that:

  • Functions live on their own and can be called directly
  • Methods live inside objects and are called through the object

Now let’s explore each concept in detail.

What is a function in programming?

Think of a function like a coffee machine. You press a button, it takes your input (coffee type), runs a set of steps (grinds beans, brews water), and gives you a nice hot cup of coffee. Just like that, in programming, a function takes input, does some work, and returns output.

It’s a reusable block of code that helps avoid repeating code. You define it once and use it as many times as you want with different inputs if needed. Let’s implement this function in JavaScript:

function makeCoffee(type) {
console.log("Here is your " + type + " coffee!");
}
makeCoffee("Espresso");
makeCoffee("Latte");

The output generated by this code will be:

Here is your Espresso coffee!
Here is your Latte coffee!

In this code, we:

  • Defined a function using the function keyword, and the name of this function is makeCoffee. It takes one input: type, also called parameter.

  • We print a message inside the function using console.log(). The message changes depending on the type of coffee you pass.

  • We call the function twice: once with "Espresso" and once with "Latte". These values are termed as arguments

The key advantages of a function are that it saves time, reduces repetition, and makes code easier to manage. Functions, when tied to an object, become methods. So, let’s understand them in detail.

What is a method in programming?

A method is just a function that lives inside an object. It does everything a function does, but it’s tied to the object it belongs to.

Imagine you have a friend named Alex, and Alex can speak.

  • “Speak” is something Alex knows how to do, it’s a method.

  • You can’t call speak() on its own, you have to say Alex.speak() because it belongs to Alex.

Let’s implement this method in JavaScript:

const person = {
speak: function() {
console.log("Hello, I can talk!");
}
};
person.speak();

This code will produce the following output:

Hello, I can talk!

In this code:

  • We created an object called person.
  • Inside it, we defined a method called speak.
  • That method prints a message.
  • To run it, we call it person.speak() because the method belongs to the person object.

Now that you know what functions and methods are, and how they differ, you have the foundation to use them confidently in your programs.

Conclusion

The fundamental difference between method and function comes down to context and ownership. Functions are independent blocks of code that can be called directly and work with any data you pass to them. Methods, on the other hand, are functions tied to objects and operate on the specific data within those objects.

Understanding this method vs function distinction is crucial whether you’re working with APIs, building object-oriented applications, or just writing clean, organized code. Remember: if it’s called on its own, it’s a function. If it’s called through an object using dot notation, it’s a method

Want to learn more about core programming concepts? Explore the Learn How to Code course on Codecademy.

Frequently asked questions

1. What is the difference between a main method and a function?

A function is a standalone block of code that performs a specific task. A main method, often seen in languages like Java or C++, is a special method that serves as the entry point of a program. While all methods are functions in concept, the main method has the unique role of starting the program’s execution.

2. What is the difference between a method and a class function?

In many languages, a class function is another term for a method: a function defined inside a class. The difference is mainly in terminology. However, some languages (like Python) differentiate between instance methods (that use self and act on object data) and class methods (that use cls and work at the class level).

3. Why are functions called methods?

Functions are called methods when they are associated with an object or class. The term “method” reflects that the function is a behavior or action tied to that specific object, rather than a general-purpose piece of code.

Codecademy Team

'The Codecademy Team, composed of experienced educators and tech experts, is dedicated to making tech skills accessible to all. We empower learners worldwide with expert-reviewed content that develops and enhances the technical skills needed to advance and succeed in their careers.'

Meet the full team

Learn more on Codecademy