# Methods and calls

> Define methods, parameters, return values, overloads, predicates, and generic contracts.

Canonical: https://alice-lang.org/documentation/language/methods/

[Documentation index](https://alice-lang.org/documentation/llms.txt) · [Complete book](https://alice-lang.org/llms-full.txt)

## Definition and return value

```alice
def add(a : Int32, b : Int32 = 1)
  a + b
end
def square(x : Int32) : Int32 = x * x
puts add(4)
puts add(4, 3)
puts square(5)
```

Expected output:

```text
5
7
25
```

[Edit and run in the Alice playground](https://playground.alice-lang.org/#code=u.ZGVmIGFkZChhIDogSW50MzIsIGIgOiBJbnQzMiA9IDEpCiAgYSArIGIKZW5kCmRlZiBzcXVhcmUoeCA6IEludDMyKSA6IEludDMyID0geCAqIHgKcHV0cyBhZGQoNCkKcHV0cyBhZGQoNCwgMykKcHV0cyBzcXVhcmUoNSk)

A method's last expression is its result. Annotate the result after the parameter list, or let Alice infer it. An endless definition, `def square(x : Int32) : Int32 = x * x`, is useful for a short expression.

`return value` exits early. A method that promises a result must supply it on every reachable path; otherwise it produces AL0308.

## Parameters and defaults

A parameter annotation follows `:`. A default follows `=`. Place optional parameters after the required ones so a call remains clear. The example's `add(4)` uses `b = 1`; `add(4, 3)` supplies both values.

Calls can use parentheses or command syntax:

```alice
puts add(2, 3)
puts "hello"
```

Use parentheses when nesting calls or mixing operators to make parsing and intent explicit. Calling a .NET API uses its declared signature and member name; Alice does not rename CLR methods into Ruby snake case.

## Inferred parameters

```alice
def length(x)
  x.Length
end
puts length("abc")
puts length([1, 2])
```

Untyped parameters can be monomorphized for the call-site types. This gives concise syntax with typed generated methods. Explicit types make an exported API easier to understand; `Any` requests dynamic dispatch instead of normal specialization.

## Method names

Predicate names can end in `?`, such as `empty?` or `positive?`. Mutating and asserting conventions may use `!`, as in `not_nil!`; a punctuation mark is part of the API's name, not a separate safety guarantee.

`def self.name` defines a static method on a class or module. Instance methods work on the receiver's state. See [classes](https://alice-lang.org/documentation/language/classes.md) and [modules](https://alice-lang.org/documentation/language/modules.md) for their lookup rules.

## Blocks and type parameters

A block parameter uses `&block : Input -> Output`; `yield` invokes it. The [blocks chapter](https://alice-lang.org/documentation/language/blocks.md) explains literal blocks and first-class lambdas.

A generic method uses `forall` and optional constraints:

```alice
def biggest(a : T, b : T) : T forall T where T : IComparable(T)
  a.CompareTo(b) >= 0 ? a : b
end
puts biggest(3, 7)
```

Constraints document the operations required on `T`. See [generics](https://alice-lang.org/documentation/language/generics.md) for generic classes and .NET types.

## Continue reading

- Previous: [Conditions and loops](https://alice-lang.org/documentation/language/control-flow.md)
- Next: [Blocks, yield, and lambdas](https://alice-lang.org/documentation/language/blocks.md)
