Built-in functions
Built-in functions are names provided directly by the language runtime. They do not need to be declared before use.
Built-in functions behave like ordinary function calls:
- they are called by name with parentheses
- their arguments are evaluated before the call
- they may return a value
- they may also have side effects such as printing or reading input
Unlike user-defined functions, their implementation lives in the interpreter rather than in a NEX function body.
The any type shown above is not a general
source-level wildcard type. It is reserved for selected built-in functions such
as print(…) and
print_inline(…). User programs currently
declare parameters, variables, and return types with the ordinary concrete
types of the language.
General built-ins
print
print(any msg) -> void
print(…) evaluates its argument and writes
the resulting value followed by a newline.
print("hello");
print(1 + 2);
This is the main output function in NEX.
print_inline
print_inline(any msg) -> void
print_inline(…) writes its argument without
appending a newline.
print_inline("name: ");
print("Ada");
This is useful for prompts and inline output.
version
version() -> str
version() returns the interpreter version as
a string.
print(version());
input
input() -> str
input() reads one line of user input and
returns it as a string.
print_inline("What is your name? > ");
str name = input();
print("Hello, " + name);
If input cannot be read, execution stops with a runtime error.
intstr
intstr(int value) -> str
intstr(…) converts an integer value to its
string representation.
str count = intstr(42);
print(count);
strint
strint(str value) -> int
strint(…) converts a string to an integer.
If the string cannot be parsed as an integer, the result is
0.
print(strint("123"));
print(strint("nope"));
Array built-ins
resize
resize(array<int> arr, int size) -> void
resize(array<str> arr, int size) -> void
resize(…) changes the logical length of an
array in place.
array<int> nums;
resize(nums, 3);
When an array grows, new slots are filled with the default value for the array
element type. For array<int>, that value is 0. For array<str>, that
value is "".
This builtin can also be called with method syntax:
nums.resize(3);
length
length(array<int> arr) -> int
length(array<str> arr) -> int
length(…) returns the current logical
length of an array.
array<int> nums;
nums.resize(3);
print(length(nums));
print(nums.length());
The method-style and function-style forms are equivalent.
reset
reset(array<int> arr) -> void
reset(array<str> arr) -> void
reset(…) replaces every existing element
with the default value for the array element type.
array<int> nums;
resize(nums, 3);
nums[0] = 7;
nums[1] = 9;
reset(nums);
print(nums[0]);
For array<int>, the default value is
0. For
array<str>, the default value is "".
This builtin can also be called with method syntax:
nums.reset();
Notes
- Built-in functions are part of the runtime namespace, not special statement forms.
- Because function calls are expressions, a built-in function can appear in a variable initializer, inside another call, or as a plain expression statement.
- Method-style calls such as
arr.length(),arr.resize(3), andarr.reset()are alternative surface syntax for ordinary built-in function calls.