Course of Raku / Objects, I/O, and exceptions / Classes and objects

Subroutines vs methods

You now have two ways to package a piece of behaviour: a subroutine and a method. They look similar but are used differently.

A subroutine stands on its own. You call it by its name and pass it everything it needs as arguments:

sub area-of($radius) {
    π * $radius * $radius;
}

say area-of(2); # 12.566370614359172

A method belongs to a class and is called on an object with the dot. It can use the object’s own data through self and the attribute accessors, so you do not pass that data in:

class Circle {
    has $.radius;

    method area {
        π * $.radius * $.radius;
    }
}

say Circle.new(radius => 2).area; # 12.566370614359172

Both compute the same number. The difference is where the data comes from: the subroutine receives the radius as an argument, while the method reads it from the object it was called on.

As a rule of thumb, use a subroutine for a standalone operation that simply transforms its arguments, and a method when the behaviour naturally belongs to an object and works with that object’s own state. For class-specific, but generic routines, use class methods.

Practice

Complete the quiz that covers the contents of this topic.

Exercises

This section contains 2 exercises. Examine all the topics of this section before doing the coding practice.

  1. Triple as a subroutine
  2. Triple as a method

Course navigation

Quiz — Class methods   |   Triple as a subroutine