Function

Use functions to modularize shared logic such as data extraction and generation.

Definition

Use the func keyword to define either single or multi-line functions. Any valid expression can be used on the right-hand side of the -> symbol.

func increment(value: integer) -> value + 1;

func addAndDouble(lhs: number, rhs: number) -> {
  let total = lhs + rhs;

  total * 2;
};

Empty Arguments

If your function does not take any arguments you can drop the parentheses as well.

func stringFactory -> "string";

const STRING = stringFactory();

Default Arguments

You can make your arguments optional by passing default values. However, if you add a default value you must also add default values for all subsequent arguments.

func partialAdd(lhs, rhs = 10) -> lhs + rhs;

const PARTIAL_CALL = parialAdd(5);      // 15
const FULL_CALL = partialAdd(5, 2);     // 7

Last updated