Your First Program
In this guide, you'll write your first Strata program and learn the basics of the language.
The Hello World Program
Let's start with the classic "Hello, World!" program:
fn main(): Void {
print("Hello, World!");
}Save this as hello.str and run it:
strata run hello.strOutput: Hello, World!
Tip: The
strata runcommand compiles and executes your code in one step, making it perfect for development. Alternatively, you can build first withstrata build hello.str, then run the compiled output withphp build/hello.php(if you have PHP installed).
Understanding the Code
Function Declaration
fn main(): Void {fn- keyword to declare a functionmain- function name (the entry point): Void- return type (returns nothing){- start of function body
The print Statement
print("Hello, World!");print()- built-in function to output text"Hello, World!"- a string literal- Semicolons are required at the end of statements
Return Statement
The main() function returns Void.
A More Complex Example
Let's create a program that does more:
fn greet(name: String): String {
return "Hello, ${name}!";
}
fn main(): Void {
let message = greet(name: "Strata");
print(message);
}Breaking It Down
Function with Parameters:
fn greet(name: String): String {name: String- parameter namednameof typeString: String- function returns aString- Named arguments are required when calling
String Interpolation:
return "Hello, ${name}!";${name}- interpolates the variablenameinto the string- Similar to PHP's
"Hello, {$name}!"
Variable Declaration:
let message = greet(name: "Strata");let- declares a variable- Type is inferred from the function return type
- Could also write:
let message: String = greet(name: "Strata")
Function Call:
greet(name: "Strata");- Named arguments are mandatory
name:specifies which parameter you're passing
Top-Level Code
Important: In Strata, the top level of a file is primarily for declarations. All complex logic must reside inside functions.
Allowed at top level:
- Namespace declarations (
namespace) - Import statements (
import) - Type declarations (
type) - Function declarations (
fn) - Class, Interface, and Trait declarations (
class,interface,trait) letvariable declarations (definitions only)echoandprintstatements
Forbidden at top level (must be inside functions):
- Control flow:
if/else,match - Loops:
while,for-each - Error handling:
try/catch,throw - State mutation: Assignment to existing variables (
x = y) - Standalone expressions (like calling a function without
print)
See Top-Level Code for complete details.
Key Concepts
1. Type Safety
Strata is statically typed, with explicit types where required and inference where possible:
let count: Int = 10; // correct
let count = 10; // also correct (type inferred)
let count: String = 10; // error: type mismatch2. Named Arguments
All function arguments must be named:
fn add(a: Int, b: Int): Int {
return a + b;
}
// correct
let sum = add(a: 5, b: 3);
// Error: positional arguments not allowed
let sum = add(5, 3);3. Semicolons Required
Strata requires semicolons at the end of statements (like PHP):
print("Hello"); // semicolon required
print("World"); // also requiredExercises
Try these exercises to practice:
- Greet Multiple People: Create a function that greets multiple people
- Calculator: Create functions for add, subtract, multiply, and divide
- Temperature Converter: Convert Fahrenheit to Celsius
Next Steps
- Types - Learn about Strata's type system
- Functions - Deep dive into functions
- Examples - See more code examples
Ready to learn more? Check out the Language Reference →