Erlang中函数的返回值 [英] Return value of a function in Erlang

查看:431
本文介绍了Erlang中函数的返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下函数将返回什么?是原子还是Cmd?

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]).

如果返回ok,那么在仍然使用io:format的同时,如何改写返回Cmd?

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

推荐答案

在Erlang中,返回函数中的最后一个表达式,在这种情况下,这是的结果io:format ,这是

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

要返回 Cmd 您可以简单地使其成为函数中的最后一个表达式:

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.

这篇关于Erlang中函数的返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