Functions In Python-
Python Functions-
- – A block of instructions/statements
- – A function consists of a block of code which is reusable and can be accessed only when it is called.
- – It can be used for a single and relatable action. With the help of functions, a particular program can provide a better approach for the application, and it can be seen in the case of Python as well.
- – Different programming languages follow a particular set of functions which run automatically during the execution of the program.
- – These functions are called the main function and it needs to have a return type and arguments with respect to the standard of the language.
- – In the case of Python, there are some built-in functions, however, python does not have some special type of functions which can be executed automatically during running a program.
Describing A Function In Python-
- For defining a Functions in python, some rules need to be followed, which are described below:
- – In case of python, for defining a function, it is required to start the function with a keyword ‘def’, followed by the function name, along with the parentheses.
- – The input parameters or any kind of arguments need to be there within the parenthesis and the users can define the parameters in this as well.
- – The function may contain some optional statements and these first statement is known as the docstring.
- – The users can place a return statement at the end of the function for exiting from the functions.
Function Comes With Below Two Attributes-
- 1) Function Definition
- 2) Function Calling
1) Function Definition In Python-
- – A set of logical statements which is used to perform a specific task.
- – We can call the function definition by function calling mechanism.
Ex- def subtraction(a, b): # function definition c = a – b return c sub = subtraction(70, 30) # function calling print(“The subtraction is- “, sub) Output- The subtraction is- 40
Types Of Functions In Python-
1) No Arg & No Return Value
def fun():
----
----
fun()
2) With Arg & No Return Value
def fun(x):
-----
-----
fun()
3) With Arg & With Return Value-
def fun(x):
----
----
return 'a'
fun(10)
4) No Arg With Return Value-
def fun():
-----
-----
return 'abc'
fun()
Notes- It is not recommended to call the function before its definition. ************************************
Types Of Functions On The Basis Of Argument Passed-
- 1) Default argument function
- 2) Required argument function
- 3) Keyword argument function
- 4) Variable argument function
1) Default Argument Function-
- – Passing the default argument will always treat as optional parameter.
- – We will not face the error if we will not pass this in any function.
- – Default argument can only be used at last during argument passing mechanism.
- – In case we pass the parameter then existing default argument value will be replaced by passing argument.
Ex-
def test(a, b=50): #Default argument b=50
print(a)
print(b)
print("****")
test(20, 30) # b=30 will override default value. so b will be 30
test(40) #b will be 50 as we didn't passed any value for argument b. So be will be 50
test(10, "Python") # b="Python" will override default value. so b will be 'Python'
Output-
20
30
****
40
50
****
10
Python
****
2) Required Argument Function-
- – During function call, it is always mandatory to pass the parameter for all the argument.
- – It is the same kind of function we have in other programming languages as well like functions in C, C++ or in Java.
Ex-
def test(a, b):
print(a)
print(b)
test(7, 2)
Output-
7
2
3) Keyword Argument Function-
- – We can pass the parameter using the argument name as a keyword.
- – In the case of keyword argument function, there is no need to follow the order of the arguments during the function calling.
Ex-
def test(name, id, loc="Delhi"):
print("name", name)
print("id", id)
print("address", loc)
print("***********")
test("Python", 27)
test(27, "Python")
test(id=27, name="Rohit", loc="UP") # Keyword name should same. Ordering doesn't matter.
Output-
name Python
id 27
address Delhi
***********
name 27
id Python
address Delhi
***********
name Rohit
id 27
address UP
***********
4) Variable Argument Function-
- – We can use this variable type of function when we don’t know the length of parameter which will be passed during function call.
- – This is one of the cause of not having method overloading in Python.
Ex-
def Test(*aa): #*aa - Dynamic memory allocation
print(aa)
Test(10, 20, 'Python')
Output-
(10, 20, 'Python') #This is a tuple.
Ex-
def f1(*args): #Argument function
print(args) #Return tuple
f1(10,20,30)
print("***********************")
def f2(**kwargs): #Keyword argument function
print(kwargs) # Return dictonary
f2(name='Python', rollno=101, sal=77777)
Output-
(10, 20, 30)
***********************
{'name': 'Python', 'rollno': 101, 'sal': 77777}
Notes-
– i++ and ++i are not allowed in Python.
Examples Of Some Built-In Functions Of Python-
- The interpreter of python assistance of numerous function for executing the program, some illustrations of them are given below:
- any(), id(), sum(), str(), print(), open(), range(), reversed(), bytes (), map(), input (), vars(), round() and so on.