Functions
Functions are a way to use various functionality of the calculator that goes beyond operators and values. Function names are not case-sensitive.
ABCalc includes over a hundred of built-in functions in several categories:
- General
- Arithmetic
- Trigonometry
- Financial
- Scientific
- Conversion
- Statistical
- String and Char
- Date and Time
You will find the full list of function descriptions in the application, on the Functions tab.
ABCalc also supports custom functions right in the expressions or (in ABCalc Plus) in libraries.
Custom Functions in Expressions
Functions in an expression let you define reusable functionality or just something that you need to execute multiple times within the expression.
A function consists of a header and a body. The body is composed of one or multiple expressions.
A function yields nothing (so you cannot write the function in place of a value), but the function body does return some result (even if it is a null result) when the function is called and its body is evaluated.
A function can be declared in one of two forms:
a) a function with a body:
fn DecodeIOCtl(code)
{
return (
(code >> 16) bit_and 0x0000FFFF;
(code >> 14) bit_and 0x00000003;
(code >> 2) bit_and 0b0111111111111;
code bit_and 0x3
)
};
b) a short notation for small functions:
fn DecodeIOCtlCode(code) => DecodeIOCtl(code);
Function parameters
A function may include zero, one, or multiple parameters:
fn NoParams() => ...
fn OneParam(param) {...}
fn ManyParams(a; b; c; d; e) {...}
Parameters may be optional. To specify such a parameter and provide a default value to it, put down the name of the parameter followed by "=" and a default value as shown below. The "=" character is used to specify the default value regardless of the option to use ":=" or "=" for an assignment. You may specify zero or more mandatory parameters followed by zero or more optional parameters.
Examples:
fn MyRound(value, up = false) => up ? Round(value) : Trunc(value);
fn MyRound(value, up = false) => iff(up; Round(value); Trunc(value)) // iff is a built-in function that works as a ternary operator.
fn BuildNum(date = null)
{
if (date = null) { date := Today() };
return Trunc(TimespanMSec(date - #2000-01-01Z#) / 86400000);
};
Returning results
A function may return a value (including a list) or null. The result may be returned implicitly (derived from the last expression in the function body) or explicitly using the return keyword followed by a value or an expression that yields a value during evaluation.
Examples:
fn MeaningOfLife() => 42;
fn MeaningOfLifeExplicit() => return 4*10 + 2;
Functions and Memory Context
The expression and its functions share common memory - they can read and modify the same memory cells (variables). The only exception is function parameters - if there exists a memory cell (variable) X and a function has a parameter named X, then if a function references "X", the value of the parameter will be read and updated, while the memory cell (variable) will not be accessed.
Custom Functions in Libraries
In ABCalc Plus, you can create a library of your own functions. Functions can be stored in one or several files with the ".abcf" extension. Put the file(s) to some directory and specify the location of this directory in the Custom Functions window of ABCalc Plus. The default location is:
- Windows - C:\Users{username}ugene\AppData\Roaming\ABCalc\Functions
- Linux - typically, /home/{username}/.config/ABCalc/Functions
- macOS - typically, /Users/{username}/Library/Application Support/ABCalc/Functions
The functions in the library are parsed during loading and are executed when an expression references them. When functions are executed, they are isolated and do not have access to main memory cells (variables) but have their own "memory pool" usable for storing intermediate calculation results. This allows you not to worry that variable names in main memory will conflict with the variables in the functions.