Skip to content

Latest commit

 

History

History
231 lines (151 loc) · 7.38 KB

File metadata and controls

231 lines (151 loc) · 7.38 KB

Functions

Mix.install([
  {:kino, "~> 0.12.0"}
])

import IEx.Helpers

Function calls

In the context of programming, a function is a named sequence of expressions that performs a computation. When you define a function, you specify the name and the sequence of expressions. Later, you can invoke the function by name. We have already seen one example of a function invocation:

iex(1)> is_integer(3)
true

The name of the function is is_integer. The expression in parentheses is called the argument of the function. The argument is a value or variable (term) that we are passing into the function as input to the function. The result, for the is_integer function, is a boolean indicating whether the argument is an integer or not.

It is common to say that a function "takes" an argument and "returns" a result. The result is called the return value.

Built-in functions

In Elixir, there are also built-in functions that can be used without providing a function definition. These functions are part of the Elixir standard library and are designed to solve common problems.

The List.first/1 and List.last/1 functions give us the first and last value in a list, respectively:

iex(1)> List.first([1, 2, 3, 4, 5])
1
iex(2)> List.last([1, 2, 3, 4, 5])
5

The List.first/1 function tells us the "first element" in the List (which turn out to be the integer "1") and the List.last/1 shows the "last element" in the List (which turn out to be the integer "2")

Another common built-in function is the String.length/1 function, which can be used to find the length of a string:

iex(5)> String.length("Hello world")
11

The String.length/1 function returns the number of characters in the string (which is 11 in this case).

Type conversion functions

Elixir also provides built-in functions that convert values from one type to another. The String.to_integer/1 function takes string representation and converts it to an integer, if it can, or complains otherwise:

iex(10)> String.to_integer("32")
32
iex(11)> String.to_integer("Hello")
** (ArgumentError) errors were found at the given arguments:

  * 1st argument: not a textual representation of an integer

    erlang.erl:4719: :erlang.binary_to_integer("Hello")
    iex:11: (file)
iex(12)> String.to_integer("3.99")
** (ArgumentError) errors were found at the given arguments:

  * 1st argument: not a textual representation of an integer

    erlang.erl:4719: :erlang.binary_to_integer("3.99")
    iex:12: (file)    

The String.to_float/1 function takes string representation and converts it to an integer, if it can, or complains otherwise:

iex(1)> String.to_float("3.999")
3.999
iex(2)> String.to_float("Hello")
** (ArgumentError) errors were found at the given arguments:

  * 1st argument: not a textual representation of a float

    :erlang.binary_to_float("Hello")
    iex:2: (file)
iex(2)> String.to_float("3")
** (ArgumentError) errors were found at the given arguments:

  * 1st argument: not a textual representation of a float

    :erlang.binary_to_float("3")
    iex:2: (file)

Finally Integer.to_string/1 takes an integer as its argument and convert to a String, in the same way Float.to_string/1 takes a float as its argument and convert to a String. In both cases if it can't it will complains.

iex(2)> Integer.to_string(3)
"3"
iex(3)> Integer.to_string(3.3)
** (ArgumentError) errors were found at the given arguments:

  * 1st argument: not an integer

    :erlang.integer_to_binary(3.3)
    iex:3: (file)
iex(3)> Float.to_string(3.3)
"3.3"
iex(4)> Float.to_string(3)
** (FunctionClauseError) no function clause matching in Float.to_string/1

    The following arguments were given to Float.to_string/1:

        # 1
        3

    Attempted function clauses (showing 1 out of 1):

        def to_string(float) when is_float(float)

    (elixir 1.16.2) lib/float.ex:624: Float.to_string/1
    iex:4: (file)

Exercise 1: Using IEx or here in the following Elixir cell explore the the Elixir Standard Library.

  • exports Kernel
  • exports String
  • exports String
exports(Kernel)
h(String.to_integer())

Math functions

In Elixir, the :math module offers various mathematical functions that you can use in your programs. This module is part of the Erlang standard library. Elixir has seamless interoperability with Erlang libraries, and it encourages direct interaction with Erlang code. Keep in mind that Erlang conventions differ slightly: variables start with an uppercase letter, and atoms (simple lowercase names) are used. So, to access the Erlang math module in Elixir, you simply write :math.

When using the i helper in the IEx (Interactive Elixir) shell, you can retrieve detailed information about an Erlang module. It provides a concise summary of the module, including its data type, bytecode location, source file, version, compile options, and implemented protocols.

Exercise 2 : Try to use i (:math) and h :math to access its documentation.

To use a function from the :math module, you can use the dot notation to specify the name of the module and the name of the function, separated by a dot. For example:

iex> ratio = 10.0 / 1.0
10.0
iex> decibels = 10 * :math.log10(ratio)
10.0

The first example computes the logarithm base 10 of the signal-to-noise ratio using the :math.log10/1 function. The :math module also provides a function called :math.log/1 that computes logarithms base e.

The second example finds the sine of a value in radians using the :math.sin/1 function:

iex> radians = 0.7
0.7
iex> height = :math.sin(radians)
0.644217687237691

The :math.sin/1 function, as well as the other trigonometric functions (:math.cos/1, :math.tan/1, etc.), take arguments in radians. To convert from degrees to radians, you can use the following formula:

iex> degrees = 45
45
iex> radians = degrees * :math.pi / 180
0.7853981633974483
iex> :math.sin(radians)
0.7071067811865476

The expression :math.pi gets the value of π from the :math module. The value of this variable is an approximation of π, accurate to about 15 digits.

If you know your trigonometry, you can check the previous result by comparing it to the square root of two divided by two:

iex> :math.sqrt(2) / 2.0
0.7071067811865476

It is a good practice to familiarize yourself with the functions available in the :math module and to use them in your programs when needed. This can save you time and help you avoid errors in your mathematical

To learn more about math module

i(:math)

h(:math)

Random numbers

Adding new functions

Definitions and uses

Flow of execution

Parameters and arguments

Fruitful functions and void functions

Why functions?

Debugging

Glossary

Exercises