Built-in Functions
Flexa provides a set of built-in functions that are always available, regardless of which libraries you import. These functions cover common tasks like input/output, string and array manipulation, and system operations. In this section, we’ll explore these functions and how to use them effectively.
Overview of Built-in Functions
Here are the main built-in functions in Flexa:
- print: Writes a message to the console without a newline.
- println: Writes a message to the console with a newline.
- read: Reads a line of input from the console.
- readch: Reads a single character from the console.
- len: Returns the length of a string or array.
- sleep: Pauses the program for a specified number of milliseconds.
- system: Executes a system command.
Using Built-in Functions
1. print
The print
function writes a message to the console without adding a newline at the end.
Syntax
print(message);
Example
print("Hello, ");
print("Flexa!");
// Output: Hello, Flexa!
2. println
The println
function writes a message to the console and adds a newline at the end.
Syntax
println(message);
Example
println("Hello, Flexa!");
// Output: Hello, Flexa! (followed by a newline)
3. read
The read
function reads a line of input from the console.
Syntax
var input = read();
Example
println("Enter your name:");
var name = read();
println("Hello, " + name + "!");
4. readch
The readch
function reads a single character from the console.
Syntax
var char = readch();
Example
println("Press any key:");
var key = readch();
println("You pressed: ", key);
5. len
The len
function returns the length of a string or array.
Syntax
var length = len(value);
Example
var str = "Hello, Flexa!";
var arr = {1, 2, 3, 4, 5};
println("Length of string: ", len(str)); // Output: 13
println("Length of array: ", len(arr)); // Output: 5
6. sleep
The sleep
function pauses the program for a specified number of milliseconds.
Syntax
sleep(milliseconds);
Example
println("Starting in 3 seconds...");
sleep(3000); // Pause for 3 seconds
println("Go!");
7. system
The system
function executes a system command.
Syntax
var result = system(command);
Example
var result = system("ls -l"); // Lists files in the current directory (Unix-based systems)
println(result);
Examples of Built-in Functions
Example 1: Basic Input/Output
println("What is your name?");
var name = read();
println("Hello, " + name + "!");
println("Press any key to exit...");
var key = readch();
println("You pressed: " + key);
Example 2: Measuring Array Length
var numbers = {1, 2, 3, 4, 5};
println("The array has ", len(numbers), " elements.");
Example 3: Using sleep
for Delays
println("Starting countdown:");
for (var i = 5; i >= 1; i--) {
println(i);
sleep(1000); // Wait 1 second
}
println("Blast off!");
Example 4: Executing System Commands
println("Listing files in the current directory:");
var result = system("dir"); // Windows
// var result = system("ls -l"); // Unix-based systems
println(result);
Best Practices
- Use
println
for Debugging: Useprintln
to print debug messages during development.println("Debug: Current value = " + value);
- Validate Input: Always validate input from the user to avoid unexpected behavior.
using flx.std.utils; // is_number var age = read(); if (is_number(age)) { println("Your age is: ", age); } else { println("Invalid input. Please enter a number."); }
-
Avoid Blocking Calls: Use
sleep
judiciously to avoid blocking the program for too long. - Handle System Command Output: Be cautious when using
system
, as it executes commands directly on the operating system.
What’s Next?
Now that you’re familiar with the built-in functions, it’s time to explore advanced examples that combine multiple features of Flexa. Head over to the Advanced Examples section to see Flexa in action.
← Back to Built-in Libraries | Next: Advanced Examples → |