Chakradhar K
Chakradhar K

Reputation: 509

erlang method not called

Hi I have a sample erlang code as,

%%file_comment
-module(helloworld).
%% ====================================================================
%% API functions
%% ====================================================================
-export([add/2,subtract/2,hello/0,greet_and_math/1]).
%% ====================================================================
%% Internal functions
%% ====================================================================
add(A,B)->
A+B.

subtract(A,B)->
io:format("SUBTRACT!~n"),
A-B.

hello()->
io:format("Hello, world!~n").

greet_and_math(X) ->
hello(),
subtract(X,3),
add(X,2).

And when I run

helloworld:greet_and_math(15).

Output is:

Hello, world!

SUBTRACT!

17

My doubt is why A-B which is 15-2=13 not printed on console?

Upvotes: 0

Views: 114

Answers (2)

rvirding
rvirding

Reputation: 20916

@a.w. it is not that the last value is automatically printed, it the shell which prints the value of the call you make. So when you call greet_and_math(15) the function will:

  • Call hello() which prints the greeting. Its return value of ok from the call to io:format is ignored.
  • Call subtract(X, 3). Its return value of 12 is ignored.
  • Call add(X, 2). Its return value of 17 then becomes the return value of the whole function.

It is this return value of 17 which the shell prints out. So:

  • Everything returns a value, you cannot not return a value.
  • Returning a value and printing a value are very different things.

Upvotes: 1

paper_knight
paper_knight

Reputation: 623

That's because you never printed 15-2. The code you need would look like this:

%%file_comment
-module(helloworld).
%% ====================================================================
%% API functions
%% ====================================================================
-export([add/2,subtract/2,hello/0,greet_and_math/1]).
%% ====================================================================
%% Internal functions
%% ====================================================================
add(A,B)->
A+B.

subtract(A,B)->
io:format("SUBTRACT!~n"),
io:format("~p~n", [A-B]).   % this will make sure A-B is printed to console

hello()->
io:format("Hello, world!~n").

greet_and_math(X) ->
hello(),
subtract(X,3),
add(X,2).

That will give you:

Hello, world!
SUBTRACT!
12
17

If you wonder why 17 is printed, that's because it is the last expression. This one is always printed to console after executing code because it is actually what is returned by your code. Just execute io:format("hello~n"). on your console and you will see:

hello
ok

ok in this case is returned by io:format, and because it is the last expression, it will be printed.

io:format("hello~n"),
io:format("world~n").

will result in:

hello
world
ok

Only the last ok returned by the second io:format can be seen on console.
I hope you get the idea around how this works.

So in your case by typing:

4> A = helloworld:greet_and_math(15).
Hello, world!
SUBTRACT!
17
5> A.
17
6> 

You see how 17 is the value returned by greet_and_math(15) because it is the last expression? And thus it can be assigned to a variable.

Upvotes: 2

Related Questions