Functions.
This commit is contained in:
parent
3f21434cd1
commit
0a49a463e2
8 changed files with 312 additions and 29 deletions
40
examples/functions_test.fun
Executable file
40
examples/functions_test.fun
Executable file
|
|
@ -0,0 +1,40 @@
|
|||
#!/usr/bin/env fun
|
||||
|
||||
// Functions test: definitions, calls, parameters, locals, returns, and if/else.
|
||||
|
||||
print("=== Functions test start ===")
|
||||
|
||||
fun add(a, b)
|
||||
return a + b
|
||||
|
||||
fun abs(x)
|
||||
if (x < 0)
|
||||
return -x
|
||||
else
|
||||
return x
|
||||
|
||||
fun sum3(a, b, c)
|
||||
// local typed declaration
|
||||
number t = a + b
|
||||
return t + c
|
||||
|
||||
fun id(x)
|
||||
return x
|
||||
|
||||
// Calls in expressions
|
||||
print(add(2, 3)) // expect 5
|
||||
print(add(10, -3)) // expect 7
|
||||
print(abs(-7)) // expect 7
|
||||
print(abs(4)) // expect 4
|
||||
print(sum3(1, 2, 3)) // expect 6
|
||||
print(add(add(1, 2), 3)) // expect 6
|
||||
print(id(42)) // expect 42
|
||||
|
||||
// Call as statement (result discarded)
|
||||
add(100, 23)
|
||||
|
||||
// Globals interacting with functions
|
||||
number n = 5
|
||||
print(add(n, 10)) // expect 15
|
||||
|
||||
print("=== Functions test end ===")
|
||||
Loading…
Add table
Add a link
Reference in a new issue