Skip to content
लिपि lipi

काम Language

Functions

Write reusable pieces of work with kaam and farkau.

kaam means “work”. A function is a piece of work you name once and call as often as you like. farkau (“send back”) returns a value from it.

kaam jod(a, b) {
  farkau a + b;
}

bhana(jod(2, 3));
bhana(jod(40, 2));
5
42

Parameters and calls

Inside the parentheses are the parameters. When you call the function, each argument is given to the matching parameter. The number of arguments must match:

kaam jod(a, b) {
  farkau a + b;
}

jod(1);
Galti: line 5 ma 'jod' lai 2 ota input chahinchha, 1 ota aayo

Functions without a name

kaam without a name makes a function value. Store it with rakha:

rakha dobar = kaam(x) {
  farkau x * 2;
};

bhana(dobar(21));
42

kaam jod(a, b) { … } is a short way of writing rakha jod = kaam(a, b) { … };.

Functions that call themselves

A function can call itself. This is called recursion, and it is how Lipi repeats things while it has no loops:

kaam factorial(n) {
  yadi (n < 2) {
    farkau 1;
  }
  farkau n * factorial(n - 1);
}

bhana(factorial(10));
3628800

If a function calls itself forever, Lipi stops it instead of crashing:

Galti: line 2 ma 'f' le aafailai dherai patak bolayo (recursion rokiyena)

Closures

A function remembers the variables around it when it was made. Here the inner kaam remembers x:

kaam gunak(x) {
  farkau kaam(y) {
    farkau x * y;
  };
}

rakha tebar = gunak(3);
bhana(tebar(7));
21

Functions as arguments

Because functions are values, you can pass one into another:

kaam duiPatak(f, x) {
  farkau f(f(x));
}

bhana(duiPatak(kaam(n) { farkau n + 10; }, 5));
25

Found a mistake on this page? Edit it on GitHub.