Custom Functions (defs)

Scott Waldron Updated by Scott Waldron

In addition to the Dino built-in functions, you can of course define your own functions. The keyword def is used to start a function definition.

def triassic() {
return 'T-Rex came later';
}

Functions are not required to return a value. A function body without a return statement will just return a special value known as null.

def nothing() {
}

var fossil = nothing(); // null

Functions can take any number of parameters. Parameters in Dino are dynamically typed; you do not specify what the type is – you simply use one or more identifiers.

def rex(name, age) {
// ...
}

Function Visibility

Functions can have private visibility so that they are not imported. By default, functions have public visibility. You can include the public keyword if you want, but it is not required.

// egg.dino
private def egg() {
return 'Only my mother can see me.';
}

print(egg()); // ok, we are in the script

def rex() {
return 'I can be seen by others.';
}
// outsideWorld.dino
import script 'egg.dino' as e;

print(e.rex()); // ok
print(e.egg()): // error, the egg function is privately defined in egg.dino

Functions as Variables

You can assign functions to variables and call them. This is often referred to as a delegate, and when passed as an argument to another function, a callback or function pointer.

def info(a) {
return a + ' is a big dino.';
}
private var delegate = info;
print(delegate('brontosaurus')); // 'brontosaurus is a big dino'

Nested Functions

Functions do not have to be declared in the global scope – they can be defined in any scope. Functions defined in a local scope such as another function body are always private visibility, and calling them outside of their local scope is an error.

def outside(a, b) {
def inside(c) {
return a + c;
}
return inside(b);
}

outside(4, 5); // 9
inside(3); // error, unknown symbol 'inside'

Parameter Defaults

Def statements may define “parameter defaults”, that is parameters that are considered optional from the call site and will have a default value if omitted.

def rex(a, b = 10) {
return a + b;
}
var a = rex(10); // a == 20

In Dino, all parameters are optional – missing arguments are simply set to null, and extra arguments are discarded and ignored. What this means is that setting a parameter to a default of null is technically not required, because the missing argument will cause the parameter to be set to null in any case.

def rex(a, b) {
}
rex(10); // ok, b will just be null
def rex(a, b = null) {
} // same as above for practical purposes
rex(10); // ok
rex(10, 20, 30, 40, 50); // last 3 args are discarded

Parameters with default values must come last in the parameter list. It is an error to define a parameter with a default value and follow it with another that does not have a default value.

def rex(a = 20, b) {
} // error

def raptor(a = 10, b = 15, c = 20, d = 25) {
} // ok, all params have defaults

How did we do?

Built-In Functions

Anonymous Functions

Contact