See also Mythryl tutorial, more on functions
Table of Contents [hide]
Intro ∞
Functions allow for reusable code and are are extremely important for reducing your code's complexity. Use them often!
The basics ∞
fun greet() = printf "Hello, world!\n"; greet(); greet(); # => # Hello, world! # Hello, world!
Passing a string to a function ∞
fun greet( string ) = printf "%s" string; greet( "Hello, world!\n" ); greet( "Fine day, isn't it!\n" ); # => # Hello, world! # Fine day, isn't it!
You can use printf
in the usual manners:
fun greet( string ) = printf "Hello, %s!\n" string; greet( "world" ); # => # Hello, world!
Functions returning data ∞
fun memory() = "I can never remember this long phrase!\n"; printf "%s" ( memory() ); # => # I can never remember this long phrase!
Remember that when "doing stuff" for a printf
you must use parentheses. So memory()
has parentheses surrounding it. The extra spaces improve readability. Here it is again:
fun memory() = "I can never remember this long phrase!\n"; printf "%s" ( memory() ); # => # I can never remember this long phrase!
You can also name a value with a function's result.
fun memory() = "I can never remember this long phrase!\n"; a = memory(); printf "%s" a; # => # I can never remember this long phrase!
Last updated 2022-08-11 at 09:08:23