MyInternships.in

Python Functions

Python Functions

A function is a named, reusable block of code that performs a task. Instead of repeating the same lines again and again, you write them once inside a function and call that function whenever you need the job done.


Why Functions Matter

As programs grow, copy-pasting the same logic everywhere becomes messy and error-prone. Functions let you break a big problem into small, testable pieces, avoid duplicate code, and give a meaningful name to a set of steps — making your code easier to read, debug and reuse across variables, loops and even other functions.

Defining a Function with def

You create a function using the def keyword, followed by a name, parentheses for parameters, and a colon. The indented block below it is the function body — it only runs when the function is called.

A simple function
Python
def greet(name):
    """Print a friendly greeting for the given name."""
    print(f"Hello, {name}! Welcome to Python.")

greet("Aarav")
greet("Diya")
Output
Output
Hello, Aarav! Welcome to Python.
Hello, Diya! Welcome to Python.

Parameters and the return Statement

Parameters are the inputs a function accepts, listed inside the parentheses. The return statement sends a value back to whoever called the function, so you can store it in a variable and use it later. A function without a return statement automatically returns None.

Returning a value
Python
def add(a, b):
    result = a + b
    return result

total = add(5, 7)
print(total)      # 12
print(add(2, 3))  # 5 (used directly)
💡

A function can have multiple return statements (for example inside an if/else), but as soon as one runs, the function stops immediately.

Calling a Function

Calling a function simply means writing its name followed by parentheses with any required arguments, such as greet("Aarav"). Python then jumps into the function body, runs it, and comes back to the next line once it finishes or hits a return.

Docstrings

A docstring is a short description written as a string right after the def line, usually in triple quotes. It documents what the function does and shows up when someone uses help() on your function — a good habit for any beginner writing real projects.

Python
def square(n):
    """Return the square of a number n."""
    return n * n

help(square)
  • def keyword — starts a function definition
  • Parameters — placeholders for input values
  • Function body — indented block of code that runs on call
  • return — sends a value back and ends the function
  • Docstring — optional description right under def

Frequently Asked Questions

What is the difference between a parameter and an argument?+

A parameter is the placeholder name listed in the function definition, like name in def greet(name). An argument is the actual value you pass in when calling it, like greet("Aarav"), where "Aarav" is the argument.

What does a Python function return if there is no return statement?+

It returns None automatically. This is common for functions that only print something or perform an action (like saving a file) without needing to send a value back.

Can a Python function return more than one value?+

Yes. You can return multiple values separated by commas, such as return a, b — Python packs them into a tuple, which you can unpack into separate variables when you call the function.

Related Python Topics

Keep learning with these closely related lessons.

Ready to use your Python skills?

Find Python, data science and software internships and fresher jobs across India.

Browse Python Internships