# 9.2 Methods and This

> A method is a subprogram that lives inside a class and works on a particular object: the one named before the dot.

Web page: https://pseudolearn.app/en/spec/objetos/metodos

> **In one sentence**
>
> A method is a subprogram that lives inside a class and works on a particular object: the one named before the dot.

## 9.2.1 How to write it

```pseudolearn
⟦Public | Private⟧ Method «nombre»(«parámetros») ⟦As «tipo»⟧
  «instrucciones»
EndMethod
```

> **Parts**
>
> - `Method` opens the method. It may be preceded by `Public` (the default) or `Private`.
> - «parameters» and `As` «type» follow the same rules as in a [subprogram](https://pseudolearn.app/en/spec/subprogramas/declaracion.md).
> - `EndMethod` closes the method.

## 9.2.2 How it works

A method is called on an object: `pasos.Sumar()`. That object is the **receiver**, and inside the method it is named with `This`. To use an attribute or method of the current object, `This.` is **required**: a bare name can only be a local variable or a parameter.

### Program: A counter with state

```pseudolearn
Class Contador
  Private Define valor As Integer;

  Method Constructor(inicial As Integer)
    This.valor <- inicial;
  EndMethod

  Method Sumar()
    This.valor <- This.valor + 1;
  EndMethod

  Method Actual() As Integer
    Return This.valor;
  EndMethod
EndClass

Algorithm Pasos
  Define pasos As Contador <- New Contador(10);
  pasos.Sumar();
  pasos.Sumar();
  Write pasos.Actual();
EndAlgorithm
```

**Output**

```text
12
```

*The program finishes normally.*

**Figure 9.2-1: The receiver and This**

The variable pasos and, inside the method, This: both are references pointing to the same object, with valor = 12.

When calling `pasos.Sumar()`, `This` inside the method is the same object as `pasos`.

The dot can be chained: `New Contador(1).Actual()`. An attribute after the dot is a [designator](https://pseudolearn.app/en/spec/declaraciones/designadores.md); a method call is not. The receiver is computed only once.

There is no overloading: two members of a class cannot share a name, even if one is an attribute and the other a method.

> **Common mistakes**
>
> ### Program: An attribute without This
>
> ```pseudolearn
> Class Contador
>   Define valor As Integer;
>   Method Sumar()
>     valor <- valor + 1;
>   EndMethod
> EndClass
>
> Algorithm Mal
> EndAlgorithm
> ```
>
> **Diagnostics**
> - `identifierMatchesFieldWithoutThis` (error, 4:5): Unqualified field access; use this.valor
>
> *The program finishes with an error.*

> **Under the hood**
>
> A method is one more function of the intermediate representation with a hidden first parameter: the receiver. `pasos.Sumar()` becomes a call that passes `pasos` as that first argument.
>
> **Intermediate representation**
>
> ```text
> module Pasos en
> class Contador { valor: i64 }
> func suspendible @main() -> unit {
> b0() mass 4:
>   stmt.enter @18:3-18:47
>   %0 = const.i64 10 @18:44-18:46
>   %1 = object.new Contador @18:31-18:47
>   %2 = call @Contador.Constructor(%1, %0) @18:31-18:47
>   stmt.exit @18:3-18:47
>   stmt.enter @19:3-19:16
>   %3 = call @Contador.Sumar(%1) @19:3-19:16
>   stmt.exit @19:3-19:16
>   stmt.enter @20:3-20:16
>   %4 = call @Contador.Sumar(%1) @20:3-20:16
>   stmt.exit @20:3-20:16
>   stmt.enter @21:3-21:23
>   %5 = call @Contador.Actual(%1) @21:9-21:23
>   out.write %5, newline @21:3-21:23
>   stmt.exit @21:3-21:23
>   return @22:1-22:13
> }
> func @Contador.Constructor(%0: obj<Contador>, %1: i64) -> unit {
> b0() mass 1:
>   stmt.enter @5:5-5:26
>   object.store %0, valor, %1 @5:5-5:26
>   stmt.exit @5:5-5:26
>   return @6:3-6:12
> }
> func @Contador.Sumar(%0: obj<Contador>) -> unit {
> b0() mass 1:
>   stmt.enter @9:5-9:33
>   %1 = object.load %0, valor @9:19-9:30
>   %2 = const.i64 1 @9:32-9:33
>   %3 = int.add %1, %2 @9:30-9:31
>   object.store %0, valor, %3 @9:5-9:33
>   stmt.exit @9:5-9:33
>   return @10:3-10:12
> }
> func @Contador.Actual(%0: obj<Contador>) -> i64 {
> b0() mass 1:
>   stmt.enter @13:5-13:22
>   %1 = object.load %0, valor @13:12-13:22
>   stmt.exit @13:5-13:22
>   return %1 @13:5-13:22
> }
> ```

**In short**
- A method works on its receiver, the object before the dot.
- Inside, `This.` is required for the object's members.
- Methods can be public or private; there is no overloading.

**See also**
- [9.1 Classes and attributes](https://pseudolearn.app/en/spec/objetos/clase.md)
- [9.4 Inheritance and overriding](https://pseudolearn.app/en/spec/objetos/herencia.md)
