Function in Python, also called subroutine, method, or procedure is a block of one or more indented statements that perform a specific task. It helps to break our program into smaller and modular chunks. Functions are used to increase the program readability, increase code re-usability, and decrease code redundancy.
Functions can be defined in one place and called or invoked in another. This helps to make code more modular and easier to understand. Suppose we need to write a code of 10 lines in 5 different places in a program. In normal programming we need to write 50 lines, but we can create a function and can call it whenever and wherever we need to. Hence use of function increases re-usability. Similarly less code and dividing every specific task in individual modules increases code readability and decreasing the possibility of errors.
SYNTAX:
>>> def function_name(parameters) :
statement(s)
Keyword def is used to mark the start of a function which is followed by a function name. Naming of functions must be done as the rules of naming variables in python (link here). We use parameters, which are used to pass the value to the function in python. This parameter is optional. Statements in functions are written using indentation (usually four spaces). If we wish to return any value then we use the return keyword. Return is also optional.
TYPES OF FUNCTION
- LIBRARY FUNCTION: These are predefined built-in functions defined in python. Example: print( ), len( ) etc.
- USER DEFINED FUNCTION: These are functions written by developers.
HOW FUNCTION WORKS ?
The flow of control a program jumps to the point where the function is defined from where the function is called when the function is called. Then it executes the blocks of codes inside the function.
The return statement is used to exit a function and go back to the place from where it was called. If there is no return statement in the function code, the function ends, when the control flow reaches the end of the function body.
Each time a functional call is made, the program flow jumps to the location of the body of the function and executes the statements there. Once the function operation is completed, it returns to the point from where the function was called.
DEFINING A FUNCTION
>>> def fun( ):
print("hello world")
In the above example we have defined a function named fun and we haven’t used any parameters as it is optional. In the function body, we have used four spaces indent and printed hello world. Let’s learn how to call the function outside of the function body. To call a function we simply type the function name with appropriate parameters.
>>> fun( )
Output:
hello world
Functions are a construct to structure programs. In the above example we created a function with empty parentheses that do not take arguments, but we can define parameters in function definitions within their parentheses. Let’s define another function add() which is used to add two numbers by passing parameters and returning value from function.
>>> def add(a,b):
sum = a+b
return sum
>>> sum = add(4,6)
>>> print ("sum of 4 & 6 is ",sum)
Output:
sum of 4 & 6 is 10
We passed the number 4 in for the parameter and 6 in for the b parameter. These values correspond with each parameter in the order they are given.
DEFAULT ARGUMENTS
Arguments are specified after the function name, inside the parentheses. We can add as many arguments as we want, separating them with a comma. A parameter is the variable listed inside the parentheses in the function definition. An argument is the value that is sent to the function when it is called. In the above example x & y are argument and a & b are parameters. There is always an equal number of parameters and arguments.
In a case that argument is not passed to the function, we define a default value to the parameter. By using default arguments we can avoid the errors that may arise while calling a function without passing all the parameters.
>>> def mul(a=2,b=6):
return a*b
>>> a = mul(4)
>>> print ("multiplication is ",a)
Output:
multiplication is 24
In the above example we have defined a function mul which multiplies two numbers and returns a value. While calling function, we have passed only one argument instead of two. This code works well because we have already defined the default value for both parameters. When we don’t pass one or both arguments, we can use the default value in Python.
ARBITRARY ARGUMENTS
We generally pass a known number of arguments while calling a function. We can pass arguments less than parameters when we declare default parameters. But what if we pass more number of arguments than parameters defined? In this condition we use arbitrary arguments. Python allows us to pass an arbitrary number of arguments into a function and then process those arguments inside the function.
To define a parameter list as being of arbitrary length, we mark parameter with an asterisk (*).
>>> def names(*args):
for name in args:
print('Welcome', name)
>>>names ('Diwas’, ‘Sush’, ‘Pari’, ‘Sunil’)
Output:
Welcome Diwas
Welcome Sush
Welcome Pari
Welcome Sunil
This page is contributed by Diwas & Sunil . If you like AIHUB and would like to contribute, you can also write an article & mail your article to itsaihub@gmail.com . See your articles appearing on AI HUB platform and help other AI Enthusiast.
This article is really a pleasant one it helps new net users, who
are wishing for blogging. adreamoftrains best website hosting