USER DEFINED FUNCTIONS
HTML
XML
VOIP
FTP
HTTP
SIP
POP
SMTP
IMAP
SMS
A function is a subprogram that acts on data and often returns a value
Consider the following example
f(x) = 2x
def calc(x) :
a=2*x
return a
Creating a Function��
Calling a Function�
User Defined Functions
Calling and using Function
Y=cube(4)
print(Y)
print(cube(4))
The following function can be called in one of the following way:
cube()
ii) Passing variable as argument in function call
num=10
cube(num)
iii) Taking input and Passing the input as argument in function call
num=int(input(‘Enter the number’))
cube(num)
cube(5)
The following function can be called in one of the following way:
iv) using function call inside another statement
print(cube(5))
v) Using function call inside expression
ans=2*cube(4)
Case 1
Case 2
Case 3�Passing parameter and no return type
Case 3-Taking input and Passing the input as argument in function call�Passing parameter and no return type
Case 4�Passing parameter and return type
Case 4�function used in expression
How to call the function greet?
greet()
How to call the function sum?
a=sum(4,6)
x,y=5,6
a=sum(x,y)
Write the order in which the statement gets executed.
2
3
4
5
1
6
Arguments�
def my_function(fname):� print(fname + " Hello")�
#main �my_function(“Sarah")�my_function(“Toby")�my_function(“Mick")
Sarah Hello
Toby Hello
Mick Hello
4. Write a Program using functions to print sum of the series 13+23+33+43+53+………N3 where a function cube_no(a) is called each time to find the cube of a number
Write a menu driven pgm with each options as functions
STRUCTURE OF A PYTHON PROGRAM
def fun1():
:
def fun2():
:
#top-level statements here
stmt1
stmt2
Flow of execution in a function call
Parameters or Arguments?�
Number of Arguments�
Example�
�
This function expects 2 arguments, but gets only 1:�
Passing parameters
Passing parameters
Python supports three types of formal argument/parameter
1. Positional arguments (Required arguments)
def check(a,b,c):
:
check(x,y,z) # 3 values (all variables) passed
check(2,x,y) #3 values(1 literal+2var) passed
check(2,3,5) #3 values(3 literals) passed
2. Default arguments
Default Parameter Value�
def interest(prin , time, rate =0.10) # legal
def interest(prin , time=2, rate =0.10) #legal
def interest(prin , time=2, rate )
#illegal ( default parameter before required parameter )
def interest(prin=100 , time, rate =0.10) #illegal
def interest(prin=100 , time=2, rate =0.10) #legal
3. Keyword Arguments
�
3. Keyword Arguments
Passing a List as an Argument�
You can send any data types of argument to a function (string, number, list, dictionary etc.), and it will be treated as the same data type inside the function.
E.g. if you send a List as an argument, it will still be a List when it reaches the function:
Return Values�
Return Values�
15
25
The pass Statement�
Function can return a multiple values
return <value1/var1>, <value2/var2>, etc
ii) The function call statement should receive or use the returned values in one of the following ways:
a) Either receive the returned values in the form a tuple variable
def square(x,y,z):
return x*x,y*y,z*z
#__main__
t=square(2,3,4)
print(t)
(4,9,16)
b) You can directly unpack the received values of tuple by specifying the same number of variables on the left- hand side of the assignment in function call
def square(x,y,z):
return x*x,y*y,z*z
#__main__
a,b,c=square(2,3,4)
print(a,”@”,b,”@”,c)
4@9@16
SCOPE OF VARIABLES
Global variable
Local Scope
Global variables:
Num1,num2,s
Local variables:
x,y,z
x,y,z& sm is local
a,b,c & s is local
Num1, num2, num3 are Global
NAME RESOLUTION�(Resolving scope of a Name)
For every name reference within a program i.e, when you access a variable from within a program or function, Python follows name resolution rule also known as LEGB RULE.
Python does the following to resolve it:
i) It checks within its Local environment (LEGB), If it has a variable within the same name , if yes, Python uses its value�if not, then it moves to step (ii)
ii) Python now checks for the enclosing environment(LEGB) i.e whether there is a variable modified in the enclosing body.
If not them moves to the next step(iii)
Enclosing (or nonlocal) scope is a special scope that only exists for nested functions. If the local scope is an inner or nested function, then the enclosing scope is the scope of the outer or enclosing function. This scope contains the names that you define in the enclosing function.
iii) Python now checks for the Global environment(LEGB).
If not them moves to the next step(iv)
iv) Python now checks for the Built in environment(LEGB).
If none of the steps finds a variable then it will gibe a
Name error
Variable not found
Let us consider some examples
Case 1: Variable in Global Scope but not in Local Scope
Case 2: Variable neither in Global Scope or Local Scope
Case 3: Some Variable name in local Scope as well as Global Scope
What if you want to use the global variable inside local scope?
MUTABLE/ IMMUTABLE PROPERTIES OF PASSED DATA OBJECTS
PROGRAMS
5. W.A.P using function to find the average of 3 given numbers and return the average
6. W.A.P using function to accept a number from the user and find the factorial of a number.
7. W.A.P using function to accept a number from the user and returns 1 if the number is positive else 0 if the number is negative.
8. W.A.P using function to accept a number from the user and print the stars in the ascending order
*
**
***
9. W.A.P using function to accept a number N from the user and find the Fibonacci series till the term N.
10. W.A.P using function to accept a string from the user and pass each character into the function and count the number of digits in the string.
11. W.A.P using function to enter the mark of a student return the grade back.
(100-90 ‘A’,
75-90 ‘B’,
50-75 ‘C’,
ELSE ‘D’ )
14. Using function that takes one char as parameter and prints out the next five character separated by #
15. Program that invokes a function calc which take 2 integers and an arithmetic operator as parameter and prints the corresponding result.
15. Program that invokes a function calc which take 2 integers and an arithmetic operator as parameter and prints the corresponding result.
16. Function called pall which accepts the number as parameter (no return type) and print if it is a palindrome or not.
17. Using function call convert that accepts an alphabet as a parameter and return the upper case or the lower-case value.