Current Page :1
Page 1 Page 2 Page 3
What is a function in Python?
In Python, 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.
Syntax of Function
def function_name(parameters):
“””docstring”””
statement(s)
Above shown is a function definition which consists of following components.
- Keyword defmarks the start of 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 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 same indentation level.
- An optional returnstatement to return a value from the function.
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.
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 __doc__ attribute of the function.
For example:
For example: def hello(): """ this is just to test a function """ print("hello function") print("how are you") def hi(): ''' this is function hi to test function ''' print("this is good function") function calling print("to call a function ") hello() hi() print() print() print("printinf doc string") print(hello.doc) print(hi.doc)
Output:
to call a function
hello function
how are you
this is good function
printinf doc string
this is just to test a function
this is function hi
to test function
>>>
Types of Functions
Basically, we can divide functions into the following two types:
* Built-in functions
* User-defined functions
Python User-defined Functions
What are user-defined functions in Python?
Functions that we define ourselves to do certain specific task are referred as user-defined functions.
Functions that readily come with Python are called built-in functions. If we use functions written by others in the form of library, it can be termed as library functions.
All the other functions that we write on our own fall under user-defined functions. So, our user-defined function could be a library function to someone else.
Advantages of user-defined functions
User-defined functions help to decompose a large program into small segments which makes program easy to understand, maintain and debug.
If repeated code occurs in a program. Function can be used to include those codes and execute when needed by calling that function.
Programmers working on large project can divide the workload by making different functions. User defined function can be written in following forms:
- Function without arguments and not returning any value
- Functions with arguments and not returning any value
- Function with arguments and returning value
- Functions returning multiple values