Python Functions

What is a function in Python?

In Python, a function is a set of related statements that perform a specific task. Functions help break down our program into smaller and modular parts. As our program grows, functions make it more organized and manageable.

Above code explanation:

  • Keyword def that marks the start of the function header.
  • A colon (:) to mark the end of the function header.
  • Optional documentation string (docstring) to describe what the function does.

An example of one python function:

How to call a function in python?

Once a function is created in Python, we can call it by writing functionName() itself or another function/ nested function. Following is the syntax for calling a function.

The return statement

The Python return statement is a special statement that you can use inside a function or method to send the function’s result back to the caller. A return statement consists of the return keyword followed by an optional return value.