DEV Community

Sreekar Reddy
Sreekar Reddy

Posted on • Originally published at sreekarreddy.com

πŸ“‹ Functions Explained Like You're 5

A recipe card you can reuse

Day 22 of 149

πŸ‘‰ Full deep-dive with code examples


The Recipe Card

Your mom has a recipe card for chocolate chip cookies.

Every time she wants cookies:

  1. She doesn't reinvent the recipe
  2. She just follows the card
  3. Same steps, same delicious cookies!

Functions are recipe cards for code!


In Code

Instead of writing the same thing over and over:

# The recipe (function)
def make_greeting(name):
    return "Hello, " + name + "!"

# Use it anytime!
make_greeting("Alex")   # "Hello, Alex!"
make_greeting("Sam")    # "Hello, Sam!"
Enter fullscreen mode Exit fullscreen mode

Write once, use many times!


The Magic

Functions can:

  • Take inputs (ingredients)
  • Do something (the recipe steps)
  • Give output (the finished dish)
Input: name
Process: add "Hello" before it
Output: greeting
Enter fullscreen mode Exit fullscreen mode

In One Sentence

Functions are reusable blocks of code that take inputs, do something, and return outputs.


πŸ”— Enjoying these? Follow for daily ELI5 explanations!

Making complex tech concepts simple, one day at a time.

Top comments (0)