A module names related functions and creates a public boundary. Data remains an explicit argument instead of hiding behind this.
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)..
%% 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
-module(village).declares the module name. The file should bevillage.erl./1in-export([greet/1]).says thatgreettakes one argument.Name/binaryappends the incoming binary to the fixed text.- 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 calledvillage.erl. -export([greet/1]).- Make the one-argument
greetfunction public. c(village).- Compile and load
village.erlin the Erlang shell. - module
- A group of functions in one
.erlfile. 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>>.
-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
-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
- Keep the module name and
.erlfile name the same. - An export list identifies functions with
name/arity. - After learning the syntax, continue to BEAM processes and messages.
Design trade-offs and language comparisons
Why this shape?
Why organize functions in modules before reaching for a class?
Add a process only when state needs an owner, a lifecycle, or failure isolation. A GenServer around a pure function merely adds a queue.
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.