Skip to content

Defining Methods

Typing

All method arguments MUST have a type and all methods MUST have an explicit return type.

Examples

Valid

add(a: uint64, b: uint64): uint64 {
return a + b
}

Invalid

add(a: uint64, b: uint64) { // missing return type
return a + b
}
add(a, b: uint64): uint64 { // missing argument type
return a + b
}

ABI Methods

All methods defined in your contract will be routed as ABI methods, unless they have the private keyword. This means they will be exposed in the ABI and Application Spec generated by TEALScript.

Private Subroutines

A method defined with the private keyword will only be accessible within the contract itself via a subroutine. This means other contracts or end-users cannot call this method directly and it will not be exposed in the ABI or Application Spec.

Example

In this example, there is a single public ABI method doMath which will then call the add or subtract subroutines depending on the method given. doMath is the only method that is externally callable.

class Calculator extends Contract {
/**
* Calculates the sum of two numbers
*
* @param a
* @param b
* @returns The sum of a and b
*/
private getSum(a: uint64, b: uint64): uint64 {
return a + b;
}
/**
* Calculates the difference between two numbers
*
* @param a
* @param b
* @returns The difference between a and b.
*/
private getDifference(a: uint64, b: uint64): uint64 {
return a >= b ? a - b : b - a;
}
/**
* A method that takes two numbers and does either addition or subtraction
*
* @param a The first number
* @param b The second number
* @param operation The operation to perform. Can be either 'sum' or 'difference'
*
* @returns The result of the operation
*/
doMath(a: uint64, b: uint64, operation: string): uint64 {
let result: uint64;
if (operation === 'sum') {
result = this.getSum(a, b);
} else if (operation === 'difference') {
result = this.getDifference(a, b);
} else throw Error('Invalid operation');
return result;
}
}