0% found this document useful (0 votes)
14 views9 pages

Class12 Functions

im aaaa good booooy

Uploaded by

elhan2k8
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)
14 views9 pages

Class12 Functions

im aaaa good booooy

Uploaded by

elhan2k8
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/ 9

M.E.

S INDIAN SCHOOL, DOHA - QATAR


Notes 2 2025- 2026

Section : Boys/Girls Date: 14-05-25


Class & Div. : XII (All Divisions) Subject: COMPUTER SCIENCE
Lesson / Topic: Unit 2 - WORKING WITH FUNCTIONS
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

FUNCTIONS

Function can be defined as a named group of instructions that are executed when the function is
invoked or called by its name.
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.
Functions is of mainly three types:

• Built-in Functions
• Modules
• User-Defined Functions
Built-in library functions

These are the functions which are predefined in python we have to just call them to use.
They always reside in standard library and we need not to import any module to use them.
len(), input(),int(), print(),str(),float(),eval(),min(),max(),abs(),type() ,round(),range() are few examples
of built-in function.
Modules

A Python module is a Python file containing a set of functions and variables to


be used in an application. Module is a simple python file.
Modules are usually stored in files with .py extension
Once written code in modules can be used in other programs.

F 061, Rev 01, dtd 10th March 2020 1|P age


How to import modules in Python?
There are two ways to import a module:
 using the import statement –imports the entire module.
 using the from...import statement –imports only individual module attributes.

Math Module

math module contains following functions–


ceil(x) returns integer bigger than x or x integer.
floor(x) returns integer smaller than x or x integer.
pow(x, n) returns x .
sqrt(x) returns square root of x.
log10(x) returns logarithm of x with base-10
cos(x) returns cosine of x in radians.
sin(x) returns sine of x in radians.
tan(x) returns tangent of x in radians.

String Module

We have already studied about string module in class XI. Here are
some other functions of string module.
Here are some other functions of string module.
String.capitalize() Converts first character to Capital Letter
String.find() Returns the Lowest Index of Substring
String.index() Returns Index of Substring

F 061, Rev 01, dtd 10th March 2020 2|P age


Python - Random Module

Functions in the random module depend on a pseudo-random number generator function random(),
which generates a random float number between 0.0 and 1.0.
random.random():
Generates a random float number between 0.0 to 1.0. The function doesn't need
any arguments. >>> import random
>>> random.random()
0.754363276054
random.randint():
Returns a random integer between the specified integers.
>>> import random
>>>random.randint(1,100)
95
>>>random.randint(1,100)
49
random.randrange():
Returns a randomly selected element from the range created by the start, stop and step arguments. The
value of start is 0 by default. Similarly, the value of step is 1 by default.
>>>random.randrange(1,10)
2
>>>random.randrange(1,10,2)
5
>>>random.randrange(0,101,10)
Program to select random subject from the list of subjects
import random
Subjects=[“CSC”,”IP”,”PHY”,”CHEM”]
Index=random.randrange(2)
print(Subjects[Index])
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.

F 061, Rev 01, dtd 10th March 2020 3|P age


def function_name(parameters):
"function docstring"
statement1
statement2
return [expr]
A function definition consists of the following components
1.Keyword def marks the start of function header.
2. A function name to uniquely identify it.
3. Function naming follows the same rules of writing identifiers in Python.
4. Parameters (arguments) through which we pass values to a function. They are optional.
5. A colon (:) to mark the end of function header.
6. One or more valid python statements that make up the function body.
7. Statements must have same indentation level.
8. An optional return statement to return a value from the function.

Types of user defined function

1. No Argument No return
2. With Argument No return
3. No Argument with return
4. With Argument with return
1. Functions with No Arguments and No Return Value These functions neither take inputs nor return
any values. They usually perform a task like printing or displaying results.
def greet():
print("Hello, world!")
greet()
2. Functions with Arguments but No Return Value
These functions take inputs (arguments) but don’t return anything.
def greet_user(name):

F 061, Rev 01, dtd 10th March 2020 4|P age


print(“Hello”, name)
greet(name)
3, Functions with Arguments and Return Value
def add(a, b):
a=4
b=6
s=add(a,b)
3.Functions with No Arguments and Return Value
def add():
a=5
b=7
return a + b
s=add()
Parameters and Arguments in Functions

