The function in a python is a block of reused code . that performs a specific task. That functions makes code as more readable , reuseable and organization . the function takes as parameter as a input and return the value as output. the functions there are two types of python functions
Advantages Or Needs of Functions
Code Reuseable
Code Organization
Easy to debug
reduce the code
avoid the redundancy
Built in Functions
User Define Functions
Anonymous functions
The Python provides the predefined functions to code get more readable we used that code in the program .
The built in functions are predefined means the python developer already developed that functions that functions we used later in the python code. some built in functions is below
The Type() it is a predefine function. it is used to check the type of variable .
The it is also predefine function used to print the console output on the screen
The input function is used to take the input from the user . The input takes every input as a treated as a string . then we want to convert that into our form when require.
It is a mathematical operation find the maximum value in the integer.
It is a mathematical operation find the minimum value in the integer.
The len() function is used find the length of the string
The abs() function is used to convert the negative value to positive value . as find the absualte value.
The Id() function is used to check the identifier value as in the collection
The complex() function is used to convert the one data type to another data type by using functions
Etc.The User Define function is a when user going to define the function when it is require . The user is creating the function by using the def keyword followed by function name and parameters . syntax and example in the below
def Function_name parameters colon
# function Body
return value
def greet(name):
return name
Result=greet("Darshan")
print(Result)
Output
Darshan
The function is a contain the def keyword and Function_name , parameters, colon, function body. we want to create the function we definitely follow this definition .
def function_name parameters and colon
return value
function statement
def the def is a keyword used to create a function as user . every user define function with start with a def keyword
Function_nameThe function name is a identifier in the python it store the data and it return the result when the function to the caller .
Parameters() it is a piece of data that function can have . it followed by colon : it specifics the end of the function
return the return is a keyword it returns the value of a function . and it sends back of the value when the function to the caller
We want to print the function body statements we most be call the function inside the function . when the program calls the function , program control is transferred to the called function. a called function returns control to the caller when its return statement is executed or the function is finished .
Example:
larger=max(3,4)
# the function body
The return statement in python is returns the function data then after that data may comes when the function to the caller . The function return value is called fruitful function . followed by the expression it evaluate the expression and performs a specific task. the expression is any type like string,int,boolean, float,and other. syntax and Example in the below
return Expression
return a+b
The void function is a one type of function no return value . the void function can perform a specific task but does not return the value. tha python program program does not include the return statement. and the the python void function provides the multiple parameters to declare the value inside the function the we want to call that function by using a function name. Example in the below
def add( a ,b):
print('sum is', a+b)
result=add(10,20)
print(result)