Return value of a function in Erlang

2019-02-22 20:11发布

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?

标签: erlang
1条回答
萌系小妹纸
2楼-- · 2019-02-22 21:01

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.
查看更多
登录 后发表回答