can i alter a parameter within a function

Ulf Wiger ulf@REDACTED
Wed Aug 10 00:59:33 CEST 2005


Den 2005-08-09 22:30:49 skrev MEENA SELVAM <meena_selvam@REDACTED>:

>
> If I have a function which takes a parameter A as in
> the call f1("found") is it OK if I change the value of
> the parameter A within the function as below?
>
> f1(A) when A /= "not_found" ->
>    VId =1,
>    if  s_key(VId) /= "not_found" ->
>           VId = VId +1,
>           f1(A),
>    true->
>         A = "not_found",
>         Vid -1
>    end

No, you cannot alter the value of a variable once it
has been bound. If you compile and run the code above,
you will (after fixing the mis-spelled Vid on the 8th
line) notice that it exits with a 'badmatch'. A is bound
to a value when the function is called, and will retain
that value until it's no longer referenced.

Besides, on the 7th line, changing A (even if you could)
would have no effect, since it is not referenced after
that. Remember that all variables in Erlang have local
scope.

See chapter "6.3 Variables" in the Erlang Reference Manual
(http://erlang.se/doc/doc-5.4.8/doc/reference_manual/expressions.html#6.3)

/Uffe
-- 
Ulf Wiger



More information about the erlang-questions mailing list