← Back to lesson 09: Put code in a module

Put code in a module: full examples and further reading

The original lesson material is preserved here for reference. The main notebook focuses on this lesson's core question.

Save functions in an `.erl` file, compile them, and look ahead to BEAM.

Save village.erl, compile it with c(village)., and run village:greet(<<"Maya">>).

Save as village.erl

After saving, open erl in the same folder and run c(village)..

Original full example
%% Keep the file name and module name the same
-module(village).

%% Allow code outside the module to call greet/1
-export([greet/1]).

%% Name is a binary
greet(Name) ->
  <<"hello, ", Name/binary>>.

When compilation succeeds, c(village). returns {ok,village}.

village:greet(<<"Maya">>). returns <<"hello, Maya">>.

Steps for the full example

  1. -module(village). declares the module name. The file should be village.erl.
  2. /1 in -export([greet/1]). says that greet takes one argument.
  3. Name/binary appends the incoming binary to the fixed text.
  4. After compiling, call it with module:function(argument): village:greet(<<"Maya">>).

Original lesson notes

The shell is like scratch paper. Named functions that need to be saved and reused usually belong in a module file. The file name and module name should match.

-export([greet/1]). allows code outside the module to call greet/1. The /1 matters because functions with the same name but different arities are different functions.

Once the module runs, you are ready for BEAM's strengths: small processes, messages, supervision, and recovery from failures. Those belong to the next learning path.

Symbols and terms
-module(village).
Declare the module name as village. The file should be called village.erl.
-export([greet/1]).
Make the one-argument greet function public.
c(village).
Compile and load village.erl in the Erlang shell.
module
A group of functions in one .erl file. The module name usually matches the file name.
export
A list of functions that code outside the module may call. Each item uses name/arity.
process
A small, independent unit of work on the BEAM. Processes work together by sending messages.

Original full exercise

Add a public function farewell/1 that returns <<"bye, ", Name/binary>>.

Exercise starter
-module(village).

%% Export one more function in this list
-export([greet/1, ____]).

greet(Name) ->
  <<"hello, ", Name/binary>>.

%% Complete the new function
____(Name) ->
  <<"bye, ", Name/binary>>.

After compiling again, village:farewell(<<"Maya">>). should return <<"bye, Maya">>.

Show hint

Put farewell/1 in the export list. Put only farewell in the function definition.

Show original answer
Reference answer
-module(village).

%% Both functions take one argument
-export([greet/1, farewell/1]).

greet(Name) ->
  <<"hello, ", Name/binary>>.

%% The final function definition ends with a dot
farewell(Name) ->
  <<"bye, ", Name/binary>>.
Why does the export list say greet/1 instead of only greet?

Erlang identifies a function by both its name and its number of arguments. greet/1 and greet/2 are different functions.

Original lesson takeaways
Design trade-offs and language comparisons
DESIGN DECISION

Why this shape?

Why organize functions in modules before reaching for a class?

Choice

A module names related functions and creates a public boundary. Data remains an explicit argument instead of hiding behind this.

Cost and boundary

Add a process only when state needs an owner, a lifecycle, or failure isolation. A GenServer around a pure function merely adds a queue.

A FAMILIAR POINT OF VIEW

Coming from Java, Python, or JavaScript

Java

Familiar starting point
A class combines data, methods, and object identity.
What BEAM changes
A BEAM module is not an object, and functions have no implicit receiver.
False friend
Calling a module does not start a process.

Python

Familiar starting point
Modules and classes can both organize code.
What BEAM changes
Module/function/arity forms the stable entry point; state is a separate design decision.
False friend
An OTP Application is not an operating-system process.

JavaScript

Familiar starting point
ES modules export functions or classes.
What BEAM changes
BEAM calls compiled modules by module name and arity.
False friend
Mix and rebar3 manage projects; neither is BEAM or OTP.
Original course references