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.
- Function
blocks begin with the keyword def followed by the function
name and parentheses ( ( ) ).
- Any input parameters or arguments should be
placed within these parentheses.
- You
can also define parameters inside these parentheses.
- The
first statement of a function can be an optional statement – the documentation
string of the function or docstring.
- The
code block within every function starts with a colon (:) and is indented.
- 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)
- Above shown is a function definition
which consists of following components.
- Keyword def marks
the start of the function header.
-
A function name to uniquely identify it. Function naming follows the same rules of writing identifiers in python.
-
Parameters (arguments) through which we pass values to a function. They are
optional.
-
A colon (:) to mark the end of the function header.
-
Optional documentation string (docstring) to describe what the function does.
-
One or more valid python statements that make up the function body. Statements
must have the same indentation level (usually 4 spaces).
-
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.
- >>> print(greet.__doc__)
- This function greets
to
- the person passed into the
- name parameter