docs: add example programs
- hello.bux: Hello World with PrintLine - fibonacci.bux: Recursive Fibonacci with while loop - factorial.bux: Recursive Factorial computation - structs.bux: Struct creation and field access All examples compile and run successfully via 'bux run'
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
// Factorial - Recursive function
|
||||
extern func Std_Io_PrintLine(s: String);
|
||||
extern func Std_Io_PrintInt(n: int);
|
||||
|
||||
func Factorial(n: int) -> int {
|
||||
if n <= 1 {
|
||||
return 1;
|
||||
}
|
||||
return n * Factorial(n - 1);
|
||||
}
|
||||
|
||||
func Main() -> int {
|
||||
Std_Io_PrintLine("Factorials:");
|
||||
|
||||
var i: int = 1;
|
||||
while i <= 10 {
|
||||
let fact: int = Factorial(i);
|
||||
Std_Io_PrintInt(i);
|
||||
Std_Io_PrintLine("! = ");
|
||||
Std_Io_PrintInt(fact);
|
||||
Std_Io_PrintLine("");
|
||||
i = i + 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
// Fibonacci - Recursive function with while loop
|
||||
extern func Std_Io_PrintLine(s: String);
|
||||
extern func Std_Io_PrintInt(n: int);
|
||||
|
||||
func Fibonacci(n: int) -> int {
|
||||
if n <= 1 {
|
||||
return n;
|
||||
}
|
||||
return Fibonacci(n - 1) + Fibonacci(n - 2);
|
||||
}
|
||||
|
||||
func Main() -> int {
|
||||
Std_Io_PrintLine("Fibonacci sequence:");
|
||||
|
||||
var i: int = 0;
|
||||
while i < 10 {
|
||||
let fib: int = Fibonacci(i);
|
||||
Std_Io_PrintInt(fib);
|
||||
Std_Io_PrintLine("");
|
||||
i = i + 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
// Hello World - Basic Bux program
|
||||
extern func Std_Io_PrintLine(s: String);
|
||||
|
||||
func Main() -> int {
|
||||
Std_Io_PrintLine("Hello, Bux!");
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
// Structs - Basic struct usage
|
||||
extern func Std_Io_PrintLine(s: String);
|
||||
extern func Std_Io_PrintInt(n: int);
|
||||
|
||||
struct Point {
|
||||
x: int;
|
||||
y: int;
|
||||
}
|
||||
|
||||
func AddPoints(a: Point, b: Point) -> Point {
|
||||
let result: Point = Point { x: a.x + b.x, y: a.y + b.y };
|
||||
return result;
|
||||
}
|
||||
|
||||
func Main() -> int {
|
||||
let p1: Point = Point { x: 10, y: 20 };
|
||||
let p2: Point = Point { x: 5, y: 15 };
|
||||
let sum: Point = AddPoints(p1, p2);
|
||||
|
||||
Std_Io_PrintLine("Point sum:");
|
||||
Std_Io_PrintLine("x = ");
|
||||
Std_Io_PrintInt(sum.x);
|
||||
Std_Io_PrintLine("");
|
||||
Std_Io_PrintLine("y = ");
|
||||
Std_Io_PrintInt(sum.y);
|
||||
Std_Io_PrintLine("");
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user