Call a function: Difference between revisions
Content deleted Content added
→{{header|Erlang}}: Changed pre tag to lang + erlang sytax |
Added LFE example |
||
Line 315: | Line 315: | ||
var foo = [1, 2, 3]; |
var foo = [1, 2, 3]; |
||
mutate(foo) // foo is now [null, 2, 3], not 42</lang> |
mutate(foo) // foo is now [null, 2, 3], not 42</lang> |
||
=={{header|LFE}}== |
|||
'''Calling a function that requires no arguments:''' |
|||
In some module, define the following: |
|||
<lang lisp> |
|||
(defun my-func() |
|||
(: io format '"I get called with NOTHING!~n")) |
|||
</lang> |
|||
Then you use it like so (depending upon how you import it): |
|||
<lang lisp> |
|||
> (my-func) |
|||
I get called with NOTHING! |
|||
ok |
|||
</lang> |
|||
'''Calling a function with a fixed number of arguments:''' |
|||
In some module, define the following: |
|||
<lang lisp> |
|||
(defun my-func(a b) |
|||
(: io format '"I got called with ~p and ~p~n" (list a b))) |
|||
</lang> |
|||
Then you use it like so: |
|||
<lang lisp> |
|||
> (my-func '"bread" '"cheese") |
|||
I got called with "bread" and "cheese" |
|||
ok |
|||
</lang> |
|||
'''Calling a function with optional arguments or calling a function with a variable number of arguments:''' |
|||
* Arguments are fixed in LFE/Erlang functions. |
|||
* One can have a dictionary, record, or list be the function argument, and use that to achieve something like variable/optional (and named) arguments. |
|||
* One can define multiple functions so that it ''appears'' that one is calling a function with optional or a variable number of arguments: |
|||
<lang lisp> |
|||
(defmodule args |
|||
(export all)) |
|||
(defun my-func () |
|||
(my-func () () ())) |
|||
(defun my-func (a) |
|||
(my-func a () ())) |
|||
(defun my-func (a b) |
|||
(my-func a b ())) |
|||
(defun my-func (a b c) |
|||
(: io format '"~p ~p ~p~n" (list a b c))) |
|||
</lang> |
|||
Here is some example usage: |
|||
<lang lisp> |
|||
> (slurp '"args.lfe") |
|||
#(ok args) |
|||
> (my-func) |
|||
[] [] [] |
|||
ok |
|||
> (my-func '"apple") |
|||
"apple" [] [] |
|||
ok |
|||
> (my-func '"apple" '"banana") |
|||
"apple" "banana" [] |
|||
ok |
|||
> (my-func '"apple" '"banana" '"cranberry") |
|||
"apple" "banana" "cranberry" |
|||
ok |
|||
> (my-func '"apple" '"banana" '"cranberry" '"bad arg") |
|||
exception error: #(unbound_func #(my-func 4)) |
|||
</lang> |
|||
'''Calling a function with named arguments:''' |
|||
* LFE/Erlang doesn't support named arguments, per se. |
|||
* However, by using atoms in function argument patterns (a fairly common pattern), one can achieve similar effects. |
|||
* One may also use records or dicts as arguments to achieve similar effects. |
|||
'''Using a function in statement context:''' |
|||
<lang lisp> |
|||
... |
|||
(cond ((== count limit) (hit-limit-func arg-1 arg-2)) |
|||
((/= count limit) (keep-going-func count))) |
|||
... |
|||
</lang> |
|||
'''Using a function in first-class context within an expression:''' |
|||
From the LFE REPL: |
|||
<lang lisp> |
|||
> (>= 0.5 (: math sin 0.5)) |
|||
true |
|||
</lang> |
|||
'''Obtaining the return value of a function:''' |
|||
There are many, many ways to assign function outputs to variables in LFE. One fairly standard way is with the <code>(let ...)</code> form: |
|||
<lang lisp> |
|||
(let ((x (: math sin 0.5))) |
|||
...) |
|||
</lang> |
|||
'''Distinguishing built-in functions and user-defined functions:''' |
|||
* There is no distinction made in LFE/Erlang between functions that are built-in and those that are not. |
|||
* "Built-in" for LFE/Erlang usually can be figured out: if a function has the module name <code>erlang</code>, e.g., <code>(: erlang list_to_integer ... )</cod>, then it's built-in. |
|||
* Most of the functions that come with LFE/Erlang are not even in the <code>erlang</code> module, but exist in other modules (e.g., <code>io</code>, <code>math</code>, etc.) and in OTP. |
|||
* One uses user/third-party modules in exactly the same way as one uses built-ins and modules that come with the Erlang distribution. |
|||
'''Distinguishing subroutines and functions:''' |
|||
* One commonly made distinction between functions and subroutines is that functions return a value (or reference, etc.) and subroutines do not. |
|||
* By this definition, LFE/Erlang does not support the concept of a subroutine; all functions return something. |
|||
'''Stating whether arguments are passed by value or by reference:''' |
|||
* Arguments and returns values are passed by reference in LFE/Erlang. |
|||
'''Is partial application possible?''' |
|||
* Not explicitly. |
|||
* However, one can use <code>lambda</code>s to achieve the same effect. |
|||
=={{header|Lua}}== |
=={{header|Lua}}== |