maruthaveeran thanujan
2 weeks ago
Maruthaveerancountry asked

What's def function 🤔

Sudip Bhandari
Expert
2 weeks ago

Great question! In Python, def is a keyword used to define a function.

A function is a block of reusable code that performs a specific task—you define it once and can use it as many times as you like.

Example:

def greet(name):
    print(f"Hello, {name}!")

In the code:

  • def tells Python that you're defining a function.

  • greet is the name of the function.

  • (name) is a parameter the function takes.

  • The indented line below it is the function body, which runs when the function is called.

To actually use the function, you'd call it like this:

greet("Alice")  # Output: Hello, Alice!

Why use functions?

  • They help you avoid repeating code.

  • They make your programs easier to read and maintain.

  • You can break a complex task into smaller parts.

Python
This question was asked as part of the Learn Python Basics course.