Introduction
Welcome to your third lesson. In our previous sessions, we established how to store data in variables and how to classify that data using data types like numbers and strings. We now have the "nouns" of our programming language. Today, we introduce the "verbs": functions.
A function is the primary way we create processes and perform actions in Elisp. Your learning outcome for this lesson is to: Define a function and illustrate how it encapsulates a process with inputs and outputs.
Given your goal of extending Emacs, this is a pivotal lesson. As you've used Emacs for a year, you've been constantly using functions—every command, from saving a file (C-x C-s) to moving down a line (C-n), is an Elisp function at its core. Today, you'll learn how to start building your own.
1. What is a Function?
At its heart, a function is a named, reusable block of code that performs a specific task. Think of it like a recipe: it has a name (e.g., "Chocolate Chip Cookies"), a list of ingredients (inputs), a set of instructions to follow (the process), and a final product (the output).
In programming, we use these terms:
- Name: An identifier we use to call the function.
- Parameters (or Arguments): The inputs that the function accepts.
- Body: The sequence of expressions that defines the process.
- Return Value: The output that the function produces.
Let's start with a short video that provides a clear, conceptual overview of functions.
Defining Functions and Commands - Learning Emacs Lisp #3
This clip from the System Crafters channel gives a solid definition of a function in a programming context, covering its role as a reusable process with inputs and an output.
Watch from 02:04 to 04:50. Focus on the core ideas of reusability, parameters (inputs), and the return value (output).
As the video explains, functions allow us to package up logic so we don't have to repeat ourselves. In Lisp, even the most basic operations are function calls.

In the expression (+ 2 3), + is the function name, 2 and 3 are the inputs (arguments), the process is addition, and the output (return value) is 5.
2. Defining a Function with defun
To create your own named function in Elisp, you use a special form called defun (which stands for "define function").
The basic structure looks like this:
(defun function-name (argument-list)
"Documentation string."
(body-of-the-function))
defun: The keyword that starts a function definition.function-name: The symbol you choose to name your function.argument-list: A list of symbols for the function's inputs. If it takes no inputs, this is an empty list()."Documentation string.": A brief explanation of what the function does. This is optional but highly recommended. You can view it later using theC-h fcommand.body: One or more Elisp expressions that make up the function's process.
For a comprehensive text-based breakdown of this structure, the GNU Emacs Lisp manual is the definitive source.
An Introduction to Programming in Emacs Lisp
Let's read the official documentation on defining functions. This section will walk you through the components of a defun expression.
Please read section 3.1, 'The defun Macro'. This section provides a clear template and explanation for the defun structure.
3. Hands-On: Your First Function
Now, let's put theory into practice. We'll define a simple function, install it into our running Emacs session, and then call it. The following video demonstrates this process. Follow along in your *scratch* buffer.
Defining Functions and Commands - Learning Emacs Lisp #3
This clip demonstrates the entire cycle of defining, evaluating (installing), and calling a simple function.
Watch from 04:50 to 08:03. Type the code into your *scratch* buffer as you watch and use C-x C-e to evaluate the expressions.
Let's create another function, this time related to your interest in writing.
Suppose you want a function that generates a standard chapter title format.
-
Define the function: Type the following into your
*scratch*buffer.(defun format-chapter-title (number title-text) "Format a chapter title with its number." (concat "Chapter " (number-to-string number) ": " title-text))This function takes two inputs: a
numberand atitle-text. The body uses theconcatfunction to join them into a single string.number-to-stringis necessary becauseconcatrequires all its arguments to be strings. -
Install the function: Place your cursor just after the final closing parenthesis of the
defunblock and pressC-x C-e. You should seeformat-chapter-titleappear in the echo area, confirming that Emacs now knows about your function. -
Call the function: Now, on a new line, write an expression to call your function with some arguments.
(format-chapter-title 1 "The Beginning") -
See the output: With your cursor after the closing parenthesis, press
C-x C-e. The echo area will display the output:"Chapter 1: The Beginning".
You have successfully encapsulated a process (formatting a title) into a reusable function!
4. Inputs and Outputs Explained
The Return Value (Output)
You might have noticed that we never explicitly wrote return like in Python or other languages. Elisp has a simple, powerful rule:
The return value of a function is the value of the last expression evaluated in its body.
In our format-chapter-title function, the body only had one expression: the call to concat. The result of that concat call—the newly created string—is the last thing evaluated, so it automatically becomes the function's return value.
Function Arguments (Inputs)
The arguments you define in the list after the function name are the "slots" for your inputs. When you call the function, the values you provide are bound to those argument names within the function's body.
Elisp functions can also have more flexible argument lists, such as optional arguments. Let's briefly watch a demonstration of this to better understand the concept of inputs.
Defining Functions and Commands - Learning Emacs Lisp #3
This segment introduces optional arguments, showing how you can design functions that have default behaviors but can be customized with extra inputs. You don't need to memorize the syntax yet; the goal is to appreciate the flexibility of function inputs.
Watch the first part of this section, from 08:03 to 12:45, focusing on the example of the multiply-maybe function. Notice how it can be called with one, two, or three arguments.
This flexibility in handling inputs is a key feature that makes Elisp powerful for building complex and user-friendly commands.
Conclusion
In this lesson, you've made the leap from passively using data to actively processing it. You now understand the most fundamental building block for creating functionality in Emacs.
Key Takeaways:
- A function encapsulates a process, taking inputs (arguments), executing a process (the body), and producing an output (the return value).
- Functions are defined in Elisp using the
defunspecial form. - Defining a function and then evaluating it with
C-x C-e"installs" it into your current Emacs session. - The value of the last expression in a function's body is its return value.
- Nearly every action in Emacs is powered by an Elisp function, and now you have the tools to start creating your own.
Preview of the Next Lesson:
Our functions so far have been simple, straight-line processes. But what if we want a function to behave differently under different circumstances? For example, what if format-chapter-title should handle a prologue (chapter 0) differently from other chapters? To do that, we need to introduce decision-making. In the next lesson, we will explore control flow and conditional logic using tools like if.