glox/tests/functions.lox

71 lines
929 B
Text
Raw Normal View History

2024-10-09 19:57:52 +03:00
2024-10-09 23:36:18 +03:00
print "native function";
2024-10-09 19:57:52 +03:00
2024-10-09 23:36:18 +03:00
print clock();
fun count(n) {
2024-10-14 22:53:26 +03:00
print n;
2024-10-09 23:36:18 +03:00
if (n > 1) count(n - 1);
print n;
}
count(10);
fun hi(name, surname) {
print "hello, " + name + " " + surname + "!";
}
hi("John", "Doe");
fun re(turn) {
2024-10-11 17:01:12 +03:00
print "before return";
2024-10-09 23:36:18 +03:00
return turn;
print "should not be printed";
}
2024-10-11 17:01:12 +03:00
print re("turn");
fun sum(start, end) {
if (start == end) return start;
return start + sum(start + 1, end);
}
print sum(1, 3);
fun fib(n) {
if (n <= 1) return n;
return fib(n - 2) + fib(n - 1);
}
for (var i = 1; i <= 10; i = i + 1) {
print fib(i);
2024-10-12 00:09:25 +03:00
}
fun makeCounter() {
var i = 0;
fun incr() {
i = i + 1;
print i;
}
return incr;
}
var counter = makeCounter();
counter();
2024-10-12 14:48:27 +03:00
counter();
fun thrice(fn) {
for (var i = 1; i <= 3; i = i + 1) {
fn(i);
}
}
thrice(fun (a) { print a; });
print fun () { return "hello, "; }() + "world";