The Boa Programming Guide - User-Defined Functions

While Boa strives to provide useful domain-specific types and functions for performing software mining research, anticipating all specific user needs is not feasible. This is why the language provides the ability for users to define their own functions. In this section, we outline the syntax for defining functions and demonstrate with a small example.

The general form of a user-defined function is given below:

id := function(arg1: arg1Type, arg2: arg2Type, ..) [ : retType ] { body [ return retVal; ] };

Functions usually are assigned to variables. This is how the function can later be called. Functions declare the arguments and their types, the function's return type (if any), and provide a body for the function. The body returns values, if the function declares a return type.

As an example, imagine we want a function that takes a Revision and determines if any files committed in that revision have the file extension '.java'. If at least one such file exists in the Revision, we return true. Otherwise we return false. The function HasJavaFile below implements this functionality.

HasJavaFile := function(rev: Revision): bool { exists (i: int; match(`\.java$`, rev.files[i].name)) return true; return false; };

This function is assigned to the HasJavaFile variable. It declares one argument named rev of type Revision. It also declares the return type of the function to be bool.

More examples of function declarations can be seen on the domain-specific functions page.