summaryrefslogtreecommitdiff
path: root/examples/all-examples.txt
diff options
context:
space:
mode:
Diffstat (limited to 'examples/all-examples.txt')
-rw-r--r--examples/all-examples.txt46
1 files changed, 46 insertions, 0 deletions
diff --git a/examples/all-examples.txt b/examples/all-examples.txt
index 966fc52..6e02967 100644
--- a/examples/all-examples.txt
+++ b/examples/all-examples.txt
@@ -202,6 +202,52 @@ ifnot pid {
say "I am the child process";
}
+# Test: function named arguments, explicit ret, and multiple return values
+
+# zero-arg function with explicit return
+func answer() {
+ ret 42;
+}
+assert 42 == say answer();
+
+# single-arg function — factorial with a while loop and ret
+func factorial(n) {
+ my result = 1;
+ while n > 1 {
+ result = result * n;
+ decr n;
+ }
+ ret result;
+}
+assert 120 == say factorial(5);
+
+# two-arg function
+func add(a, b) {
+ ret a + b;
+}
+assert 8 == say add(3, 5);
+
+# conditional return inside if
+func absval(n) {
+ if n < 0 { ret neg n; }
+ ret n;
+}
+assert 5 == say absval(5);
+assert 5 == say absval(neg 5);
+
+# multiple return values — both land on the caller's stack
+func minmax(a, b) {
+ if a < b { ret a, b; }
+ ret b, a;
+}
+say minmax(3, 7);
+
+# old-style zero-arg function without parens still works
+func greet {
+ say "hello";
+}
+greet;
+
#*
* Examples of how to use functions
*#