Camelia
Prev: Private Attributes

Method with parameters

Methods can have parameters, just as any subroutine would have.
examples/classes/point_05.p6
use v6;

class Point {
    has $.x is rw;
    has $.y is rw;

    method reset {
        $.x = 0;
        $.y = 0;
    }
    
    method set_coordinates($x, $y) {
        $.x = $x;
        $.y = $y;
    }
};

my $a = Point.new(x => 23, y => 42);

say $a.x;  # 23

$a.set_coordinates(10, 20);

say $a.x;  # 10

Prev: Private Attributes

Table of Contents

Comments

In the comments, please wrap your code snippets within <pre> </pre> tags and use spaces for indentation.
comments powered by Disqus

Perl 6 Tricks and Treats newsletter

Register to the free newsletter now, and get updates and news.
Email:
Name: