So I've wrote this program that takes 1 String Argument and print it as following:
-module(sayhi).
-export([sayhi/1]).
sayhi({greeting, Greeting}) -> io:fwrite(Greeting).
then I call the function as following (from terminal).
c(sayhi).
ok
sayhi:sayhi({greeting, "HELLO!\n"}).
HELLO!
ok
Until now everything is good.
But When I try to implement 2 arguments, I get error: *** argument 1: wrong number of arguments
Here is my Code:
-module(sayhi).
-export([sayhi/2]).
sayhi({greeting, Greeting}, {name, Name}) -> io:fwrite(Greeting, Name).
When I call my function:
sayhi:sayhi({greeting, "Hola "}, {name, "Sam"}).
The program Runs successfully but does not give me the output needed. does the problem come from my statement of calling the function?
And what if I had 3, or even 10 arguments?
Erlang has a comprehensive documentation on all of its built-in functions, such as io:fwrite/1, io:fwrite/2, io:fwrite/3
(can be found here).
If you want to use the function with 1 argument, then you can call it like this:
io:fwrite(Greeting ++ Name). %% '++' is nothing but appending strings
io:fwrite(Greeting ++ Name ++ NextParam1 ++ NextParam2). %% You can then expand it as needed
When using 2 arguments, i.e. write(Format, Data)
, then:
io:fwrite("~s~s", [Greeting, Name]).
io:fwrite("~s~s~s~s", [Greeting, Name, NextParam1, NextParam2]). %% You can also expand as needed