0% found this document useful (0 votes)
9 views35 pages

Functions in Python

The document provides an overview of Python functions, including their definition, types (built-in and user-defined), and advantages such as code reusability and readability. It explains function parameters, arguments, and various types of arguments like positional, keyword, and default arguments, along with examples. Additionally, it includes sample function implementations and outlines key concepts such as function return values and the behavior of mutable and immutable types.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views35 pages

Functions in Python

The document provides an overview of Python functions, including their definition, types (built-in and user-defined), and advantages such as code reusability and readability. It explains function parameters, arguments, and various types of arguments like positional, keyword, and default arguments, along with examples. Additionally, it includes sample function implementations and outlines key concepts such as function return values and the behavior of mutable and immutable types.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

s = “Microsoft”

x = len(s)
p = [Link](‘o’)
n = [Link]()
FUNCTIONS
Python Functions
A function is a block of reusable code that performs a
specific task.
•It helps in organizing code, making it more readable,
and avoiding repetition.
•It is identified by a unique name.
•A function can be executed by calling it.
•Writing the name of the function will call a function.
TYPES OF FUNCTIONS

(i) Built-in function ( all functions defined by


Python min() max() , len() etc.

(ii) User-defined functions ( defined by the


user )
Advantage
(i) Reduces the size of the program
(ii) Increases readability
(iii)Improves reusability of code
Keywords associated with Functions
def :- def keyword declares a user defined function
followed by parameters and terminated with a
colon.
return :- return keyword returns the control back to
its caller along with some value if passed
explicitly. Writing return is not compulsory and
we can write as many return keywords as needed
but only one return keyword is executed.
Function Declaration
Function declaration & definition
def display():
print("Welcome to Python programming")

#main

display()
Parameters / Arguments
Parameters :- The variables declared in the header
part of the function is called parameters or the
values received by the functions from its caller.

Arguments:- When we call a function and pass


some values to the function. These passed values
are called arguments.
Example of parameters & arguments
def sum(a, b):
c=a+b
print("Sum=",c)
return

#main
x, y = 10, 20
sum(x, y)
Example of function returning value
def amount (p, r, t):
i = p*r*t/100
a=p+i
return a

#main
t = amount(5000, 6, 3)
print(t)
Write a function findbig that takes 2 integers as
arguments and returns the largest value.
def findbig(a, b):
if a>b:
return a
#main
else:
x, y = 15, 10
return b
z = findbig(x, y)
print("Largest =",z)
1. Write a function findsum that takes an integer
as argument and returns sum of the digits.
2. Write a function isprime that takes an integer
as argument and returns true if it is a prime
number and false otherwise.
3. Write a function ispalindrome that takes a
string as argument and returns true if it is a
palindrome string and false otherwise.
4. Write a function findsum that takes the number of
terms and the value of x as argument and returns sum
of the given expression-
x4 x6 x8
S    ... n terms
(3!) (5!) (7!)

5. Write a function show that takes an integer and a


string as argument and display it as the given pattern
if show(3, “@”) it displays –
@@@
@@
@
6. Write a function vowels() that takes a string as
argument and returns the number of vowels.
7. Write a function words() that takes a string as
argument and returns the number of words
present in it.
8. Write a function findpower() that takes two
numbers as arguments and returns the power
of the first argument by the second argument.
Function Arguments
A function by using the following types of formal
arguments::
•Positional / Required arguments
•Keyword arguments
•Default arguments
Positional / Required arguments
•the arguments must be provided for all
parameters (required)
•the values of arguments are matched with
parameters, position (order) wise (Positional)
Positional / Required arguments
def amount(p, r, t):
i = p*r*t/100
a=p+i
return a
a, b, c = 2000, 6, 3
x = amount(a, b, c)
y = amount(b, c, a)
z = amount(5000, b, 5)
Keyword Arguments
•Keyword arguments are related to the function
calls. When you use keyword arguments in a
function call, the caller identifies the arguments by
the parameter name.
•This allows you to skip arguments or place them
out of order because the Python interpreter is able
to use the keywords provided to match the values
with parameters.
Keyword arguments
def printme( str ):
print str
return
printme( str = "My string")
This would produce following result:
My string
Keyword arguments
def printinfo( name, age ):
print ("Name: ", name)
print ("Age: ", age)
return

printinfo( age=50, name="mickey" )

This would produce following result:


Name: mickey
Age: 50
Default Arguments
•A default argument is an argument that assumes a
default value if a value is not provided in the
function call for that argument.
Default arguments
def printinfo( name, age = 35 ):
print ("Name: ", name)
print ("Age: ", age)
return
printinfo( age=50, name="mickey" )
printinfo( name="mickey" )
This would produce following result:
Name: mickey
Age: 50
Name: mickey
Age: 35
Default arguments
def interest( principal, rate, time=3 ):
i = principal * rate * time / 100
return i

interest( 2000, 7, 5)
interest( 2000, 7 )

In a function header, any parameter cannot have a default value


unless all parameters appearing on its right have their default values.
Default arguments
def interest( principal, rate=5, time ):

def interest( principal=5000,rate=5, time ):

The above functions will produce an error.

def interest( principal, rate=5, time=3 ):


def interest( principal=5000,rate=5,time=3):

These functions are legal.


Functions returning values
def amount (p, r, t):
i = p*r*t/100
a=p+i
return a

t = amount(5000, 6, 3)
print(t) output: 5900
Functions returning no values (void functions)
def amount (p, r, t):
i = p*r*t/100
a=p+i
print(a)
return

amount(5000, 6, 3)
Functions returning multiple values
def amount (p, r, t):
i = p*r*t/100
a=p+i
return i, a
t = amount(5000, 6, 3)
print(t) output: (900, 5900)
x, y = amount(5000, 6, 3)
print(x, y) output: 900, 5900
Functions calling (Parameters)
def change(a):
a=a+3
print(a)

b=5
change(b)
print(b)
Functions calling (Parameters)
def change(list1):
list1[0] = 10
print(list1)

list2 = [5,2]
change(list2)
print(list2)
Functions calling (Parameters)
def change(list1):
list2 = [6,8]
list1 = list2
print(list1)

list3 = [5,2]
change(list3)
print(list3)
Points to remember
•Changes in immutable types are not reflected in
the caller function at all.
•Changes, if any, in mutable types –
•are reflected in caller function if its name is not
assigned a different variable or datatype.
•are not reflected in the caller function, if it is
assigned a different variable or datatype
•Write a function FOUR(L), where L is the list of elements (list of words) passed as
argument to the function. The function returns another list named ‘wordList’ that
stores all four lettered word of L.
For example:
If L contains [“DINESH”, “RAMESH”, “AMAN”, “SURESH”, “KARN”]
The wordList will have [AMAN, KARN]

•Write a function INDEX_LIST(L), where L is the list of elements passed as


argument to the function. The function returns another list named ‘indexList’ that
stores the indices of all Non-Zero Elements of L.
For example:
If L contains [12,4,0,11,0,56]
The indexList will have - [0,1,3,5]
• Write a function in python named SwapHalfList(Array), which accepts a list Array
of numbers and swaps the elements of 1st Half of the list with the 2nd Half of the
list, ONLY if the sum of 1st Half is greater than 2nd Half of the list.
Sample Input Data of the list
Array= [ 100, 200, 300, 40, 50, 60],
Output Array = [40, 50, 60, 100, 200, 300]

•Write a function LeftShift(Numlist, n) in Python, which accepts a list Numlist of


numbers and n is a numeric value by which all elements of the list are shifted to
left.
Sample input data of the list Numlist= [10, 20, 30, 40, 50, 60, 70], n=2
Output
Numlist-[30, 40, 50, 60, 70, 10, 20]
• Write a function in python named VowelWords(Array), which accepts a list Array
of words and returns a new list with the words containing vowels.
Sample Input Data of the list
Array= [ “Fly”, “Fruits”, “Fry”, “Spy”, “Mango”],
Output Array = [“Fruits”, “Mango”]

•Write a function noPalindrome(Numlist) in Python, which accepts a list Numlist


of numbers and returns a new list without palindromes.
Sample input data of the list Numlist= [10, 221, 333, 252, 60, 171]
Output
Numlist-[10, 221, 60]

You might also like