What is function?What is function in python?


Function in python


What is function?What is function in python?


Function is the most important tool in procedural programming language.A function can be defined as it is a block of statement which is used to perform any particular operation according to requirement.A function is a  block of organized and reusable program and code that perform a specific and as well as definite task.The main objective to create a function is to reduce the complexity of a program, in this case a program can be divided in to multiple parts according to requirement that part is called function or module.
We can create user friendly program with the help of function in this case user can create separate function for each module  module according to requirement.
It provide extensibility , it means program can be enhanced module wise in feature. it also provide reusable of program, it means a function can coll more than one times
There are two types of function one predefined function like-dir( ),len ( ) and abs ( ) and second is user define function which is created with the help of def keyword in python.

How to create a function in python ?

Some important point to be remember  during creating a function.


  • function block must be start with def keyword.
  • The function is followed by function name and parentheses ( ).
  • After parentheses ( ) : colon sign must be placed.
  • Parameter and augment is placed in parentheses ( ) 
  • The block of statement within function is properly indented from the block of statement.
  • A function may be return type which have return[ expression] that is optional

Example:
    
         def function_name(variable1,variable2,..... ):
                 block of statement/ body of function

How to call a function in python ?

           function_name()
           #calling a non parameterized function
           function_name(variable1,variable2,......)
           #calling a parameterized function

Python program using function............

Q.write a program to add two number using function



def addition(num1,num2):
    return(num1+num2)
#calling function
#function is return type so i can write in print function
#and second method is using another variable like- sum=addition(10,15)
#then print sum
#sum=addition(10,15)
#print("Addition of two number is :"sum)
print("Addition of two number is :",addition(10,15))
  


Q.Write a program to input three number after that check greater and smallest.


def greater(num1,num2,num3): #find greater function
    if num1>num2 and num1>num3:
        print("Greater number is :",num1)
    elif num2>num3:
          print("Greater number is :",num2)
    else:
          print("Greater number is :",num3)


def smallest(num1,num2,num3): #find smallest function
    if num1<num2 and num1<num3:
        print("Smallest number is :",num1)
    elif num2<num3:
          print("Smallest number is :",num2)
    else:
          print("Smallest number is :",num3)


num1=int(input("enter first number :"))    
num2=int(input("enter first number :"))  
num3=int(input("enter first number :")) 
greater(num1,num2,num3) #calling function
smallest(num1,num2,num3)
Output:

enter first number :11
enter first number :12
enter first number :13

Greater number is : 13
Smallest number is : 11

Q.Write a program to return full name in python.

def name(first,last):
    n=first+" "+last
    return n
first=input("Enter first name :"
last=input("Enter last name :")
print("**********************")
print("Full name is:",name(first,last))   

Output:
Enter first name :ankesh
Enter last name :kumar
**********************
Full name is: ankesh kumar

Q.Write a program time in to minutes.
def time_min_in_min(hrs,min):
    minu=hrs*60+min
    return minu

h=int(input("Enter the hours :"))
m=int(input("Enter the minutes :"))
minutes=time_min_in_min(h,m) 
print("Minutes :",minutes)   
Output:
Enter the hours :1
Enter the minutes :30
Minutes : 90

Q.Write a program to find frequency of string.
def frequency(str):
   n=len(str)
   p=0
   for i in range(0, n):
      count=0
      for j in range(0,n):
         if(str[i]==str[j]):
            count+=1

      for k in range(0,i):
          if(str[i]==str[k]):
             p=1
      if(p==0):
         print(str[i],"=",count) 
      p=0
          
         
strinput("Enter any string :")
frequency(str)
Output
Enter any string :welcome
w = 1
e = 2
l = 1
c = 1
o = 1
m = 1


Thank you..........
please like and follow my Facebook page.....
Let's_code
lambda_function
Decision making statement in python
Lopping_statement_in_python
Pattern program 

Comments

Popular posts from this blog

python pattern programming

Decision making statement in python

Looping statement in python