

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Still calibrating
click for more info
Not enough gems
Cost: 6 gems
1: What Is Functional Programming?
incomplete
2: Why Python?
incomplete
3: Immutability
incomplete
4: Declarative Programming
incomplete
5: It's Math
incomplete
6: Classes vs. Functions
incomplete
7: Debugging FP
incomplete
8: Functional vs. OOP
incomplete
9: Statements vs. Expressions
incomplete
10: Ternary Expressions
incomplete
11: Functions Practice
incomplete
Back
ctrl+,
Next
ctrl+.
This lesson's interactive features are locked, please to keep using them
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???)
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.
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