We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

This lesson's interactive features are locked, please to keep using them

Declarative Programming

Click to play video

Functional programming aims to be declarative. We prefer to declare what we want the computer to do, rather than muck around with the details of how to do it.

Let's take an extreme example and pretend we wanted to style a webpage with CSS. (Obviously a hypothetical because, well, why would anyone want to work on the frontend???)

Declarative Styling

The following CSS changes all button elements to have red text:

button {
  color: red;
}

It does not execute line-by-line like an imperative language. Instead, it simply declares the desired style, and it's up to a web browser to figure out how to apply and display it.

Imperative Styling

Unlike functional programming (and CSS), a lot of code is imperative. We write out the exact step-by-step implementation details. This Python script draws a red button on a screen using the Tkinter library:

from tkinter import Button, Tk  # first, import the library

master: Tk = Tk()  # create a window
master.geometry("200x100")  # set the window size
button: Button = Button(master, text="Submit", fg="red")  # create a button
button.pack()
master.mainloop()  # start the event loop