Function or Procedure in Python

0
Function or Procedure in Python:-

It is used to subdivide the program based on different sub-routine. The function will be defined by def keyword in Python.

def Functionname():
   statements
   ...


Functionname()  #call to function


Type of Function:-

1  Default:-

we can not pass any parameters to default function, Input variable will be defined under function block.
1.1.1  Without return statement:-  This function will display output under method definition

def Functioname():
    statements
    statements

1.1.2  With return statement:- This function will return output data from the method definition

Def functionname():
    Statements
    Statements
    return statements

data = functionname()

//Program of Default With ReturnType and Without ReturnType
def add():
    a=int(input("enter first number"))
    b=int(input("enter second number"))
    c=a+b
    print(c)
def sub():
    a=int(input("enter first number"))
    b=int(input("enter second number"))
    c=a-b
    return c 

add()
res = sub()
print(res)


2 Parametrized:-

We can pass the value as a parameter from calling a function to called function.


2.1 Default  Argument:-

we will set a default value in "called argument", if we do not pass any value then the default value will be substituted.

def fun1(a=100,b=200):
   print(a+b)

fun1(10,20)
fun1(10)
fun1()
fun1(b=20)


2 Required Argument:-

We should pass an argument from  "Calling Function" to "Called Function" , if we do not pass then it will provide an error.

def functionname(a,b):
     print(a+b)

functionname(10,20)

Example:-

def fun1(a,b):
 print(a+b)

fun1(10,2)

3 Keyword-based Argument:-

We can pass the value as a "Calling argument name" from called function .argument name is called keyword.

def functionname(a,b):
   print(a/b)

functionname(b=2,a=3)

b=2,a=3 is keyword argument

4 variable-length Argument:-

it is also called, call by reference we will use a pointer as a reference variable under calling function.

mostly list and tuple object will be passed a variable-length argument.

def functionname(*a):
   print(a)

b=(10,20,30)
functionname(b)

by default variable-length argument store data using tuple object.

def functionname(b,*a):
   print(b,"\n",a)


functionname(10,20,30)




3 Lambda:-

It is used to declare a function in inline similar to the ternary statement of c and CPP language.

var = lambda argument:result

res =lambda a,b:a+b

res(10,20)





ASSIGNMENT:-

CREATE MULTI DIMENSION LIST USING PARAMETRISED KEYWORD BASED FUNCTION and display complete row which contains the largest element?

Find the Highest value foeach row element in a multi-dimension dictionary using the required parameters?

SWAP TWO NUMBER USING Variable-length arguments?

Calculate Addition of list using lambda function?








Post a Comment

0Comments

POST Answer of Questions and ASK to Doubt

Post a Comment (0)