---
title: "Welcome to the Functional World!"
description: "If you're ever asked in an interview, What's the difference between Procedural and Functional Programming?, don't worry. In this post, we'll mainly talk about Functional Programming and what makes it different from other paradigms."
canonical_url: "https://otabek.io/blogs/welcome-to-functional-world"
md_url: "https://otabek.io/blogs/welcome-to-functional-world.md"
language: "en"
last_updated: "2024-09-01"
tags: ["Paradigms"]
---

# Welcome to the Functional World!

If you're asked, **"What's the difference between Procedural and Functional Programming?"** in an interview, there's no need to panic. In this post, we'll mainly discuss **Functional Programming** and what sets it apart from other **paradigms**.

![What is Functional Programming?](https://miro.medium.com/v2/1*FDn8bRsxPNBkDYSKnKQfQg.jpeg)

## FP, What?

Functional Programming (FP) is a declarative programming style that tackles complex problems through sequential functions. When we say "functions," we're not referring to the typical code we write daily but rather to mathematical functions. In FP, you write code without relying on **shared state** or **mutable** values.

- **Imperative**: You write “Hello Otabek” to a buffer, and then to the standard output... `there’s still a lot to say`
- **Declarative**: You simply print "Hello Otabek": `print("Hello Otabek")`

## Immutability

![Mutate variable meme](https://i.chzbgr.com/full/9721547520/h628DD729/ahh-mutating-global-state-s-functional-programmer-not-afraid-side-effects-stop-patrick-scaring-him)

You’ve probably heard it—one of the core concepts in FP is **immutability**. Once you declare a variable, you cannot change it. It’s like saying that all values are constants, not variables.

Imagine you're running a program and you know the variables you've declared will never change. Would you still use **try-except** or **catch-throw** statements? Of course not, because the variables never change, and bugs won’t appear (unless you write buggy code yourself).

## Recursion

![recursion](https://preview.redd.it/0wap3cp4khm01.jpg?auto=webp&s=3dec95610a58f519cdc503061ef41bf85ed63295)

When we need to repeat a task or iterate through something, we often use **loops** (like `while`, `for`, etc.). But loops use variables, and they store state. This goes against the principle of immutability we talked about earlier. That’s where **recursion** comes in.

Recursion has a **base case**, which tells us when to stop calling the function. Each function call utilizes the **stack** data structure, following the LIFO (Last In, First Out) principle. When the **base case** tells us to stop, elements are popped from the stack in order to process results in the desired way.

```python-run
# This function says hello as many times as you want.
def say_hello(times: int):
    if times == 0:  # base case, tells us when to stop.
        return
    print("Hello!")
    return say_hello(times - 1)

say_hello(5)
```

## Pure Functions

A **pure function** is exactly that—a function without side effects. If you provide the same input, it will always return the same output. There are no hidden changes happening in the background.

Pure functions are fast because they can be cached. Since they always produce the same output for the same input, there's no need to recalculate results. It’s simple, but believe me, it works wonders.

## High Order vs First-Class Functions

One of the key concepts in FP is **First-class functions**. This means that functions are treated as first-class citizens—essentially, they can be passed as arguments to other functions or returned from functions. For example:

```python-run
# You can use both regular or lambda functions
five = lambda: 5
six = lambda: 6

def add(x, y):
    return x() + y()

print(add(five, six))  # This is essentially 5 + 6
```

Another important concept is **High-order functions**. These are functions that can take other functions as arguments or return functions as their result. For instance:

```python-run
# Takes a function as an argument
def apply_twice(func, x):
    return func(func(x))

def add_two(n):
    return n + 2

result = apply_twice(add_two, 5)
# func(func(x)) means calling the function twice
# add_two(add_two(5)) => add_two(7) => add_two(7) => 9
print(result)  # Output: 9
```

## Procedural vs Functional

Procedural programming is what we typically use when writing code—it involves variables, functions, statements, etc. However, in Functional Programming, the approach is fundamentally different. In short: it’s all about functions and immutability. 😉

## Conclusion

![](https://www.vasinov.com/images/i-should-cat.jpg)

I haven’t written about the pros and cons because the main points you need to understand about FP have already been covered. FP is filled with many terms that belong to the world of mathematics. If you're interested, dive into those topics; there’s a lot to learn. I think this post serves as a great introduction for anyone starting out.

Now, you're ready to explain the difference between Functional and Procedural programming without hesitation. Cheers!

---

```quiz
{
  "quiz": {
    "id": "functional-programming-quiz",
    "title": "Functional Programming Quiz",
    "description": "Test your understanding of FP concepts",
    "questions": [
      {
        "id": "q1",
        "type": "single-choice",
        "question": "What is a core principle of Functional Programming?",
        "options": [
          { "id": "a", "text": "Mutable state", "description": "FP avoids mutable state - immutability is the core principle." },
          { "id": "b", "text": "Immutability", "description": "" },
          { "id": "c", "text": "Global variables", "description": "FP avoids shared state, including global variables." },
          { "id": "d", "text": "Object inheritance", "description": "Inheritance is an OOP concept, not FP." }
        ]
      },
      {
        "id": "q2",
        "type": "single-choice",
        "question": "Why does FP use recursion instead of loops?",
        "options": [
          { "id": "a", "text": "Recursion is faster", "description": "It's not about speed - it's about avoiding mutable state in loop variables." },
          { "id": "b", "text": "Loops use mutable state", "description": "" },
          { "id": "c", "text": "Loops don't exist in FP languages", "description": "Many FP languages have loops, but recursion is preferred for immutability." },
          { "id": "d", "text": "Recursion uses less memory", "description": "Recursion often uses more memory (stack), but it preserves immutability." }
        ]
      },
      {
        "id": "q3",
        "type": "single-choice",
        "question": "What is a 'pure function'?",
        "options": [
          { "id": "a", "text": "A function that only uses primitive types", "description": "Purity is about behavior, not types used." },
          { "id": "b", "text": "A function with no side effects that returns the same output for the same input", "description": "" },
          { "id": "c", "text": "A function that doesn't take any arguments", "description": "Pure functions can take arguments - they just always return the same result for them." },
          { "id": "d", "text": "A function written in a functional language", "description": "Pure functions can be written in any language." }
        ]
      },
      {
        "id": "q4",
        "type": "drag-fill",
        "question": "Complete the higher-order function that applies a function twice:",
        "template": "def apply_twice({{b1}}, x):\\n    return {{b2}}({{b3}}(x))",
        "options": [
          { "id": "opt1", "content": "func" },
          { "id": "opt2", "content": "func" },
          { "id": "opt3", "content": "func" }
        ],
        "blanks": [
          { "id": "b1" },
          { "id": "b2" },
          { "id": "b3" }
        ]
      },
      {
        "id": "q5",
        "type": "single-choice",
        "question": "What makes a function 'first-class'?",
        "options": [
          { "id": "a", "text": "It can be passed as an argument or returned from functions", "description": "" },
          { "id": "b", "text": "It runs faster than other functions", "description": "First-class refers to how functions are treated, not their speed." },
          { "id": "c", "text": "It's defined at the top of the file", "description": "Position in file doesn't matter for first-class status." },
          { "id": "d", "text": "It's a built-in function", "description": "First-class means functions are treated as values, not about being built-in." }
        ]
      },
      {
        "id": "q6",
        "type": "multiple-choice",
        "question": "Which are benefits of pure functions? (Select all that apply)",
        "options": [
          { "id": "a", "text": "They can be cached (memoized)", "description": "" },
          { "id": "b", "text": "They are easier to test", "description": "" },
          { "id": "c", "text": "They always run faster", "description": "Pure functions aren't necessarily faster - their benefit is predictability." },
          { "id": "d", "text": "They produce predictable results", "description": "" }
        ]
      }
    ]
  },
  "answers": {
    "q1": { "correctOptionIds": ["b"] },
    "q2": { "correctOptionIds": ["b"] },
    "q3": { "correctOptionIds": ["b"] },
    "q4": { "correctPlacements": { "b1": "opt1", "b2": "opt2", "b3": "opt3" } },
    "q5": { "correctOptionIds": ["a"] },
    "q6": { "correctOptionIds": ["a", "b", "d"] }
  }
}
```


## Sitemap

See the full [Markdown sitemap](/sitemap.md) for all pages.
