User Defined Functions -Notes
User Defined Functions -Notes
INTRODUCTION
• These are the functions defined by the programmer.
• Functions that we define ourselves to do some special task are called user-defined
functions.
• It is created or defined by the def statement followed by the function name,
parenthesis() and colon(:).
• Syntax:
Output
I am learning Python Programming
a,b are variables, Here a+1 is an expression, Here 5,’b’ are literals,
Hence valid fn definition. Hence invalid fn definition. hence invalid fn definition.
HOW FUNCTIONS RETURN A VALUE
• Syntax:
def function_name(arguments):
return <value>
• Return statement: returns the value of the expression following return keyword.
• *It is used to end the execution of function call and specifies what value is to be returned back to calling
function.
• Function can take input values as parameters, execute them and return output to the calling function with a
return statement.
• Example:
def areaRectangle(length, breadth): Output
area= length * breadth 600
return area
Result= areaRectangle(30, 20)
print(Result)
HOW FUNCTIONS RETURN A VALUE CONTINUED…..
• If the return statement is absent and when we call a function, the default value None is
returned.
• Example:
def add(a,b): def add(a,b):
s=a+b s=a+b
result=add(10,20) return s
print(result) result=add(10,20)
print(result)
Output is None Output is 30
HOW FUNCTIONS RETURN MULTIPLE VALUES
• A function in Python can return multiple values.
• A return statement can return any number of values and the calling function receive the values in one of
the following ways:
1. By specifying same number of variables on the left-hand side of assignment operator in a
function call.
Example:
def calc( a,b):
add= a+b
sub= a-b
mul= a*b
return add, sub, mul
x, y,z= calc(200,180) Output
print(x, y,z) 380 20 36000
HOW FUNCTIONS RETURN A VALUE
2. By receiving the returned values in the form of a tuple.
• Example:
def calc( a,b):
add= a+b
sub= a-b
mul= a*b
return add, sub, mul
result= calc(200,180) Output
print(result) (380, 20, 36000)
TYPES OF USER-DEFINED FUNCTIONS