Functions.
This commit is contained in:
parent
3f21434cd1
commit
0a49a463e2
8 changed files with 312 additions and 29 deletions
|
|
@ -8,7 +8,7 @@ print("=== Expressions test start ===")
|
|||
number a = 2
|
||||
number b = 3
|
||||
number n = 10
|
||||
string s = "Hello, world!"
|
||||
string s = "Have, fun!"
|
||||
boolean flag = true
|
||||
|
||||
// Arithmetic and precedence
|
||||
|
|
|
|||
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 ===")
|
||||
|
|
@ -1,7 +1,7 @@
|
|||
#!/usr/bin/env fun
|
||||
|
||||
/*
|
||||
* Have fun! in Fun
|
||||
* Have fun in Fun
|
||||
*/
|
||||
|
||||
print("Hello, fun!")
|
||||
print("Have, fun!")
|
||||
|
|
|
|||
10
examples/have_fun_function.fun
Executable file
10
examples/have_fun_function.fun
Executable file
|
|
@ -0,0 +1,10 @@
|
|||
#!/usr/bin/env fun
|
||||
|
||||
/*
|
||||
* Have fun in Fun
|
||||
*/
|
||||
|
||||
fun have_fun()
|
||||
print("Have, fun!")
|
||||
|
||||
have_fun()
|
||||
Loading…
Add table
Add a link
Reference in a new issue