Java, .NET, PHP, PYTHON, ANGULAR, .NET, ML, Data Scince, 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 proper structure to python code.
the function will subdivide the code in a single file which 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()


Type of function:-

1)  Default:- We can not pass input parameters in default function ,input variable will be define under function block or globally.


1.1 With Return Type  :-   We can return value from function using this.
def functionname():
     statements
     return value


1.2 Without Return Type :-  

We can not return value or output ,it will be defined under function block.
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)


2) Parametrised  :-  

Parameter is used to pass input value from calling function to called function ,python provide different type of parameters based on requirement.


2.1)  required   :-  

it should take all input value from calling argument to called argument.if we not pass all values then it will provide 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:- 

 using this type of function we will create keyword which will contain the value of calling arguments to called the 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  :-  

using this function we can set 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 :- 

 We can pass multiple parameters as a tuple arguments 
def functionname(*args):
    Statements
    Statements
functionname(10,20,30,40,...)
We can value till nth parameters


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 contain single statement
var = lambda arg1,arg2:statement

1) WAP to create marksheet program using function take dictionary as a input variable
2)  Create ATM Program using function ?


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