1
2
FUNCTIO 3
NS
A function is a block of organized, reusable code that is
used to perform a single, related action
better modularity
High degree of code reusing.
Function 4
Syntax Input Parameter is placed within the
parenthesis() and also define
parameter inside the parenthesis.
The keyword
def
introduces a
function The code block
definition. within every
function starts
with a colon(:) .
Return statement exits a
function block. And we can also
use return with no argument.
5
Simple program to return the “ Hello
World “
def sabir(string) :
print (string)
return
Sabir(4)
Print(Sabir(3))
PASSING ARGUMENTS TO 6
In FUNCTIONS
programming, there are two ways in which arguments can be passed to functions :-
Pass by Value:
Function creates a copy of the variable(Object in Python) passed to it as an
argument.
The actual object is not affected.
Object is of immutable type,because immutable objects cannot be modified.
Pass by Reference:
The actual object is passed to the called function.
All the changes made to the object inside the function affect its original value.
Object is mutable type, as mutable objects can be changed, the passed objects
EXAMPLES OF PASSING IMMUTABLE
7
ARGUMENTS
OUTPUT: New memory address of ‘a’
inside the function.
Actual variable ‘a’ is not
changed.
EXAMPLES OF PASSING MUTABLE
8
ARGUMENTS
Value of my_list is
changed after
function call.
Address is same
9
FUNCTION ARGUMENTS
DEFAULT ARGUMENT 10
VALUES
The default value is evaluated only once.
EXAMPLE:
KEYWORD 11
ARGUMENTS
Keyword arguments are related to the function calls.
Example:
Caller identifies the keyword
argument by the parameter name.
Calling of the argument Order
doesn’t matter .
Output:
KEYWORD 12
Example 1:
ARGUMENTS
Possbile Error:
ARBITRARY ARGUMENT 13
LISTS
Variable-Length Arguments
The syntax of such arguments?
This tuple remains empty if no additional arguments are specified during
the function call.
Syntax :
def sabir( marks, *values) : This is called
print(marks) arbitrary argument
print(marks , values) and asterisk sign
is placed before
the variable name.
Sabir(23)
Sabir(23,20,2,3,4,5)
DOCUMENTATION 14
STRINGS
‘’’ Hello my name is Muhammad sabir and I am teaching the
python to the bscs-2017-2021n “””
‘.
15
Lambda Functions
# Program to show the use of 16
lambda functions
Filter ()
my_list = [1, 5, 4, 6, 8, 11, 3, 12]
new_list = list(filter(lambda x: (x%2 == 0) , my_list))
print(new_list)
# Output: [4, 6, 8, 12]
17
Map ()
my_list = [1, 5, 4, 6, 8, 11, 3, 12]
new_list = list(map(lambda x: x * 2 , my_list))
print(new_list)
# Output: [2, 10, 8, 12, 16, 22, 6, 24]
18
Files In Python
R
A
T
W
X
B
+
19
OOP
20
21
22
23
24
25
26
27
28
#definig the class
class Sabir: 29
def __init__(self, Name, RollNumber,Session,Subject):
[Link]=Name
[Link]=RollNumber
[Link]=Session
[Link]=Subject
Name='Sabir'
RollNumber='BSCS_17_04'
Session= '2017_2021'
Subject='Python_programming'
s1= Sabir(Name,RollNumber,Session,Subject)
s2= Sabir(Name,RollNumber,Session,Subject)
print([Link])
print([Link])
print([Link])
30
OOP Vs Functional
Programming
31