0% found this document useful (0 votes)
42 views

User Defined Function Part I

The document discusses user defined functions in Python. It defines a function as a set of statements that perform a specific task. Functions allow code to be reused and make programs more organized and readable. Some key points made include: - Functions can make development faster by dividing work among team members. - Functions increase code reusability and readability by encapsulating reusable blocks of code. - Functions are defined using the def keyword followed by the function name and parameters. - Functions can return single or multiple values. Multiple values are returned as a tuple. - The actual arguments passed in a function call must match the number and type of the formal parameters defined in the function header.

Uploaded by

HITESH PUJARI
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views

User Defined Function Part I

The document discusses user defined functions in Python. It defines a function as a set of statements that perform a specific task. Functions allow code to be reused and make programs more organized and readable. Some key points made include: - Functions can make development faster by dividing work among team members. - Functions increase code reusability and readability by encapsulating reusable blocks of code. - Functions are defined using the def keyword followed by the function name and parameters. - Functions can return single or multiple values. Multiple values are returned as a tuple. - The actual arguments passed in a function call must match the number and type of the formal parameters defined in the function header.

Uploaded by

HITESH PUJARI
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

USER DEFINED FUNCTION

PART-I
User Defined Function

• A function is a set of statement that performs a specific

task.
• Function allows us to use a piece of code repeatedly in
different parts of a program.

• Functions are also known as routine, methods or

procedures.
Advantages of Function
1.Program development made easy and fast : Work can be divided
among project members thus implementation can be completed fast.
2.Program testing becomes easy : Easy to locate and isolate a faulty
function for further investigation.
3.Code sharing becomes possible : A function may be used later by
many other programs this means that a python programmer can use
function written by others.
4.Code re-usability increases : A function can be used to keep away
from rewriting the same block of codes which we are going to use two
or more locations in a program.
5.Increases program readability : It makes the program more readable
and the length of the source program can be reduced by using functions
at appropriate places.
How to define and Call a function
A user defined function is defined using def keyword followed
by the function name and parentheses() as shown in the given
image.

def function_name(comma separated list of parameters):


statements
statements

Note:
• Keyword def starts the function header.
• Function name must be unique and function naming follows the same
rule as rules for defining identifiers in Python.
• Parameters are optional and must be separated by comma.
• A colon(:) to mark the end of function header.
• All the statements must have same indentation.
Program: 1
Define a function which does not return any value

def myfunc1( ):
print(“DPS Ranipur Haridwar”) Function Definition
print(“Uttarakhand”)

myfunc1() # Function Call


Output:
DPS Ranipur Haridwar
Uttarakhand
Note:
The function defined above is also called void function because it is not returning
any value.
Program: 2
Define a function to print right angle triange of star(astriks)

def triangle():
print(“*”)
print(“* *”)
print(“* * *”)
print(“* * * *”)
triangle()

Output:
*
**
***
****
How a Function return a value
• The following statement is used to return a value :
return <value>
• The return statement in Python specify that what value is to
be return back to the calling function.
Program:3
Define a function to find the area of rectangle
def AreaRectangle(a,b):
ar = a*b
return ar
r = AreaRectangle(4,3)
print(“Area:”,r)

Output:
Area:12
Program: 4
Define a function to find the factorial of a number entered by the user
def factorial(n):
f=1
while(n>=1):
f = f*n
n=n-1
return f
num=int(input("Enter any number:"))
t = factorial(num)
print("Factorial of entered number:",t)

Output:
Enter any number:4
Factorial of entered number: 24
Function returning multiple values
• Unlike other high level language like C, C++, java in which a function can
return at most one value at a time, while in Python a function can return
multiple values.
• The return statement can return any number of values.
• Function return multiple values in form of tuple.
Program:5
Define a function to illustrate return statement returning multiple values
def Add_Diff(a,b):
ad = a + b
df = a – b
return ad,df
x,y = Add_Diff(5,3)
print(“Sum = ”,x)
print(“Subtraction = ”,y)

Output:
Sum=8
Subtraction=2
Function returning multiple values……
Program:6
Define a function returning multiple values in form of tuple
def Calculation(x,y):
a=x+y
s=x–y
m=x*y
d=x/y
return a,s,m,d
result = Calculation(6,2) # Here result becomes tuple i.e. (8,4,12,3)
print(“Results are: ”)
for i in result:
print(i)

Output:
Results are:
8
4
12
3
Actual and Formal Arguments/parameters
• Actual Arguments: These are the values provided in function call
statement.
• Formal Arguments: These are the variables provided in the
parentheses when we write the function header.
example:
def AreaRectangle(a,b):
ar = a*b
return ar
r = AreaRectangle(4,3)
print(“Area:”,r)
Note:
• In above program a and b are the Formal arguments while 4 and 3 are the
Actual arguments.
• The Actual arguments can be : Literal or variable or expression.
• The Formal arguments/Parameters must be variables because these are used
to hold the values.
Identify which of these are invalid function definition:
1. def compute(a,b):
print(a+b)

2. def compute(a+1,b):
print(a*b)

3. def compute(2,’b’):
print(a*b)
Identify which of these are invalid function definition:
1. def compute(a,b):
print(a+b)

2. def compute(a+1,b):
print(a*b)

3. def compute(2,’b’):
print(a*b)

Ans:
1. Valid
2. Invalid. Because a+1 can not be used as parameter/Formal
Argument. This is a expression.
3. Invalid. Because 2 and ‘b’ are literals and can not be used as
formal parameters.
Thank You

You might also like