Java, .NET, PHP, PYTHON, ANGULAR, ML, Data Science, Testing, CI Tutorials in Easy Languages.

"Best Software Training, Internship, Project Development center of Indore India, Helpline 780506-3968"

Python User define Function, How to create a function in the python program:-

 

using this we will create a set of methods to provide a proper structure to python code.

the function will subdivide the code in a single file that is easy to understand and re-use program code in the same or different file.


if we create a function-based program in python then it is called the procedural pattern of python code structure.



Syntax of function or procedure:-


def  Functionname():
     statements
     ......

Functionname()



Example of User define a function to calculate addition and subtraction:-

def addition():     #called function
    a = int(input("enter first number"))
    b = int(input("enter second number"))
    c = a+b
    print(c)

def substraction():
    a = int(input("enter first number"))
    b = int(input("enter second number"))
    c = a-b
    print(c)



    
addition()     #calling function
substraction()



Another example of a User define a function to calculate Simple Interest:-


def calculatesi():
    p = float(input("enter value for amount"))
    r = float(input("enter value for rate"))
    t = float(input("enter value for time"))
    si = (p*r*t)/100
    print("result is ",si)

calculatesi()    


Type of function:-

1)  Default:- 

We can not pass input parameters in the default function, input variable will be defined under function block or globally.


1.1 With Return Type:-   We can return value from a function using this.

Syntax of Return Type Function:-


def functionname():
     statements
     return value



Example of return type function in the Python program:-

def calculatesi():

    p = float(input("enter value for amount"))

    r = float(input("enter value for rate"))

    t = float(input("enter value for time"))

    si = (p*r*t)/100

    return si


res = calculatesi()

print(res)




1.2 Without Return Type:-  

We can not return value or output, output will be display under the function block.

Syntax of function:-

def functionname():

       statements
       print(output)


Example of default function 

def addition():
    x=100
    y=200
    print("result is ",(x+y))


def substraction():
    x=10
    y=2
    return x-y


addition()
res = substraction()
print(res)



Assignment of default function:-


1)  WAP to reverse a five-digit number using the no return type function.

2) WAP to perform addition of complex number using return type function

3)  WAP to create a salary calculator using no return type function

4)  WAP to calculate compound interest using no return type function.


2) Parametrised:-  

The parameter is used to pass input value from calling function to called function, python provides a different type of parameters based on the requirement.


2.1)  required argument:-  

it should take all input values from calling  argument to called an argument. if we did not pass any values then it will provide an error.


def Functionname(param1,param2):
       statements


Functionname(10,20)  #code will run
Functionname(10)  #code will provide error


Example of require:-


def addition(x,y):
   print("result is ",(x+y))

addition(10)  #error
addition(10,2)


2.2) keyword-based argument:- 

using this type of function we will create a keyword for the argument which will contain the value of calling arguments to called the argument, name of the keyword argument should be the same as called argument.



def Functionname(x,y):
      statements


Functionname(x=10,y=20) 
Functionname(y=100,x=200)


Example of Keyword based 


def addition(x,y):
   print("result is ",(x/y))


addition(x=10,y=20)
addition(y=100,x=20)
#addition(x=10,2) #error
addition(10,2)


2.3)  default argument:-  

using this function we can set the default value to called argument from calling argument.

def Functionname(x=10,y=20):
      Statements
Functionname(100,200)
Functionname()
Functionname(2)


2.4)  variable length arguments:- 

We can pass multiple parameters as a tuple arguments using variable length arguments

def functionname(*args):
    Statements
    Statements
functionname(10,20,30,40,...)


We can pass a value to nth parameters as a tuple object.


Example of variable-length parameters


def addition(*x):
   for x1 in x:
     print("result is ",x1)

addition(100,4,23,34,45,5,67)



2.5) lambda function :-


It contains a single statement and it is basically used to solve normal expressions in the program.

lambda is a keyword that is used to define the lambda function in the program.


Syntax:-


var = lambda arg1,arg2:statement


Example of lambda function in Python:-

sum = lambda x,y:x+y

print(sum(10,20))



Assignment of Parametrised function in Python:-

1) WAP to create a mark sheet program using function takes the dictionary as an input variable

2)  Create an ATM Program using a function?




11 Comments

