Functions-UDF-2
Functions-UDF-2
Call statement –
# Now you can call printme function
UDF_Call (“Value passed to UDF")
UDF_Call("Again second call to the same function")
UDF
SIMPLE PROGRAM
INPUT
PROCESS
OUTPUT
def sum( a,b): def sum( a,b):
c=a+b c=a+b
print(“Sum = ") return (c)
print (c)
# Now you can call printme function # Now you can call printme function
a=int(input(“Enter 1st number”)) a=int(input(“Enter 1st number”))
b=int(input(“Enter 2st number”)) b=int(input(“Enter 2st number”))
sum(a,b) ans=sum(a,b)
print(“Sum = ")
print(ans)
def Calc_Avg( Total, Strength) :
avg=Total/Strength
return (avg)
FUNCTION
Built-in Functions
• Readymade Solution.
• In python Built-in functions are predefined, we
have to just call them to use.
• These make Python programming efficient and
provide a structure to language.
• Python supports various built-in functions,
which makes programming easy, fast and
efficient.
• These always reside in standard library and we
need not to import any module to use them.
Built-in Functions
1. Type Conversion Functions: Type Conversion
Functions are used to converts the values from one type to
another-
1. int( ) – To convert the string into integer.
2. float( ) – To covert string into float.
3. str( ) – To covert any value into string.
This gives the series from START to STOP – 1 and the interval
between two numbers of series will be STEP.
e.g.
list(range(1,5))
[1, 2, 3, 4]
for i in range(1,10) :
print(i)
PYTHON MODULES
• Module is a .py file which contains the
definitions of functions and variables.
• Module is a simple python file.
• When we divide a program into modules then
each module contains functions and variables.
And each functions is made for a special task.
• Once written code in modules can be used in
other programs.
• When we make such functions which may be
used in other programs also then we write them
in module.
• We can import those module in any program an
we can use the functions.
PYTHON MODULES
• Python provides two ways to import a
module –
• import statement : to import full module.
import math
n = 16
print(math.sqrt(a))
• from : To import all or selected functions from
the module.
from math import sqrt
n = 16
print(math.sqrt(a))
PYTHON MODULES
• In last slide’s example first the math.py file is
searched.
import math
n = 16
print(math.sqrt(a))
• random (): This generates floating value between 0 and 1. it does not
require any argument.
random Module
• randint (): This method takes 2 parameters a,b in which first one is lower
and second is upper limit. This may return any number between these two
numbers including both limits. This method is very useful for randon
selection type of guessing applications.
• uniform (): This method return any floating-point number between two
given numbers.
random Module
• choice (): this method is used for random selection from list, tuple or
string.
• shuffle (): this method can shuffle or swap the items of a given
list.
User-defined Functions
• These are the functions which are made by
user as per the requirement of the user.
• Function is a kind of collection of statements
which are written for a specific task.
• We can use them in any part of our program
by calling them.
• def keyword is used to make user defined
functions.
User-defined Functions
• We use following syntax with def keyword to
prepare a user defined function.
def Function_Name(List_Of_Parameters):
”””docstring”””
statement(s)
After the line containing def
there should be a 4 spaces
Keyword indentation, which is also
know as the body or block
of the function. Function
Definition
User-defined Functions without argument and
without return
Function
Definition
Function Call
User-defined Functions with argument and
without return
In pytho a function
mayn retur multiple
values.n In multiple
returning it returns a
sequence of values to
the calling statement.
These returned values
may be used as per
the requirement.
User-defined Functions with argument and
with return values
Built-in Namespace 4
Global Namespace 3
Enclosing Namespace 2
Local Namespace 1
Name Resolution Rule
• Case 1 : Variable in Global scope but not in local scope :
Global Namespace 3
n1=4 n2=5
Enclosing Namespace 2
Not any intermediate
enclosing variable
Local Namespace 1
x=4
y=5
s=9
Name Resolution Rule
• Case 2 : Variable neither in local scope nor in
global scope
• If there is a situation in which variable is neither in
its local environment nor in its parent environment?
Then Python will report an error.
Global Namespace 3
Not any Global variable
Local Namespace 1
Not any Local
variable
Name Resolution Rule
• Case 3 : Same variable in both LOCAL & GLOBAL
scope
• If both local and global variables having same
name. Here Local Variable hides the Global
Variable if control is in local environment.
Global Namespace 3
n = 10
Local Namespace
n=5
Name Resolution Rule
• What if GLOBAL variable is required in LOCAL
environment. Keyword global will be used in this
case.
Global Namespace 3
n = 10
Local Namespace
No Local variable
created
Food for Thought
1. Write a Function CountVowel( ) which takes string as
parameter count and return number of vowels (both
cases) .
2. WAF SumEven( ) which takes List Lst=[1,2,3,5,6,7,4,8]
find sum of all even number and return.
3. WAF Multiple5() which takes List Lst=[2,4,5,7,15,20]
find all multiple of 5 and return.
4. WAF PefSqr( ) which takes List of int as parameter find
sum of all perfect square integers.
5. WAF DiffChars( ) which takes string as parameter, find
number of different characters.
6. WAF Words( ) which takes string as parameter find
and display all words having 3 characters.
Using main() as a Function
• Main function is not necessary in python.
• We can use it by its name as –
Flow of Execution at the Time of Function Call
• The execution of any program starts from the very first line
and this execution goes line by line.
• One statement is executed at a time.
• Function doesn’t change the flow of any program.
• Statements of function doesn’t start executing until it is called.
• When function is called then the control is jumped into the
body of function.
• Then all the statements of the function gets executed from top
to bottom one by one.
• And again the control returns to the place where it was called.
• And in this sequence the python program gets executed.