Variable Scope

Elixir is lexically scoped. The basic unit of scoping is the function body. Variables defined in a function (including its parameters) are local to that function. In addition, modules define a scope for local variables, but these are accessible only at the top level of that module, and not in functions defined in the module.

Do-block Scope

Most languages let you group together multiple code statements and treat them as a single code block. Often languages use braces for this. Here’s an example in C:

 int​ line_no = 50;
 
 /* ..... */
 
 if​ (line_no == 50) {
  printf(​"new-page​​\f​​"​);
  line_no = 0;
 }

Elixir doesn’t really have blocks such as these, but it does have ways of grouping expressions together. The most common ...

Get Programming Elixir ≥ 1.6 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.