Functional Programming

10. Functional Programming#

Functional programming is a style of writing programs as transformations from inputs to outputs.

  • Pure functions and side effects

  • Immutable data and returned values

  • Functions as values

  • Comprehensions, lambdas, and higher-order functions

  • Decorators, recursion, context managers, and functools

Python is not a purely functional language, but it includes functional tools that help you write concise, reusable, and testable code.

Chapter Video

This overview introduces the idea that functions are values that can be assigned, passed, and returned.

Programming Terms: First-Class Functions (15:28) by Corey Schafer

Hosted on YouTube; property of its original creator, embedded here for educational reference only.

Learning Objectives

By the end of this chapter, you will be able to:

  1. Identify pure functions and avoid unnecessary side effects

  2. Use immutability by returning new values instead of mutating existing objects

  3. Treat functions as first-class values that can be passed to and returned from other functions

  4. Use lambda functions with map(), filter(), and sorted()

  5. Build decorators, recursive functions, context managers, and selected functools utilities

Chapter Flow

Glossary

#

Term

Explanation

1

Functional programming

A programming style that emphasizes functions, expressions, and limited mutation.

2

Pure function

A function whose output depends only on its inputs and that has no side effects.

3

Side effect

A change or interaction outside a function’s returned value, such as printing or mutating data.

4

Immutability

The practice of using values that are not changed after creation.

5

First-class function

A function that can be stored, passed as an argument, or returned like other values.

6

Higher-order function

A function that accepts another function as an argument or returns a function.

7

Lambda function

A small anonymous function written with the lambda keyword.

8

Map

A pattern that transforms each item in a collection.

9

Filter

A pattern that keeps only items that satisfy a condition.

10

Reduce

A pattern that combines many values into one result.

11

Decorator

A function that wraps another function to extend or change its behavior.

12

Comprehension

A compact expression for building a collection from an iterable.

13

Recursion

A technique where a function calls itself on a smaller version of a problem.

14

Base case

The stopping condition in a recursive function.

15

Recursive case

The part of a recursive function that calls the function again.

16

Context manager

An object that manages setup and cleanup around a with block.

17

functools

A standard-library module with tools for functional programming patterns.

18

Partial function

A function with some arguments pre-filled, often created with functools.partial.

19

Cached result

A saved function result returned immediately when the same arguments are used again.

20

lru_cache

A functools decorator that memoizes recent function calls.

Chapter Overview Slides