codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🐍

Python Programming

24 / 68 topics
24Python Functions25Function Arguments (*args, **kwargs)26Python Lambda Functions27Python Namespace & Scope (Global/Local)28Python Closures29Python Recursion
Tutorials/Python Programming/Python Functions
🐍Python Programming

Python Functions

Updated 2026-05-15
30 min read

Python Functions

In this tutorial, you'll learn how to create reusable blocks of code using functions in Python. Functions are essential for organizing your code, making it more readable and maintainable. Whether you're a beginner or an experienced programmer, mastering functions will significantly enhance your Python skills.

Introduction

Functions are fundamental building blocks in programming. They allow you to encapsulate a block of code that performs a specific task and reuse it throughout your program. By using functions, you can avoid repeating the same code multiple times, making your programs more efficient and easier to manage.

In this section, we'll cover how to define functions, use parameters, return values, handle multiple return values, and write docstrings for better documentation.

Defining Functions

To define a function in Python, you use the def keyword followed by the function name and parentheses. The basic syntax is:

Python
1def function_name():
2 # Function body
3 pass

Here's an example of a simple function that prints "Hello, World!":

hello_world.py
1def greet():
2 print("Hello, World!")
3
4greet()

When you run this code, the greet function is called, and it executes the code inside its body:

Terminal
Output

Multiple Return Values

Python functions can return multiple values using tuples. This is useful when you need to return more than one piece of information from a function.

Python
1def get_name_and_age():
2 name = "Alice"
3 age = 30
4 return name, age

You can call this function and unpack its return values into variables:

multiple_returns.py
1def get_name_and_age():
2 name = "Alice"
3 age = 30
4 return name, age
5
6name, age = get_name_and_age()
7print(f"Name: {name}, Age: {age}")

When you run this code, the get_name_and_age function returns a tuple containing the name and age. These values are then unpacked into the variables name and age, and printed:

Terminal
Output

Summary

  • Defining Functions: Use the def keyword followed by the function name and parentheses.
  • Parameters: Allow functions to accept input values.
  • Return Values: Use the return statement to return a value from a function.
  • Multiple Return Values: Functions can return multiple values as tuples.
  • Docstrings: Provide documentation for functions using triple quotes.

What's Next?

In the next section, we'll explore more advanced topics related to function arguments, including variable-length arguments (*args and **kwargs). This will allow you to create even more flexible and powerful functions. Stay tuned!


PreviousPython ArraysNext Function Arguments (*args, **kwargs)

Recommended Gear

Python ArraysFunction Arguments (*args, **kwargs)