Class XI · Chapter 7Unit 2, Python Programming (45 marks)5 min read
Share:WhatsAppLinkedIn

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

  1. Built-in functions: Predefined in Python (e.g., print(), len(), type(), input())
  2. Functions from modules: Imported from library modules (e.g., math.sqrt())
  3. 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 return statement, function returns None
  • return without a value also returns None

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

  1. Find output of function-based code (3-4 marks): Trace through function calls
  2. Write a function for given task (3-5 marks): With parameters and return
  3. Scope of variable questions (2-3 marks): Local vs global, what gets printed
  4. Default argument questions (2 marks): Which value is used
  5. Keyword vs Positional arguments (2 marks)
  6. math/random module functions (1-2 marks): What does function return
  7. Return value questions (1-2 marks): What does function return, None if no return

Key Points Students Miss

  1. Default arguments must come AFTER non-default arguments in definition
  2. Function without return returns None
  3. Local variable with same name as global shadows the global (does not modify it)
  4. Must use global keyword to modify a global variable inside a function
  5. math.sqrt() returns float, not int (math.sqrt(25) = 5.0, not 5)
  6. random.randint(a, b) includes BOTH a and b; random.randrange(a, b) excludes b
  7. Parameters are in definition; Arguments are in function call
  8. A function can return multiple values (as tuple)
  9. Mutable objects (lists) passed to functions can be modified inside the function
  10. import math requires math.sqrt(); from math import sqrt allows just sqrt()

Test Your Knowledge

Take a quick quiz on this chapter

Start Quiz →

Prefer watching over reading?

Subscribe for free.

Subscribe on YouTube