Functions & OOP

Functions

Functions in BanglaCode are defined using the kaj keyword (meaning "work" or "task"). Functions are first-class values and support closures.

New Utility Methods

BanglaCode now includes additional JavaScript-style helpers for arrays, strings, number parsing, and URI encoding.

1// Array search helpers
2dhoro first = khojo_prothom([1, 3, 8, 10], kaj(x) { ferao x > 5; }); // 8
3dhoro idx = khojo_index([1, 3, 8, 10], kaj(x) { ferao x > 5; }); // 2
4dhoro last = khojo_shesh([1, 3, 8, 10], kaj(x) { ferao x % 2 == 0; }); // 10
5
6// String helpers
7ache_text("banglacode", "code"); // sotti
8shuru_diye("banglacode", "bang"); // sotti
9shesh_diye("banglacode", "code"); // sotti
10baro("ha", 3); // "hahaha"
11text_at("bangla", -1); // "a"
12
13// Number + URI helpers
14purno_sonkhya("42"); // 42
15doshomik_sonkhya("3.14abc"); // 3.14
16sonkhya_na("abc"); // sotti
17uri_ongsho_encode("hello world"); // "hello%20world"
18
19// Date + Regex helpers
20dhoro ts = tarikh_ekhon();
21tarikh_format(ts, "2006-01-02");
22regex_test("[a-z]+", "bangla"); // sotti
23regex_search("la", "bangla"); // 4
24
25// Object helpers
26nijer_ache({a: 1}, "a"); // sotti
27jora_theke([["x", 10], ["y", 20]]); // {x: 10, y: 20}
28ekoi_ki(1, 1); // sotti
29
30// Timers
31dhoro id = setInterval(kaj() { dekho("tick"); }, 1000);
32clearInterval(id);

Defining Functions

Use the kaj keyword followed by the function name and parameters:

1// Basic function definition
2kaj greet() {
3 dekho("Namaskar!");
4}
5
6// Call the function
7greet(); // Output: Namaskar!

Functions with Parameters

1kaj greetPerson(naam) {
2 dekho("Namaskar,", naam);
3}
4
5greetPerson("Rahim"); // Namaskar, Rahim
6greetPerson("Karim"); // Namaskar, Karim
7
8// Multiple parameters
9kaj add(a, b) {
10 dekho(a, "+", b, "=", a + b);
11}
12
13add(5, 3); // 5 + 3 = 8

Return Values

Use the ferao keyword (meaning "return") to return a value:

1kaj add(a, b) {
2 ferao a + b;
3}
4
5dhoro sum = add(10, 20);
6dekho(sum); // 30
7
8// Use return value directly
9dekho(add(5, 3) * 2); // 16
10
11// Function without return returns null
12kaj sayHello() {
13 dekho("Hello!");
14}
15
16dhoro result = sayHello();
17dekho(result == khali); // sotti

Early Return

1kaj getGrade(score) {
2 jodi (score >= 90) {
3 ferao "A";
4 }
5 jodi (score >= 80) {
6 ferao "B";
7 }
8 jodi (score >= 70) {
9 ferao "C";
10 }
11 jodi (score >= 60) {
12 ferao "D";
13 }
14 ferao "F";
15}
16
17dekho(getGrade(85)); // B
18dekho(getGrade(55)); // F

Function Expressions

Functions can be assigned to variables (anonymous functions):

1// Anonymous function assigned to variable
2dhoro multiply = kaj(x, y) {
3 ferao x * y;
4};
5
6dekho(multiply(4, 5)); // 20
7
8// Functions as array elements
9dhoro operations = [
10 kaj(a, b) { ferao a + b; },
11 kaj(a, b) { ferao a - b; },
12 kaj(a, b) { ferao a * b; },
13 kaj(a, b) { ferao a / b; }
14];
15
16dekho(operations[0](10, 5)); // 15 (addition)
17dekho(operations[2](10, 5)); // 50 (multiplication)

