Python

Functions Basics

Learn the building blocks of modular code. Understand how to define, call, and document Python functions.

By TechCoder TeamLast updated: 2026-06-02
In a Nutshell

Learn the building blocks of modular code. Understand how to define, call, and document Python functions. This hands-on tutorial focuses on practical implementation of functions basics concepts.

Functions Basics

A Function is a block of reusable code that performs a specific task. Instead of writing the same code over and over, you wrap it in a function and call it whenever you need it.

Why use Functions?

  1. Reusability: Write once, use many times.
  2. Organization: Break complex programs into smaller, manageable chunks.
  3. Readability: Giving a name to a block of code makes it easier to understand.

How Functions Work

When you call a function, Python jumps to that function's code, executes it, and then returns to where it was called.

Defining a Function

Use the def keyword followed by the function name and parentheses ().

PYTHON PLAYGROUND
⏳ Loading editor…

Docstrings

It's best practice to include a Docstring (documentation string) immediately after the function definition to explain what it does.

PYTHON PLAYGROUND
⏳ Loading editor…

Type Hints (Modern Python) 🎯

Type hints help document what types of data your function expects and returns. They don't enforce types but make code more readable.

PYTHON PLAYGROUND
⏳ Loading editor…

Recursion: Functions Calling Themselves 🔄

A function can call itself! This is called recursion. It's useful for problems that can be broken down into smaller versions of themselves.

PYTHON PLAYGROUND
⏳ Loading editor…

The pass Statement

Function definitions cannot be empty. If you want to define a function but write the code later, use pass.

PYTHON PLAYGROUND
⏳ Loading editor…

Coding Challenge: FizzBuzz Function 🏆

Write a function fizzbuzz(n) that prints numbers from 1 to n, but:

  • For multiples of 3, print "Fizz"
  • For multiples of 5, print "Buzz"
  • For multiples of both, print "FizzBuzz"
PYTHON PLAYGROUND
⏳ Loading editor…

AI Mentor

Confused about "Python functions definition and usage"? Ask our AI mentor for a simplified explanation.

Quiz

Quiz

Question 1 of 5

Which keyword is used to define a function?

function
func
def
define

Key Takeaways

✅ Use def name(): to define a function.
✅ Use name() to call it.
Docstrings ("""...""") explain what the function does.
Type hints (-> int) document expected types.
Recursion is when a function calls itself.

What's Next?

Functions are boring without data! In the next lesson, we'll learn how to pass Parameters and Arguments to make them dynamic.

Keep coding! 🚀