Return value of a function in Erlang

自古美人都是妖i 提交于 2020-01-11 08:33:09

问题


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:


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.


来源:https://stackoverflow.com/questions/18787867/return-value-of-a-function-in-erlang

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!