Mata course: Functions

Example

real matrix id(real scalar n)
{
real scalar i
real matrix res

res = J(n, n, 0)
for (i=1; i<=n; i++) {
res[i,i] = 1
}
return(res)
}

Syntax

1. They are used in Mata programs

2. They follow the classic definition

f(a,b,c,...) returns a value based on a, b, c, ...

or

f(a,b,c,...) returns multiple values by modifying its arguments

or

f(a,b,c,...) does something but returns nothing

3. The words function, subroutine, and program are used interchangeably;
all mean the same thing

Flavors of functions

1. Built-in

They are part of Mata itself, and are written in C

2. Library

They are part of Mata, and are written in Mata

3. User written

All other, including the functions (programs, subroutines)
you write

Sometimes the word built-in is used to refer to (2).

Top