Higher-Order Functions

Functions can take other functions as arguments or return functions:

1// Function that takes a function as argument
2kaj applyOperation(a, b, operation) {
3 ferao operation(a, b);
4}
5
6kaj add(x, y) { ferao x + y; }
7kaj multiply(x, y) { ferao x * y; }
8
9dekho(applyOperation(5, 3, add)); // 8
10dekho(applyOperation(5, 3, multiply)); // 15
11
12// Function that returns a function
13kaj makeMultiplier(factor) {
14 ferao kaj(n) {
15 ferao n * factor;
16 };
17}
18
19dhoro double = makeMultiplier(2);
20dhoro triple = makeMultiplier(3);
21
22dekho(double(5)); // 10
23dekho(triple(5)); // 15

Closures

Functions capture variables from their surrounding scope:

1kaj makeCounter() {
2 dhoro count = 0;
3
4 ferao kaj() {
5 count = count + 1;
6 ferao count;
7 };
8}
9
10dhoro counter1 = makeCounter();
11dhoro counter2 = makeCounter();
12
13dekho(counter1()); // 1
14dekho(counter1()); // 2
15dekho(counter1()); // 3
16
17dekho(counter2()); // 1 (separate counter)
18dekho(counter2()); // 2

Closure with State

1kaj createBankAccount(initialBalance) {
2 dhoro balance = initialBalance;
3
4 ferao {
5 deposit: kaj(amount) {
6 balance = balance + amount;
7 dekho("Deposited:", amount, "- Balance:", balance);
8 },
9 withdraw: kaj(amount) {
10 jodi (amount > balance) {
11 dekho("Insufficient funds!");
12 ferao mittha;
13 }
14 balance = balance - amount;
15 dekho("Withdrew:", amount, "- Balance:", balance);
16 ferao sotti;
17 },
18 getBalance: kaj() {
19 ferao balance;
20 }
21 };
22}
23
24dhoro account = createBankAccount(1000);
25account.deposit(500); // Deposited: 500 - Balance: 1500
26account.withdraw(200); // Withdrew: 200 - Balance: 1300
27dekho(account.getBalance()); // 1300

Recursion

Functions can call themselves:

1// Factorial
2kaj factorial(n) {
3 jodi (n <= 1) {
4 ferao 1;
5 }
6 ferao n * factorial(n - 1);
7}
8
9dekho(factorial(5)); // 120
10
11// Fibonacci
12kaj fibonacci(n) {
13 jodi (n <= 1) {
14 ferao n;
15 }
16 ferao fibonacci(n - 1) + fibonacci(n - 2);
17}
18
19dekho(fibonacci(10)); // 55
20
21// Sum of array using recursion
22kaj sumArray(arr, index) {
23 jodi (index >= dorghyo(arr)) {
24 ferao 0;
25 }
26 ferao arr[index] + sumArray(arr, index + 1);
27}
28
29dekho(sumArray([1, 2, 3, 4, 5], 0)); // 15

Default-Like Behavior

BanglaCode doesn't have default parameters, but you can simulate them:

1kaj greet(naam, greeting) {
2 // Check if parameter is provided
3 jodi (greeting == khali) {
4 greeting = "Namaskar";
5 }
6 dekho(greeting + ",", naam);
7}
8
9greet("Rahim", "Hello"); // Hello, Rahim
10greet("Karim", khali); // Namaskar, Karim

Rest Parameters

Use ... (spread/rest operator) to collect remaining arguments into an array:

1// Variadic function with rest parameter
2kaj sum(...numbers) {
3 dhoro total = 0;
4 ghuriye (dhoro i = 0; i < dorghyo(numbers); i = i + 1) {
5 total = total + numbers[i];
6 }
7 ferao total;
8}
9
10dekho(sum(1, 2, 3)); // 6
11dekho(sum(1, 2, 3, 4, 5)); // 15
12dekho(sum()); // 0
13
14// Mixed regular parameters with rest
15kaj greetAll(greeting, ...names) {
16 ghuriye (dhoro i = 0; i < dorghyo(names); i = i + 1) {
17 dekho(greeting, names[i]);
18 }
19}
20
21greetAll("Hello", "Alice", "Bob", "Charlie");
22// Hello Alice
23// Hello Bob
24// Hello Charlie

