Program Structure
Main Function Requirement
Every project must define a main
function, which serves as the entry point of execution.
Reasoning:
- Ensures a clear starting point for program execution.
- Improves code organization and readability.
- Makes it easier for tools and compilers to locate the main logic.
fn main() {
console.log("Hello, world!");
}
No Function Calls in Global Scope
Functions cannot be called directly in the global scope; all execution must happen within functions.
Reasoning:
- Prevents unintended side effects during program initialization.
- Encourages structured, modular code.
- Avoids execution order issues, ensuring a predictable runtime.
console.log("This is not allowed"); // ❌ Function call in global scope
fn main() {
console.log("This is allowed"); // ✅ Function call inside a function
}