|
print("=== Functions Demo ===");
|
|
print("");
|
|
|
|
print("--- Methods in Classes ---");
|
|
class Calculator {
|
|
Calculator(this) {
|
|
}
|
|
|
|
add(this, a, b) {
|
|
return a + b;
|
|
}
|
|
|
|
subtract(this, a, b) {
|
|
return a - b;
|
|
}
|
|
|
|
multiply(this, a, b) {
|
|
return a * b;
|
|
}
|
|
|
|
divide(this, a, b) {
|
|
if (b == 0) {
|
|
return 0;
|
|
}
|
|
return a / b;
|
|
}
|
|
}
|
|
|
|
calc = new Calculator();
|
|
print("add(5, 3) =", calc.add(5, 3));
|
|
print("subtract(10, 4) =", calc.subtract(10, 4));
|
|
print("multiply(6, 7) =", calc.multiply(6, 7));
|
|
print("divide(20, 4) =", calc.divide(20, 4));
|
|
|
|
print("");
|
|
print("--- Required Parameters ---");
|
|
class Greeter {
|
|
Greeter(this) {
|
|
}
|
|
|
|
greet(this, name) {
|
|
print("Hello,", name + "!");
|
|
}
|
|
}
|
|
|
|
greeter = new Greeter();
|
|
greeter.greet("Alice");
|
|
greeter.greet("Bob");
|
|
|
|
print("");
|
|
print("--- Optional Parameters with Defaults ---");
|
|
class ConfigurableGreeter {
|
|
ConfigurableGreeter(this) {
|
|
}
|
|
|
|
greet(this, name, greeting = "Hello", punctuation = "!") {
|
|
print(greeting + ",", name + punctuation);
|
|
}
|
|
}
|
|
|
|
cg = new ConfigurableGreeter();
|
|
cg.greet("Alice");
|
|
cg.greet("Bob", "Hi");
|
|
cg.greet("Charlie", "Hey", "?");
|
|
|
|
print("");
|
|
print("--- Null Default Values ---");
|
|
class Formatter {
|
|
Formatter(this) {
|
|
}
|
|
|
|
format(this, value, prefix = null, suffix = null) {
|
|
result = "";
|
|
if (prefix) {
|
|
result += prefix;
|
|
}
|
|
result += str(value);
|
|
if (suffix) {
|
|
result += suffix;
|
|
}
|
|
return result;
|
|
}
|
|
}
|
|
|
|
fmt = new Formatter();
|
|
print("No prefix/suffix:", fmt.format(42));
|
|
print("With prefix:", fmt.format(42, "$"));
|
|
print("With both:", fmt.format(42, "$", ".00"));
|
|
|
|
print("");
|
|
print("--- Variable Arguments (*args) ---");
|
|
class VarArgs {
|
|
VarArgs(this) {
|
|
}
|
|
|
|
printAll(this, *args) {
|
|
print("Received", len(args), "arguments:");
|
|
for (i = 0; i < len(args); i++) {
|
|
print(" arg[" + str(i) + "] =", args[i]);
|
|
}
|
|
}
|
|
|
|
sum(this, *numbers) {
|
|
total = 0;
|
|
for (i = 0; i < len(numbers); i++) {
|
|
total += numbers[i];
|
|
}
|
|
return total;
|
|
}
|
|
|
|
first(this, required, *rest) {
|
|
print("Required:", required);
|
|
print("Rest:", len(rest), "items");
|
|
}
|
|
}
|
|
|
|
va = new VarArgs();
|
|
va.printAll(1, 2, 3);
|
|
va.printAll("a", "b", "c", "d", "e");
|
|
|
|
print("sum(1,2,3,4,5) =", va.sum(1, 2, 3, 4, 5));
|
|
|
|
va.first("mandatory", "extra1", "extra2");
|
|
|
|
print("");
|
|
print("--- Keyword Arguments (**kwargs) ---");
|
|
class KwArgs {
|
|
KwArgs(this) {
|
|
}
|
|
|
|
configure(this, **options) {
|
|
print("Configuration options:");
|
|
print(" (kwargs captured as dict)");
|
|
}
|
|
}
|
|
|
|
kw = new KwArgs();
|
|
kw.configure();
|
|
|
|
print("");
|
|
print("--- Return Values ---");
|
|
class Math {
|
|
Math(this) {
|
|
}
|
|
|
|
square(this, n) {
|
|
return n * n;
|
|
}
|
|
|
|
factorial(this, n) {
|
|
if (n <= 1) {
|
|
return 1;
|
|
}
|
|
return n * this.factorial(n - 1);
|
|
}
|
|
|
|
abs(this, n) {
|
|
if (n < 0) {
|
|
return 0 - n;
|
|
}
|
|
return n;
|
|
}
|
|
|
|
max(this, a, b) {
|
|
if (a > b) {
|
|
return a;
|
|
}
|
|
return b;
|
|
}
|
|
|
|
min(this, a, b) {
|
|
if (a < b) {
|
|
return a;
|
|
}
|
|
return b;
|
|
}
|
|
}
|
|
|
|
math = new Math();
|
|
print("square(7) =", math.square(7));
|
|
print("factorial(5) =", math.factorial(5));
|
|
print("abs(-42) =", math.abs(-42));
|
|
print("max(10, 20) =", math.max(10, 20));
|
|
print("min(10, 20) =", math.min(10, 20));
|
|
|
|
print("");
|
|
print("--- Recursion ---");
|
|
class Recursive {
|
|
Recursive(this) {
|
|
}
|
|
|
|
fibonacci(this, n) {
|
|
if (n <= 1) {
|
|
return n;
|
|
}
|
|
return this.fibonacci(n - 1) + this.fibonacci(n - 2);
|
|
}
|
|
|
|
gcd(this, a, b) {
|
|
if (b == 0) {
|
|
return a;
|
|
}
|
|
return this.gcd(b, a % b);
|
|
}
|
|
|
|
power(this, base, exp) {
|
|
if (exp == 0) {
|
|
return 1;
|
|
}
|
|
return base * this.power(base, exp - 1);
|
|
}
|
|
}
|
|
|
|
rec = new Recursive();
|
|
print("fibonacci(10) =", rec.fibonacci(10));
|
|
print("gcd(48, 18) =", rec.gcd(48, 18));
|
|
print("power(2, 8) =", rec.power(2, 8));
|
|
|
|
print("");
|
|
print("--- Method Chaining ---");
|
|
class StringBuilder {
|
|
buffer = "";
|
|
|
|
StringBuilder(this) {
|
|
}
|
|
|
|
append(this, text) {
|
|
this.buffer += text;
|
|
return this;
|
|
}
|
|
|
|
appendLine(this, text) {
|
|
this.buffer += text + "\n";
|
|
return this;
|
|
}
|
|
|
|
toString(this) {
|
|
return this.buffer;
|
|
}
|
|
|
|
clear(this) {
|
|
this.buffer = "";
|
|
return this;
|
|
}
|
|
}
|
|
|
|
sb = new StringBuilder();
|
|
sb.append("Hello").append(" ").append("World").append("!");
|
|
print("Result:", sb.toString());
|
|
|
|
sb.clear();
|
|
sb.appendLine("Line 1").appendLine("Line 2").append("Line 3");
|
|
print("Multi-line:");
|
|
print(sb.toString());
|