× Python Introduction What is Python Python Features Python History Python Applications Python Install Python Path Python Example Execute Python Keywords Constant Variable Statements & Comments Python I/O and Import Operators UnaryBinaryTernary Unary Operators Unary Minus Binary Operators Arithmetic Operators Assignment Operators Relational Operators Logicaloperators Bitwise Operator Ternary Operators Control Statements in Python conditonal Statements IF if else Else If Nested if Switch For loop Nested For Loop While Loop Nested while Loop Unconditonal Statemets Continue Break Pass FUNCTIONS Python Function Function Argument Python Recursion Anonymous Function Python Modules NATIVE DATATYPES Python List Python Numbers Python Tuple Python String Python Set Python Dictionary OOPS PRINCIPALS Encapsulation Class Variable Method Object Or Instance CreationMethod Calling OOPS Syntax And Explanation DATA ABSTRACTION Constructor Inheritance 1.Single or simple Inheritance 2.Multilevel Inheritance 3.Hierarchical Inheritance 4.Multiple Inheritance 5.Hybrid Inheritance Operator Overloading File Operation Python Directory Python Exception Python - Multithreading Python - Database Access Python - CGI Python - Reg Exp Python - Date Python - XML Processing Python - GUI
  • iconPython Online Training In Andhra Pradesh and Telangana
  • icon9010519704

Opening Hours :7AM to 9PM

Python Functions

In this article, you'll learn about functions, what a function is, the syntax, components, and types of functions. Also, you'll learn to create a function in Python.

What is a function in Python?
In Python, a function is a group of related statements that performs 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 the code reusable.
Syntax of Function

def function_name(parameters):
	"""docstring"""
	statement(s)
Above shown is a function definition that consists of the following components.
Keyword def that marks the start of the function header.
A function name to uniquely identify the function. 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 the 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 the same indentation level (usually 4 spaces).
An optional return statement to return a value from the function.

In Python language these Functions are classisfied into following types
1.Noparametarized Functions
2.Parametarized Functions
Noparametarized Functions:
If any Functions signature does not contain parameters is known as Noparametarized Functions
Syntax:

def functionsname():
    ....
    ...
    ...
    
    
Parametarized Functions
If any Functions signature contains List of parameters is known as parametarized Functions
Syntax:

    
def Functionsname(variable1,variable2):
    ....
    ...
    ...
Based on the return type these Functions are classified into following types
1.No return type with no Parametarized Function
2.No return type with Parametarized Function
3.return type with no Parametarized Function
4.return type with Parametarized Function

Python Functions

Image
1. MaxNumber Program Using No return type with No Parametarized

def max():
        x=10
        y=20
        if(x>y):
            print("x is max")
        else:
            print("y is max")

if __name__=="__main__:
    
    max()    
    
Output:
y is Max

def max():
        x=int(input("Enter x Value"))
        y=int(input("Enter y Value"))
        if(x>y):
            print("x is max")
        else:
            print("y is max")

if __name__=="__main__:
    
    max()    
    
Output:
Enter x ,y values
23 45
y is Max
2. MaxNumber Program Using No return type with Parametarized

def max(x,y):
        
        if(x>y):
            print("x is max")
        else:
            print("y is max")

if __name__=="__main__:
    
    max(10,20)    
    
Output:
y is Max

def max(x,y):
        
        if(x>y):
            print("x is max")
        else:
            print("y is max")

if __name__=="__main__:
    
    x=int(input("Enter x Value"))
    y=int(input("Enter y Value"))  
    max(x,y)    
    
Output:
Enter x ,y values
23 45
y is Max
3. MaxNumber Program Using return type with No Parametarized

def max():
        x=10
        y=20
        if(x>y):
            return("x is max")
        else:
            return("y is max")

if __name__=="__main__:
    
    s=max()  
    print(s)
    
Output:
y is Max

def max():
        x=int(input("Enter x Value"))
        y=int(input("Enter y Value"))  
        if(x>y):
            return("x is max")
        else:
            return("y is max")

if __name__=="__main__:
    
    s=max()  
    print(s)
    
Output:
Enter x ,y values
23 45
y is Max
4. MaxNumber Program Using return type with Parametarized

def max(x,y):
        
        if(x>y):
            return("x is max")
        else:
            return("y is max")

if __name__=="__main__:
    
    s=max(10,20)  
    print(s)
    
Output:
20

def max(x,y):
         
        if(x>y):
            return("x is max")
        else:
            return("y is max")

if __name__=="__main__:
    
    x=int(input("Enter x Value"))
    y=int(input("Enter y Value")) 
    s=max(x,y)  
    print(s)
    
Output:
Enter x ,y values
23 45
45
3. MaxNumber Program Using return type with No Parametarized (Integer return)

def max():
        x=10
        y=20
        if(x>y):
            return x
        else:
            return y  

if __name__=="__main__:
    
    s=max()  
    print(s)
    
 
Output:
20

def max():
        x=int(input("Enter x Value"))
        y=int(input("Enter y Value"))
        if(x>y):
            return x
        else:
            return y  

if __name__=="__main__:
    
    s=max()  
    print(s)    
Output:
Enter x ,y values
23 45
45
4. MaxNumber Program Using return type with Parametarized(Integer Return)

def max(x,y):
        
        if(x>y):
            return x
        else:
            return y  

if __name__=="__main__:
    
    s=max(10,20)  
    print(s)
    
 
Output:
20

def max(x,y):
        
        if(x>y):
            return x
        else:
            return y  

if __name__=="__main__:
    
    x=int(input("Enter x Value"))
    y=int(input("Enter y Value"))
    s=max(x,y)  
    print(s)    
Output:
Enter x ,y values
23 45
45