Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Python · Const

def

def defines a named user-defined function, optionally with decorators, parameters, and a return annotation. Its indented body runs when the function is called.

How it works

Executing a function definition creates a function object and binds it to the function name in the current namespace. The definition may include decorators, parameters, and an optional return annotation; its indented suite runs when the function is called, not when it is defined.

Syntax

[decorators] "def" funcname "(" [parameter_list] ")" ["->" expression] ":" suite

Parameters

NameTypeDescription
decoratorsexpressionsOptional decorators applied to the function object.
funcnameidentifierThe identifier receiving the newly created function object.
parameter_listparametersThe function's positional, keyword, variadic, and defaulted arguments.
return_annotationexpressionOptional expression recorded as the function's return annotation.
suitestatement suiteThe indented statements that form the function body.

Returns

The def statement binds a user-defined function object to its name; calling that object produces the function's result.

Key points

  • def creates a callable user-defined function object and binds it to the declared name.
  • A function definition is a compound statement with a header and an indented suite.
  • The def header ends in :, followed by the function body suite.
  • The indented suite contains the statements executed when the function is called.
  • Function definitions are one of the alternatives included in Python's compound-statement grammar.

Common mistakes

  • The definition runs when execution reaches it, so the function name is unavailable before that point.

Example: A basic function definition

def greet(name: str, greeting: str = "Hello") -> str:
    """Return a greeting for one person."""
    return f"{greeting}, {name}!"

print(greet("Ada"))

Defines and calls a function with a required parameter, a default value, and a return annotation.

When to use it

Reusable behavior

Encapsulate reusable computation behind a descriptive callable.

Backend and scripting APIs

Expose a stable interface accepting validated or documented arguments.

Program structure

Separate a script's executable workflow into testable units.

Interview questions

QWhat does def do?

The def statement creates a user-defined function object and binds it to a name. The body is an indented suite that executes when the function is called.

Follow-up: What happens if a def statement appears inside a conditional? The function is created and bound only if execution reaches that branch.

QWhat components can a function definition contain?

Its syntax permits decorators, a function name, a parameter list, an optional -> expression, and a colon followed by the suite. Parameters describe the arguments accepted by the call.

Follow-up: How can a function accept arbitrary positional and keyword arguments? Use *args and **kwargs in the parameter list.

QWhen does a defined function return a value?

A function definition does not itself return the result of the function body. Calling the resulting function executes the suite; an explicit return supplies the call result, while falling through produces None.

Follow-up: Can the definition specify a return type? Yes; the optional annotation after -> is stored as metadata and does not enforce runtime type checking.

  • lambda
  • async def
  • class
  • return
  • decorators
  • function annotations

Source: 8. Compound statements. Checked 2026-10-16.