Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return value of a function in Erlang

Tags:

erlang

What the following function will return? ok atom or Cmd?

function_test() ->
    Cmd = os:cmd("ls"),
    io:format("The result of ls is:~p~n", [Cmd]).

If it returns ok then how it should be rephrased to return Cmd while still using io:format?

like image 214
coffeMug Avatar asked Sep 13 '13 13:09

coffeMug


Video Answer


1 Answers

In Erlang the last expression in your function is returned, in your case that would be the result of io:format which is ok.

To return Cmd you can simply make it the last expression in your function:

function_test() ->
    Cmd = os:cmd("ls"),
    io:format("The result of ls is:~p~n", [Cmd]),
    Cmd.
like image 133
Stephan Dollberg Avatar answered Sep 28 '22 05:09

Stephan Dollberg