Parameters are the value(s) provided in the parenthesis when we write function header.
An Argument is a value that is passed to the function when it is called.
For eg:
def f1(x,y) :
--------------------
---------------------
f1(20,30)
Here x and y are formal arguments where as 20,30 are actual arguments.
A functions has two types of parameters:
Formal Parameter: Formal parameters are written in the function prototype and
function header of the definition. Formal parameters are local variables which are
assigned values from the arguments when the function is called.
Actual Parameter: When a function is called, the values that are passed in the call are
called actual parameters. At the time of the call each actual parameter is assigned to the
corresponding formal parameter in the function definition.
FOUR TYPES OF ACTUAL ARGUMENTS IN PYTHON

1. Positional Arguments
2. Default Arguments
3. Keyword Arguments
4. Variable length Arguments

F 061, Rev 01, dtd 10th March 2020 5|P age


1.Positional Arguments: These arguments are passed to a function in correct positional order.

Eg:
def subtract (a, b):
print (a-b)
subtract (200,100)
output: 100
Passing arguments number and positions should be matched, if we change the order
result will be changed.
2. Default Arguments:T hese are the arguments through which we can provide default values to
the function.
Eg:
def sum(x=3, y=5): #here 3 and 5 are default arg:
z=x+y
return z
r=sum () # calling sum () with no arguments, so here 3, 5 will use.
print (r) # print result 8 (x=3, y=5)
r=sum(x=4) # calling sum () with 1 argument, so x=4 and y default.
print(r) # print result 9(x=4, y=5)
r=sum(y=45) # calling sum () with 1 argument, so y=45 and x default.
print(r) # print result 48(x=3, y=45)
#default value of x and y is being used when it is not passed
3. Keyword Arguments
If a function have many arguments and we want to change the sequence of them then we have to
use keyword arguments. Biggest benefit of keyword argument is that we need not to remember
the position of the argument.For this whenever we pass the values to the function then we pass the
values with the argument name. e.g.

F 061, Rev 01, dtd 10th March 2020 6|P age


4. Variable length Arguments
As we can assume by the name that we can pass any number of arguments according to the
requirement. Such arguments are known as variable length arguments.
We use (*) asterisk to give Variable length argument.

Scope of Variables
SCOPE means in which part(s) of the program, a particular piece of code or data is accessible or
known.
In Python there are broadly 2 kinds of Scopes:
¤ Global Scope
¤ Local Scope
Global Scope: A name declared in top level segment(__main__) of a program is said to have
global scope and can be used in entire program.
Variable defined outside all functions are global variables.
Local Scope: A name declare in a function body is said to have local scope i.e. it can be used only
within this function and the other block inside the function.
The formal parameters are also having local scope.

F 061, Rev 01, dtd 10th March 2020 7|P age


Lifetime of Variable:

For Global variables the lifetime is entire program run i.e. as long as program is executing.
For Local variables lifetime is their function‟s run i.e. as long as function is executing.
Name Resolution (Scope Resolution):
LEGB rule
LOCAL : first check whether name is in local environment
(ii) ENCLOSING ENVIRONMENT: if not in local, Python checks whether name is in Enclosing
Environment
(iii) GLOBAL ENVIRONMENT: if not in above scope Python checks it in Global environment
(iv) BUILT-IN ENVIRONMENT: if not in above scope, Python checks it in built-in environment,
if yes, Python uses its value otherwise Python would report the error

F 061, Rev 01, dtd 10th March 2020 8|P age


PASSING ARRAYS/LISTS TO FUNCTIONS
Passing a list as an argument actually passes a reference to the list, not a copy of the list.
Since lists are mutable, changes made to the elements referenced by the parameter change
the same list that the argument is referencing.
For example, the function below takes a list as an argument and displays each
element as a output:
def my_function(food):
for x in food:
print(x)
fruits = ["apple", "banana", "cherry"]
my_function(fruits)
Output:
apple
banana
cherry
For example, the function below takes a list as an argument and multiplies each
element in the list by 2:
def doubleList(aList):
for position in range(len(aList)):
aList[position] = 2 * aList[position]
things = [2, 5, 9]
print(things)
doubleList(things)
print(things)
Output
[2,5,9]
[4,10,18]

*****************The End********************

F 061, Rev 01, dtd 10th March 2020 9|P age

You might also like