What is a function in Python?

In Python, a function is a group of related statements that perform a specific task.
Functions help break our program into smaller and modular chunks. As our program grows larger and larger, functions make it more organized and manageable.
Furthermore, it avoids repetition and makes code reusable.
You can define functions to provide the required functionality. Here are simple rules to define a function in Python.
  1. Function blocks begin with the keyword def followed by the function name and parentheses ( ( ) ).
  2. Any input parameters or arguments should be placed within these parentheses.
  3. You can also define parameters inside these parentheses.
  4. The first statement of a function can be an optional statement – the documentation string of the function or docstring.
  5. The code block within every function starts with a colon (:) and is indented.
  6. The statement return [expression] exits a function, optionally passing back an expression to the caller. A return statement with no arguments is the same as return None.


Syntax of Function

def function_name(parameters):
        """docstring"""
        statement(s)
  1. Above shown is a function definition which consists of following components.
  2. Keyword def marks the start of the function header.
  3. A function name to uniquely identify it. Function naming follows the same rules of writing identifiers in python.
  4. Parameters (arguments) through which we pass values to a function. They are optional.
  5. A colon (:) to mark the end of the function header.
  6. Optional documentation string (docstring) to describe what the function does.
  7. One or more valid python statements that make up the function body. Statements must have the same indentation level (usually 4 spaces).
  8. An optional return statement to return a value from the function.


Example of a function

   def greet(name):
    """This function greets to
    the person passed in as
    parameter"""
    print("Hello, " + name + ". Good morning!")

How to call a function in python?

Once we have defined a function, we can call it from another function, program or even the Python prompt. To call a function we simply type the function name with appropriate parameters.
>>> greet('Paul')
Hello, Paul. Good morning!
Note: Try running the above code into the Python shell to see the output.

Docstring


The first string after the function header is called the docstring and is short for documentation string. It is used to explain in brief, what a function does.
Although optional, documentation is a good programming practice. Unless you can remember what you had for dinner last week, always document your code.
In the above example, we have a docstring immediately below the function header. We generally use triple quotes so that docstring can extend up to multiple lines. This string is available to us as the __doc__ attribute of the function.
For example:
Try running the following into the Python shell to see the output.
  1. >>> print(greet.__doc__)
  2. This function greets to
  3. the person passed into the
  4. name parameter


Share To:

Arogya Thapa Magar

Post A Comment:

0 comments so far,add yours