Skip to main content

Python Tutorial 5.1 - Defining Functions in Python

Welcome to the “Defining Functions” tutorial!


Overview

Functions let you group reusable blocks of code under a name.

Syntax

def function_name(param1, param2=default_value):
    """
    Optional docstring: describe what the function does
    """
    # function body
    return result

Example

def add(a, b):
    return a + b

sum = add(3, 5)  # sum is 8

Key Takeaways

  • Use def to declare a function.
  • Parameters can have default values.
  • Use return to send back a result.

Happy coding!

Comments