Functions in Python
Functions in Python
Topperworld.in
Functions
• The idea is to put some commonly or repeatedly done tasks together and
make a function so that instead of writing the same code again and again
for different inputs, we can do the function calls to reuse code contained
in it over and over again.
➢ Once defined, Python functions can be called multiple times and from
any location in a program.
➢ Our Python program can be broken up into numerous, easy-to-follow
functions if it is significant.
➢ The ability to return as many outputs as we want using a variety of
arguments is one of Python's most significant achievements.
➢ However, Python programs have always incurred overhead when calling
functions.
❖ Function Declaration
©Topperworld
Python Programming
Function Description
len() Returns the length of a python object
abs() Returns the absolute value of a number
max() Returns the largest item in a python iterable
min() Returns the largest item in a python iterable
sum() Sum() in Python returns the sum of all the items in an iterator
type() The type() in Python returns the type of a python object
help() Executes the python built-in interactive help console
input() Allows the user to give input
format() Formats a specified value
bool() Returns the boolean value of an object
➢ User-defined function:
These functions are defined by a programmer to perform any specific task
or to reduce the complexity of big problems and use that function
according to their need.
©Topperworld
Python Programming
Example:
def fun():
print("Welcome to Topper World")
Output:
After creating a function in Python we can call it by using the name of the
function followed by parenthesis containing parameters of that particular
function.
Example:
©Topperworld
Python Programming
Output:
❖ Function Arguments
Arguments are the values passed inside the parenthesis of the function. A
function can have any number of arguments separated by a comma.
Python supports various types of arguments that can be passed at the time of
the function call.
In Python, we have the following 4 types of function arguments.
• Default argument
• Keyword arguments (named arguments)
• Positional arguments
• Arbitrary arguments (variable-length arguments *args and **kwargs)
❖ Default Arguments
©Topperworld
Python Programming
Output:
x:10
y:15
❖ Keyword Arguments
The idea is to allow the caller to specify the argument name with values so
that the caller does not need to remember the order of parameters.
Example:
# Keyword arguments
student(firstname='Topper', lastname='Practice')
student(lastname='Practice', firstname='Tooper')
Output:
Topper Practice
Topper Practice
©Topperworld
Python Programming
❖ Positional Arguments
We used the Position argument during the function call so that the first
argument (or value) is assigned to name and the second argument (or
value) is assigned to age.
Example:
Output:
Case-1:
Hi, I am Kritika
My age is 19
Case-2:
Hi, I am 19
My age is Kritika
©Topperworld
Python Programming
➔ Docstring
• The first string after the function is called the Document string
or Docstring in short.
• This is used to describe the functionality of the function. The use of
docstring in functions is optional but it is considered a good practice.
The below syntax can be used to print out the docstring of a function:
Syntax:
print(function_name.__doc__)
return [expression_list]
©Topperworld
Python Programming
Example:
Output:
©Topperworld