Functions
A function is a subroutine used to encapsulate a section of code to keep the main body of code clear and user-readable. Often given descriptive names that reflect their purpose, functions are a cornerstone of clean and efficient code. Earlier in the course we used the printf() function to print text to the console output window. While some functions are predefined in t, the language fully supports user-developed functions.
float test (float parameter)
{
parameter += 1;
return parameter;
}
Like a variable, the first thing we need to do is declare the function. This includes declaring the function’s type which also designates what type of value will be returned by the function when called. Functions that return no value are declared void. In our example below, we define our function as we declare it, but a function can be declared before it is defined such as float test (float parameter);
with the most import detail being that a function is always declared before it is called; even if it is not defined until later in the script. Like C++, functions can be overloaded by defining different functions with the same name but different types of parameters passed to the function. Each definition must simply return the same type.
float test (int parameter)
{
parameter += 1;
return parameter;
}
Predefined Functions
While t places few limitations on developing your own functions, much of what one may want to do in a t script is already predefined with a library of predefined functions. From setting bitrates to sending messages, starting and stopping logging, to loading additional t scripts and beyond. A great deal has already been developed for your convenience.