Spread Operator

Use ... to expand arrays in function calls or array literals:

1// Spread in function calls
2kaj sum(...numbers) {
3 dhoro total = 0;
4 ghuriye (dhoro i = 0; i < dorghyo(numbers); i = i + 1) {
5 total = total + numbers[i];
6 }
7 ferao total;
8}
9
10dhoro nums = [1, 2, 3, 4, 5];
11dekho(sum(...nums)); // 15
12
13// Combine with regular arguments
14dekho(sum(10, ...nums, 20)); // 10 + 1 + 2 + 3 + 4 + 5 + 20 = 45
15
16// Spread in array literals
17dhoro arr1 = [1, 2];
18dhoro arr2 = [3, 4];
19dhoro combined = [...arr1, ...arr2]; // [1, 2, 3, 4]
20dhoro withExtra = [0, ...arr1, 99]; // [0, 1, 2, 99]
21
22// Clone an array
23dhoro original = [1, 2, 3];
24dhoro copy = [...original];
25
26// Spread with dekho
27dhoro items = ["apple", "banana", "cherry"];
28dekho(...items); // apple banana cherry

Callback Pattern

1// Process array with callback
2kaj forEach(arr, callback) {
3 ghuriye (dhoro i = 0; i < dorghyo(arr); i = i + 1) {
4 callback(arr[i], i);
5 }
6}
7
8dhoro numbers = [1, 2, 3, 4, 5];
9
10forEach(numbers, kaj(value, index) {
11 dekho("Index", index, ":", value);
12});
13
14// Custom map function
15kaj map(arr, transform) {
16 dhoro result = [];
17 ghuriye (dhoro i = 0; i < dorghyo(arr); i = i + 1) {
18 dhokao(result, transform(arr[i]));
19 }
20 ferao result;
21}
22
23dhoro doubled = map([1, 2, 3], kaj(n) { ferao n * 2; });
24dekho(doubled); // [2, 4, 6]
25
26// Custom filter function
27kaj filter(arr, predicate) {
28 dhoro result = [];
29 ghuriye (dhoro i = 0; i < dorghyo(arr); i = i + 1) {
30 jodi (predicate(arr[i])) {
31 dhokao(result, arr[i]);
32 }
33 }
34 ferao result;
35}
36
37dhoro evens = filter([1, 2, 3, 4, 5, 6], kaj(n) { ferao n % 2 == 0; });
38dekho(evens); // [2, 4, 6]

Practical Examples

Memoization

1// Memoized Fibonacci
2kaj createMemoizedFib() {
3 dhoro cache = {};
4
5 ferao kaj(n) {
6 jodi (n <= 1) {
7 ferao n;
8 }
9
10 dhoro key = lipi(n);
11 jodi (cache[key] != khali) {
12 ferao cache[key];
13 }
14
15 dhoro result = ei(n - 1) + ei(n - 2);
16 cache[key] = result;
17 ferao result;
18 };
19}
20
21dhoro fib = createMemoizedFib();
22dekho(fib(40)); // Much faster with memoization!

Compose Functions

1kaj compose(f, g) {
2 ferao kaj(x) {
3 ferao f(g(x));
4 };
5}
6
7kaj addOne(n) { ferao n + 1; }
8kaj double(n) { ferao n * 2; }
9
10dhoro addOneThenDouble = compose(double, addOne);
11dekho(addOneThenDouble(5)); // 12 ((5+1)*2)
12
13dhoro doubleThenAddOne = compose(addOne, double);
14dekho(doubleThenAddOne(5)); // 11 ((5*2)+1)