Language
Guides
Functions

Functions and their types

Functions in Tact could be defined in different ways:

  • Global static function
  • Extension functions
  • Mutable functions
  • Native functions
  • Receiver functions
  • Getter functions

Global static functions

You can define global function anywhere in your program:

fun pow(a: Int, c: Int): Int {
  let res: Int = 1;
  repeat(c) {
    res = res * a;
  }
  return res;
}

Extension function

Extension functions allow you to implement extensions for any possible type.

Warning The name of the first argument MUST be named self and the type of this argument is the type you are extending.

extends fun pow(self: Int, c: Int) {
  let res: Int = 1;
  repeat(c) {
    res = res * self;
  }
  return res;
}

Mutable functions

Mutable functions are performing mutation of a value replacing it with an execution result. To perform mutation, the function must change the self value.

extends mutates fun pow(self: Int, c: Int) {
  let res: Int = 1;
  repeat(c) {
    res = res * self;
  }
  self = res;
}

Native functions

Native functions are direct bindings of func functions:

Note Native functions could be also mutable and extension ones.

@name(store_uint)
native storeUint(s: Builder, value: Int, bits: Int): Builder;
 
@name(load_int)
extends mutates native loadInt(self: Slice, l: Int): Int;

Receiver functions

Receiver functions are special functions that are responsible for receiving messages in contracts and could be defined only within a contract or trait.

contract Treasure {
 
  // ...
  
  // This means that this contract can receive the comment "Increment" and this function would be called for such messages
  receive("Increment") {
    self.counter = self.counter + 1;
  }
}

Getter Functions

Getter functions define getters on smart contracts and can be defined only within a contract or trait.

contract Treasure {
 
  // ...
  
  get fun counter(): Int {
    return self.counter;
  }
}