Skip to main content
Skip to content

Functions keyword

karya — function

karya defines a reusable function with a name, parameters and a body. Call it by writing its name followed by brackets.

Meaning: "task, work"

How karya works

karya naam(a, b) { ... } declares a function. Parameters are local variables filled from the call; missing arguments arrive as khali.

AltFLang runs your program from top to bottom, so define a function before the line that calls it. Functions remember the variables around them (closures) and may call themselves (recursion), with a depth limit that turns runaway recursion into an error.

Functions are values: rakho dugna = karya (x) { lautao x * 2; }; stores a nameless function in a variable, and functions can be passed to built-ins such as har and chuno.

Syntax

syntax
karya naam(param1, param2) {
  // body
  lautao result;
}

Runnable example

Expected output

Press Run to try it

Kamra: 20 sq mHall: 48 sq m

Same idea in JavaScript and Python

JavaScript
function area(l, b) {
  return l * b;
}
console.log("Kamra:", area(4, 5), "sq m");
Python
def area(l, b):
    return l * b

print("Kamra:", area(4, 5), "sq m")

Common mistakes

Calling a function before defining it

The declaration has to run before the call. Move the karya block above the line that uses it.

Galat
bol(dugna(4));
karya dugna(x) { lautao x * 2; }
Sahi
karya dugna(x) { lautao x * 2; }
bol(dugna(4));

Printing instead of returning

A function that only calls bol gives back khali, so its result cannot be used in a calculation. Use lautao to hand the value back.

Galat
karya teen_guna(x) { bol(x * 3); }
rakho y = teen_guna(2) + 1;
Sahi
karya teen_guna(x) { lautao x * 3; }
bol(teen_guna(2) + 1);

karya: frequently asked questions

How do I return a value from a karya function?

Use lautao followed by the value. lautao ends the function immediately; without it the function returns khali.

Can AltFLang functions be recursive?

Yes. A function may call itself, as long as it has a base case that stops. There is a call-depth limit, so infinite recursion ends with a clear error.