Chapter (7) Function

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Chapter (7)

Function
Function

A Function is a self-block of code. A Function can be called as a section of a program that is written
once and can be executed whenever required in the program, thus making code reusability.

Syntax:

def <Function Name>([parameters]):

Statements

Create the “Intro.py”

def Intro():

print (“Hi!!!”)

print ("We are Family of KMD Group of Company")

Intro()

Create the “Greeting.py”

def Greeting(name):

print ("Hello have a nice day ",name,"!!")

Greeting("Soe Mar Oo")

Greeting("Su Myat Noe")

Function Return

Return is used to return response to the caller function. Can use expression with the return
keyword.

Create the “GetNumber.py”

def Getnum():

num=input("Enter the number : ")

return num

def Sum(a,b):

return a+b

Python Programming Ch 7-1


KMD Computer Centre
n1=int(Getnum())

n2=int(Getnum())

n3=Sum(n1,n2)

print ("Total Sum = ",n3)

print ("Average = ",(n3/2))

PassingArgument Parameters

Python supports following

1) Positional argument (Required argument).

2) Default argument.

3) Tupel argument (No limit the argument)

1) Positional argument (Required argument)

When the function call statement must match the number and order of arguments as defined in the
function definition it is Positional Argument matching.

Create the “CentreInfo_Fun.py”

def CentreInfo(name,phone):

print name , " : " , phone

CentreInfo("Pansodan",245180)

CentreInfo("Myaynigone",502233)

2) Default argument

Default Argument is the argument which provides the default values to the parameters passed in the
function definition, in case value is not provided in the function call.

Create the “Contact.py”

def Contact(name,address="Yangon"):

print "Name = " , name

print "Address = " , address

print ""

Contact("Ma Htein")
Python Programming Ch 7-2
KMD Computer Centre
Contact("Mya Thandar","South Dagon")

3) Tupel argument (No limit argument)

When the function call give the arguments without limitation, need to accept with tupel object.*args
just means that the function takes a number of arguments, generally of the same type.

Create the “Course.py”

def Course(course,*syllabus):

print len(syllabus)," Syllabus in ", course ," Course."

print "They are ",syllabus,”\n”

Course("SE","SQL","C#","Java")

Course("PHP","HTML","CSS","JavaScript","JQuery","PHP","MySqL")

Lambda

Lambda function is used for creating small, one-time and anonymous function objects in Python.
Lambda operator can have any number of arguments, but it can have only one expression. It cannot
contain any statements and it returns a function object which can be assigned to any variable.

Syntax: lambda arguments: expression

Create the “Lambda.py”

print ("Simple Lambda")

L=(lambda:(2+3))

print(L)#Location on CPU

print(L())

print("_______________________________________")

Multi=(lambda s,g:s*g)

Multi(5,4)

Python Programming Ch 7-3

You might also like