You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

functions.l2 1.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. # Functions return their last expression
  2. func := {
  3. "hello"
  4. }
  5. # Call a function without arguments using ()
  6. print func()
  7. # A function, returning a function, with a closure so that it can access
  8. # variables from outside
  9. str := "Hello World"
  10. func := {
  11. {
  12. str
  13. }
  14. }
  15. # func() returns a function, which can be called again with an extra ()
  16. print func()()
  17. func := {
  18. {
  19. # $ is a variable containing the arguments passed to the function,
  20. # so this nested function sets the global 'str' variable to whatever
  21. # the function is passed
  22. str = $.0
  23. }
  24. }
  25. # func() calls the 'func' function with no arguemnts, and the nested function
  26. # is returned. That returned function is then called with 10 as a parameter.
  27. func() 10
  28. # Since the nested function modified it, 'str' should now be the number 10
  29. print str
  30. func := {
  31. # Variables don't have to be global; this function returns a function which
  32. # has a reference to the 'retval' variable
  33. retval := "what's up"
  34. {retval}
  35. }
  36. print func()()
  37. # Lastly, just print a function literal
  38. print {0}