If you have any doubt in programming or join online classes then you can contact us by comment .

  1. #WAP to reverse a five-digit number using no return type function.
    def reverse():#define a function
    num=int(input("enter any 5-digit number"))
    for i in range(0,5):
    i=num%10
    num=num//10
    print(i)
    reverse()#calling a function

    ReplyDelete
  2. #WAP to perform addition of complex number using return type function
    def complex():
    a=2+3j
    b=4+5j

    return a+b
    res=complex()
    print(res)

    ReplyDelete
  3. #WAP to create a salary calculator using no return type function
    def salary():
    name=input("enter name of employee")
    basic=float(input("enter basic salary"))
    da=float(10*basic)/100
    hra=float(15*basic)/100
    pf=float(5*basic)/100
    ta=float(8.5*basic)/100
    perday=float(basic/30)
    netpay=float(basic+hra+ta+da-(2*perday))
    grosspay=float(netpay-pf)
    print(grosspay)
    salary()

    ReplyDelete
  4. # WAP to calculate compound interest using no return type function.
    def compoundinterest():
    p=float(input("enter value of p"))
    r=float(input("enter value of r"))
    t=float(input("enter value of n"))
    Amount = p * (pow((1 + r / 100), t))
    CI = Amount - p
    print("Compound interest is", CI)
    compoundinterest()




    ReplyDelete
  5. #WAP to calculate Simple Interest where rate and time will be constant and
    #the principal will be an integer? (by using function)
    def simpleinterest():
    p=int(input("enter principle amount"))
    const=(2,4)
    SI=p*const[0]*const[1]

    print(SI)
    simpleinterest()

    ReplyDelete
  6. #WAP to calculate the area of a triangle, circle, and rectangle in the same program where the value of pi, the base of the triangle, and the height of the rectangle will be constant.
    def traingle():
    H=int(input("enter value of H"))
    const=(2,0)
    area=(const[0]*H)/2
    print(area)
    def circle():
    r=int(input("enter the value of radius"))
    const=(3.14,0)
    area=(const[0]*r*r)
    print(area)
    def rectangle():
    B=int(input("enter value of base"))
    const=(3,0)
    area=(const[0]*B)
    print(area)
    traingle()
    circle()
    rectangle()



    ReplyDelete
  7. #WAP to convert temperature from Celsius to Fahrenheit?
    def temp():
    c=int(input("enter temprature in celsius"))
    f=((9*c)+160)/5
    print(f)
    temp()

    ReplyDelete
  8. #WAP to swap two numbers without using a third variable by using function
    def swap():
    a=int(input("enter value of a"))
    b=int(input("enter value of b"))
    print("before swappine",a,b)
    a,b=b,a
    print("after swapping",a,b)
    swap()

    ReplyDelete
  9. #WAP to calculate square and cube of any entered number(by using function)
    def square():
    a=int(input("enter value of a"))
    sqr=a*a
    print(sqr)
    def cube():
    b=int(input("enter value of b"))
    cub=b*b*b
    print(cub)

    cube()
    square()

    ReplyDelete
  10. sir this program not run properly is it right logic or not
    # Create ATM Program using function ?

    name=input("enter your name")
    balance=float(input("enter balance"))

    def menu():
    print(name,"welcome to our bank")
    print("\n 1-debit,\n 2-credit,\n 3-viewbalance,\n 4-exit")
    def debit():
    print("enter amount that you want to withdraw",amount)
    balance=balance-amount
    def credit():
    print("enter amount that you want deposit",amount)
    balance=balance+amount
    def showbalance():
    print("total balance is",balance)

    def ATM():
    flag=False
    PIN=1234

    for i in range(1,4):
    p=int(input("enter 4 digit pin number"))
    if p==PIN:
    flag=True
    print(flag)
    break
    else:
    print("pin is incorrect and number of attempt is attempted")
    while(True):
    print("\n 1-debit,\n 2-credit,\n 3-viewbalance,\n 4-exit")
    select=int(input("enter your choice"))
    if select==1:
    print("name","amount to withdraw",debit())
    menu()
    if select==2:
    print("name","amount to withdraw",credit())
    menu()
    if select==3:
    print("name","amount to withdraw",showbalance())
    menu()
    if select==4:
    print("name","amount to withdraw",exit())
    menu()
    called by
    import atm
    atm.ATM()
    debit.atm()
    credit.atm()
    showbalance.atm()






    ReplyDelete
  11. #sir not run properly is it right logic for function
    def marksheet(dict):
    dict={"phy":60,"eng":40,"res":90,"hindi":40,"maths":50}
    count=0
    gm=0
    s=""
    dist=""
    for i in dict:
    if dict[i]>=0 and dict[i]<=100:
    print(dict[i])
    if dict[i]<33:
    count=count+1
    s=s+i+ " "
    gm=dict[i]
    else:
    if dict[i]>=75:
    dist = dist+ i + " "
    else:
    print("All subject marks should be 0 to 100")
    break


    total=total+marks[i]
    else:
    if count==0 or(count==1 and gm>=29):
    per= total/5
    if per>33 and per<45:
    print("Third division with {0} %".format(per))
    elif per<60:
    print("Second division with {0} %".format(per))
    else:
    print("First division with {0} %".format(per))

    if gm>0:
    print("You are pass by grace and grace marks is ",str(33-gm), "grace subject is ",s)
    if dist!=0:
    print("Distinction subject name is ",dist)

    elif count==1:
    print("suppl in ",s)
    else:
    print("fail in ",s)

    marksheet(dict)

    ReplyDelete

Post a Comment

If you have any doubt in programming or join online classes then you can contact us by comment .

Previous Post Next Post