Method

Method

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.

Method (C#)

A method in C# is a function defined inside a class that defines what an object can do.

This page covers Method in C#. For a language-agnostic introduction, see Method.

Defining a Method#

A C# method has an access modifier, a return type, a name, and a body.

public void Greet()
{
    Console.WriteLine("Hello!");
}
  • public — the access modifier; public means the method can be called from anywhere
  • void — the return type; void means the method returns nothing
  • Greet — the method name; use PascalCase by convention
  • () — the parameter list; empty here, but arguments go inside these parentheses

Return Types#

If a method produces a value, declare the return type instead of void and use return to send it back.

Method (JavaScript)

A method in JavaScript is a function defined inside a class body.

This page covers Method in JavaScript. For a language-agnostic introduction, see Method.

Defining a Method#

Methods in a JavaScript class are written without the function keyword — just the name, parentheses, and body.

class Player {
    greet() {
        console.log("Hello!");
    }
}

By convention, method names use camelCase: greet, play, getScore.

Return Values#

Use return to send a value back. A method without a return statement returns undefined.

Method (Python)

A method in Python is a function defined inside a class using the def keyword.

This page covers Method in Python. For a language-agnostic introduction, see Method.

Defining a Method#

Every instance method in Python takes self as its first parameter. self is a reference to the object the method is being called on.

class Player:
    def greet(self):
        print("Hello!")

By convention, method names use snake_case: greet, play, get_score.

Return Values#

Use return to send a value back from a method. A method without a return statement returns None.