0% found this document useful (0 votes)
5 views

Functions in Python_250426_235532

A function is a programming block that performs a specific task and can accept parameters and return results. There are three types of functions: built-in functions, functions inside modules, and user-defined functions, each serving different purposes. Additionally, Python supports various types of arguments, including positional, default, keyword, and variable length arguments, and emphasizes the importance of variable scope and name resolution.

Uploaded by

animashdasrupak
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)
5 views

Functions in Python_250426_235532

A function is a programming block that performs a specific task and can accept parameters and return results. There are three types of functions: built-in functions, functions inside modules, and user-defined functions, each serving different purposes. Additionally, Python supports various types of arguments, including positional, default, keyword, and variable length arguments, and emphasizes the importance of variable scope and name resolution.

Uploaded by

animashdasrupak
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
You are on page 1/ 10

What is Function?

A function is a programming block of codes which is used to perform a single,


related task. It only runs when it is called. We can pass data, known as
parameters, into a function. A function can return data as a result.

Advantages of using functions:

1. Program development made easy and fast


2. Program testing becomes easy
3. Code sharing becomes possible
4. Code re-usability increases
5. Increases program readability
6. Function facilitates procedural abstraction
7. Functions facilitate the factoring of code

TYPES OF FUNCTIONS
Functions can be categorized into three types:
1) Built in Functions.
2) Modules.
3) User Defined functions.

1) BUILT IN FUNCTIONS
These are predefined function in python and are used as and when there is need
by simply calling them. For example:

int()
float()
str()
min()
max() ...etc

Mrinmoy Paul (PGT-CS/IP)


2) FUNCTIONS INSIDE MODULES
Module is a container of functions, variables, constants, class in a separate file
which can be reused.
IMPORTING MODULES:
i) import statement
ii) from statement
IMPORTING MODULES – import STATEMENT
i) import statement : used to import entire module.

Syntax: import modulename

example: import math

ii) from: import all functions or selected one.


Syntax:
from module name import function name

for example:
from random import randint
3) USER DEFINED FUNCIONS
Function is a set of statements that performs specific task.

Syntax of user defined function

def FunctionName(list of parameters)


................
................
Statements

Example:
def sum_diff(x,y):
add=x+y

Mrinmoy Paul (PGT-CS/IP)


diff=x-y
return add,diff

def main():
x=9
y=3
a,b=sum_diff(x,y)
print("Sum = ",a)
print("diff = ",b)
main()

Python Lambda
A lambda function is a small anonymous function which can take any number of
arguments, but can only have one expression.
E.g.
x = lambda a, b : a * b
print(x(5, 6))
OUTPUT: 30

PARAMETERS AND ARGUMENTS IN FUNCTION


 Parameters are the values which are provided at the time of function
definition. Parameter is also called as formal parameter or formal
argument.

def sum_diff(p,q):
add=p+q
diff=p-q
return add,diff

 Arguments are the values which are passed while calling a function
Mrinmoy Paul (PGT-CS/IP)
def main():
x=9
y=3
a,b=sum_diff(x,y)
print("Sum = ",a)
print("diff = ",b)
main()

TYPES OF ARGUMENTS
Python supports following type of arguments:
1. Positional arguments
2. Default Arguments
3. Keyword Arguments
4. Variable Length Arguments

1. POSITIONAL ARGUMENTS
These are the arguments passed to a function in correct positional order
For example:
def substract(a,b):
print(a-b)
>>>substract(100,200)
-100
>>>substract(150,100)
50

2. DEFAULT ARGUMENTS
When a function call is made without arguments, the function has defalut values
for it for example:
For example:
Mrinmoy Paul (PGT-CS/IP)
def greet_msg(name=“Mohan”):
print(“Hello “, name)
greet_msg()
greet_msg(“Vinay”) # valid
greet_msg() #valid

3. KEYWORD ARGUMENTS
If function containing many arguments, and we wish to specify some among
them, then value for such parameter can be provided by using the name.
For example:
def greet_msg(name,msg):
print(name,msg)
greet_msg()
#calling function
greet_msg(name=“Mohan”,msg=“Hi”): # valid
O/p -> Mohan Hi

4. VARIABLE LENGTH ARGUMENTS


In some situation one needs to pass as many as argument to a function, python
provides a way to pass number of argument to a function, such type of
arguments are called variable length arguments.
Variable length arguments are defined with * symbol.
For Example:
def sum(*n):
total=0
for i in n:
total+=i
Mrinmoy Paul (PGT-CS/IP)
print(“Sum = “, total)
sum()
# Calling function
sum() o/p sum=0
sum(10) o/p sum=10
sum(10,20,30,40) o/p sum=100

PASSING ARRAYS/LISTS TO FUNCTIONS


Arrays in basic python are lists that contain mixed data types and can be passed
as an argument to a function.
For Example: # Arithmetic mean of list
def list_avg(lst):
l=len(lst)
sum=0
for i in lst:
sum+=i
return sum/l

def main():
print(“Input integers”)
a=input()
a=a.split()
for i in range(len(a) ):
a[i]=int(a[i])
avg=list_ave(a)
print (“Average is = “, avg)

Mrinmoy Paul (PGT-CS/IP)


SCOPE OF VARIABLES
Scope mean measure of access of variable or constants in a program. Generally
there are two types of scope of variables:
i) Global (Module)
ii) Local (Function)
i) Global variables are accessed throughout the program their scope is global
For Example:
m=100
def add_diff(x,y):
add=x+y
diff=x-y
global m
m= m +10
print("m in add_diff function=",m)
return add,diff

def main():
x=9
y=3
a,b=add_diff(x,y)
print("Sum = ",a)
print("diff = ",b)
print("m in main function = ",m)

main()

Mrinmoy Paul (PGT-CS/IP)


NAME RESOLUTION (Resolving Scope of a variable/name):
LEGB RULE
For every name reference or a variable within a program i.e. when we access
a variable from within that function, Python follows the LEGB Name
Resolution Rule to resolve:
i) Python Checks within its LOCAL Environment if it has the same name ; if
Yes, Python uses its value. If Not, then it moves to step (ii).
ii) Python Checks the ENCLOSING Environment if it has the same name;
if Yes, Python uses its value. If Not, then it moves to step (iii).
iii) Python Checks within its GLOBAL Environment if it has the same name;
if Yes, Python uses its value. If Not, then it moves to step (iv).
iv) Python Checks within its BUILT-IN Environment if it has the same name;
if Yes, Python uses its value.
v) If Not, then Python repots ERROR.

Mrinmoy Paul (PGT-CS/IP)


Mrinmoy Paul (PGT-CS/IP)
Mrinmoy Paul (PGT-CS/IP)

You might also like