9

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?

1
  • 1
    Please be aware that printing a value and returning a value are two completely different things. Here you print the value of Cmd but you return the value returned by io:format which is ok.
    – rvirding
    Commented Sep 15, 2013 at 7:27

1 Answer 1

16

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.