function keyword
function introduces a function. Functions are first-class values — you can store them in variables, pass them as arguments, and return them from other functions. The Bangla form ফাংশন is equivalent.
Declaration
function add(a, b) {
return a + b;
}
print(add(2, 3)); // 5
Expression (anonymous function)
var double = function (n) { return n * 2; };
print(double(7)); // 14
You'll see this form a lot when passing callbacks or .next / .fail handlers:
import "request" as r;
r.get("https://example.com/data")
.next(function (resp) { print(resp.body); })
.fail(function (e) { print("err:", e); });
Fixed arity (with optional defaults)
Functions take a fixed number of arguments. There are no overloads, no rest parameters, no arguments object. Parameters at the end of the list may have default values; those become optional at the call site.
function greet(name, greeting = "Hi") {
return greeting + ", " + name;
}
greet("Alice"); // "Hi, Alice"
greet("Alice", "Hello"); // "Hello, Alice"
// greet(); // error: expects 1 to 2 arguments, got 0
// greet("Alice", "x", "y"); // error: expects 1 to 2 arguments, got 3
Default rules:
-
Defaults must come at the end of the parameter list.
function bad(a = 1, b) { ... }is a parse error. -
Default expressions are evaluated lazily, on each call, in the function's local environment — so a later default can reference earlier params:
function range(start, end = start + 10) { return [start, end]; } range(0); // [0, 10] range(100); // [100, 110]
For genuinely different shapes (different return type, different intent), still split into separately-named functions:
function greet(name) { return "Hi, " + name; }
function greet_formal(title, name) { return title + " " + name; }
Closures
Inner functions capture outer variables by reference:
function make_counter() {
var n = 0;
return function () {
n = n + 1;
return n;
};
}
var c = make_counter();
print(c()); // 1
print(c()); // 2
Return value
return <expr>; returns the expression's value. A bare return; (or falling off the end) returns null.
See return for details.
Bangla form
ফাংশন যোগ(a, b) {
ফেরত a + b;
}
print(যোগ(2, 3)); // 5