Class XI · Chapter 7Unit 2, Python Programming (45 marks)5 min read
Chapter 7: Functions
CBSE Unit: Unit 2, Python Programming (45 marks) Marks Weightage: ~8-10 marks (functions are very heavily tested) Priority: CRITICAL, user-defined functions, scope, library functions
Key Concepts
7.1 Introduction to Functions, A function is a block of organised, reusable code that performs a single, related action, Benefits: code reusability, modularity, easier debugging, reduces code length
7.2 Types of Functions
- Built-in functions: Predefined in Python (e.g.,
print(),len(),type(),input()) - Functions from modules: Imported from library modules (e.g.,
math.sqrt()) - User-defined functions: Created by the programmer
7.3 User-Defined Functions
Defining a Function
def function_name(parameter1, parameter2, ...):
"""docstring (optional)"""
statement(s)
return value # optional
Calling a Function
result = function_name(argument1, argument2)
Example
def greet(name):
"""This function greets the person"""
print("Hello,", name)
greet("Amit") # Output: Hello, Amit
7.4 Function Parameters and Arguments
Parameters vs Arguments
- Parameters: Variables in function definition (formal parameters)
- Arguments: Values passed during function call (actual arguments)
Types of Arguments
(A) Positional Arguments: Matched by position
def subtract(a, b):
return a - b
subtract(10, 3) # a=10, b=3, result=7
(B) Default Arguments: Have default values
def greet(name, msg="Good morning"):
print("Hello", name, msg)
greet("Amit") # Uses default msg
greet("Amit", "Good night") # Overrides default
Rule: Default arguments must be at the end (after non-default arguments)
(C) Keyword Arguments: Identified by parameter name
def greet(name, msg):
print(name, msg)
greet(msg="Hello", name="Amit") # Order doesn't matter
7.5 The return Statement
def add(a, b):
return a + b
result = add(5, 3) # result = 8
- A function can return multiple values using a tuple:
def calc(a, b):
return a + b, a - b, a * b
s, d, p = calc(10, 5) # s=15, d=5, p=50
- If no
returnstatement, function returnsNone returnwithout a value also returnsNone
7.6 Scope of Variables
Local Variable, Defined inside a function, Accessible only within that function, Created when function is called, destroyed when function returns
Global Variable, Defined outside all functions, Accessible throughout the program (read only inside functions by default)
The global Keyword
x = 10 # global variable
def change():
global x # declares x as global
x = 20 # now modifies the global x
change()
print(x) # Output: 20
Without global keyword, assigning to x inside function creates a new local variable.
7.7 Python Standard Library Modules
math Module
import math
math.sqrt(25) # 5.0
math.ceil(4.2) # 5
math.floor(4.7) # 4
math.pow(2, 3) # 8.0
math.pi # 3.141592653589793
math.e # 2.718281828459045
math.factorial(5) # 120
math.fabs(-5) # 5.0
random Module
import random
random.random() # Random float between 0.0 and 1.0
random.randint(1, 10) # Random integer between 1 and 10 (inclusive)
random.randrange(1, 10) # Random integer between 1 and 9
random.choice([1,2,3]) # Random element from sequence
Importing Modules
import math # Import entire module
from math import sqrt # Import specific function
from math import * # Import all functions (not recommended)
import math as m # Import with alias
7.8 Function Composition
Using the return value of one function as an argument to another:
result = math.sqrt(math.pow(3, 2) + math.pow(4, 2)) # 5.0
Programs/Code Examples
Area of Circle
import math
def area_circle(radius):
return math.pi * radius ** 2
r = float(input("Enter radius: "))
print("Area =", area_circle(r))
Fibonacci Series
def fibonacci(n):
a, b = 0, 1
for i in range(n):
print(a, end=" ")
a, b = b, a + b
fibonacci(10)
Check Palindrome
def is_palindrome(string):
return string == string[::-1]
word = input("Enter word: ")
if is_palindrome(word):
print("Palindrome")
else:
print("Not palindrome")
Common Board Exam Question Patterns
- Find output of function-based code (3-4 marks): Trace through function calls
- Write a function for given task (3-5 marks): With parameters and return
- Scope of variable questions (2-3 marks): Local vs global, what gets printed
- Default argument questions (2 marks): Which value is used
- Keyword vs Positional arguments (2 marks)
- math/random module functions (1-2 marks): What does function return
- Return value questions (1-2 marks): What does function return, None if no return
Key Points Students Miss
- Default arguments must come AFTER non-default arguments in definition
- Function without
returnreturns None - Local variable with same name as global shadows the global (does not modify it)
- Must use
globalkeyword to modify a global variable inside a function math.sqrt()returns float, not int (math.sqrt(25)= 5.0, not 5)random.randint(a, b)includes BOTH a and b;random.randrange(a, b)excludes b- Parameters are in definition; Arguments are in function call
- A function can return multiple values (as tuple)
- Mutable objects (lists) passed to functions can be modified inside the function
import mathrequiresmath.sqrt();from math import sqrtallows justsqrt()
Prefer watching over reading?
Subscribe for free.