Functions in Python

Functions in Python programming are used to divide a large program into sub-programs for easy control. All these sub-programs are called sub-functions of the program.

Program Calling Function

Program

def fun(): #this is Function which name is 'fun()',
    print("This is Function")
fun() #calling 'fun()' function.
print("After Calling Function")
#Must Write 'def' Before Function Name when U Declaring Function.
Output
This is Function
After Calling Function

Program Calling Functions

Program

def fun(): #'fun()' is name of Function.
    print("This is 1st Function")
print("Before Calling 1st Function")
fun() #calling 'fun()' function.
print("After Calling 1st Function")
def fun2(): #'fun2()' is name of Function.
    print("This is 2nd Function")
print("Before Calling 2nd Function")
fun2() #calling 'fun2()' function.
print("After Calling 2nd Function")
Output
Before Calling 1st Function
This is 1st Function
After Calling 1st Function
Before Calling 2nd Function
This is 2nd Function
After Calling 2nd Function

Program Calling Functions Another Program

Program

def fun(): #'fun()' is name of Function.
    print("This is 1st Function")
    print("This is Also 1st Function")
print("Before Calling 1st Function")
fun() #calling 'fun()' function.
print("After Calling 1st Function")
def fun2(): #'fun2()' is name of Function.
    print("This is 2nd Function")
    print("This is Also 2nd Function")
print("Before Calling 2nd Function")
fun2() #calling 'fun2()' function.
print("After Calling 2nd Function")
Output
Before Calling 1st Function
This is 1st Function
This is Also 1st Function
After Calling 1st Function
Before Calling 2nd Function
This is 2nd Function
This is Also 2nd Function
After Calling 2nd Function

Program Calling Functions Another Method
Program

def fun(): #'fun()' is name of Function.
    print("This is fun Function")
def fun2(): #'fun2()' is name of Function.
    print("This is fun2 Function")
    print("This is Also fun2 Function")
def fun3(): #'fun3()' is name of Function.
    print("This is fun3 Function")
print("Before Calling Functions")
fun2() #calling 'fun2()' function.
fun() #calling 'fun()' function.
fun3() #calling 'fun3()' function.
print("After Calling Functions")
Output
Before Calling Functions
This is fun2 Function
This is Also fun2 Function
This is fun Function
This is fun3 Function
After Calling Functions