Class 12 Computer Science CBSE Format

CBSE Class 12 Computer Science Python Functions Complete Notes

Updated for 2025–2026 Board Pattern · 7 Views

CBSE Class 12 Computer Science Python Functions Complete Notes for 2025–2026 Board Exams

In the CBSE Class 12 Computer Science syllabus for 2025–2026, understanding Python Functions is essential for scoring high marks in Unit 1: Computational Thinking and Programming - 2. Questions on functions appear across multiple sections of the board exam 12 paper, including 1-mark MCQs, 2-mark output/error-finding questions, and 3-mark programming questions. These CBSE Class 12 Computer Science Python Functions Complete Notes provide an NCERT-aligned, comprehensive review of all definitions, syntax rules, argument types, scope rules, and solved board-style questions.

Key Concepts

A function in Python is a named, organized, and reusable block of code designed to perform a single, specific action. Functions reduce redundancy, divide complex problems into manageable sub-tasks (modularity), and make programs easier to test and debug.

1. Types of Functions in Python

The CBSE Computer Science curriculum categorizes Python functions into three distinct groups:

  • Built-in Functions: Predefined functions that are always available in the Python standard environment without importing any module. Examples include len(), type(), int(), float(), input(), print(), range(), id(), ord(), and chr().
  • Functions Defined in Modules: Functions stored inside external Python modules that must be imported before use using the import statement. Examples include math.sqrt(), math.pow(), math.ceil() from the math module, and random.random(), random.randint(), random.randrange() from the random module.
  • User-Defined Functions: Custom functions created by the programmer using the def keyword to meet specific application requirements.

2. Anatomy of a User-Defined Function

To define and invoke a user-defined function in Python, follow the standard syntax below:

def function_name(parameter_1, parameter_2, ...):
    """Optional Docstring describing the function"""
    # Function Body (Indented statements)
    statement_1
    statement_2
    return [expression]

Key structural components include:

  • def Keyword: Marks the beginning of the function header.
  • Function Name: A valid Python identifier following standard naming rules.
  • Parameters: Variables listed inside parentheses in the function header that receive values when the function is called.
  • Colon (:): Terminates the function header and opens the indented block.
  • Function Body: Indented block of statements executed when the function is called.
  • return Statement: Optional statement used to exit a function and pass a result back to the caller. A function without an explicit return statement implicitly returns None. If multiple comma-separated values are returned (e.g., return a, b), Python packs them into a tuple.

3. Parameters vs. Arguments

Parameter (Formal Parameter) Argument (Actual Parameter)
Variables declared in the function header definition. Values, variables, or expressions passed to the function in the function call statement.
Example: def add(x, y):x and y are formal parameters. Example: add(10, 20)10 and 20 are actual arguments.

4. Types of Arguments in Python

Python supports three primary ways to pass arguments to functions:

  1. Positional Arguments: Arguments passed to a function in correct positional order. The number and positions of arguments must match the formal parameters exactly.
    def greet(name, age):
        print("Name:", name, "Age:", age)
    
    greet("Aman", 17)  # Correct positional matching
  2. Default Arguments: Parameters that assume a default value if no corresponding argument is passed in the function call.
    Critical CBSE Rule: In a function header, any parameter with a default value must be placed after all parameters without default values. Non-default arguments cannot follow default arguments.
    # VALID function header:
    def calc_interest(principal, rate=7.5, time=1):
        return (principal * rate * time) / 100
    
    # INVALID function header (causes SyntaxError: non-default argument follows default argument):
    # def calc_interest(principal=1000, rate, time=1):
  3. Keyword (Named) Arguments: Arguments identified by the parameter name during the function call. The order of arguments can be changed when using keyword arguments.
    def display_student(name, roll_no):
        print(roll_no, name)
    
    display_student(roll_no=101, name="Riya")  # Keyword arguments

5. Scope and Lifetime of Variables

Scope refers to the program region where a variable is accessible and recognized. The lifetime is the duration for which the variable exists in memory.

  • Local Scope: Variables defined inside a function. They are created when the function starts executing and destroyed as soon as the function terminates. Local variables cannot be accessed outside the function.
  • Global Scope: Variables declared outside all functions at the top level of the module/script. They are accessible throughout the entire program.
  • The global Statement: By default, an assignment statement inside a function creates a new local variable. To modify an existing global variable from within a local function scope, you must declare it using the global keyword.
val = 50  # Global variable

def modify():
    global val
    val = val + 10  # Modifies the global variable val
    print("Inside function:", val)

modify()
print("Outside function:", val)
# Output:
# Inside function: 60
# Outside function: 60

LEGB Rule: When Python resolves variable names, it searches scopes in the following strict hierarchy: Local → Enclosing → Global → Built-in.

6. Passing Mutable vs. Immutable Arguments (Pass by Object Reference)

In Python, arguments are passed by object reference. The effect of modifications inside a function depends on the mutability of the passed object:

  • Immutable Objects (Integers, Floats, Strings, Tuples): Reassigning or changing the value inside the function creates a new local object. The original caller variable remains unchanged.
  • Mutable Objects (Lists, Dictionaries, Sets): In-place modifications (such as list.append(), list.pop(), or item assignments) directly alter the original object in the caller's scope because both refer to the exact same memory location.

Important CBSE Questions with Answers

Question 1 (Theory - 2 Marks)

Differentiate between positional arguments and default arguments with a suitable Python code example.

Answer:

  • Positional Arguments: These are arguments passed to a function in sequential order. The number of actual arguments must match the number of formal parameters defined.
  • Default Arguments: These are formal parameters initialized with default values in the function definition. If an actual argument is omitted during the call, the default value is used.
def power(base, exp=2):  # exp is a default argument
    return base ** exp

print(power(5))       # Uses default value exp=2 -> Output: 25
print(power(5, 3))    # Overrides default with positional argument 3 -> Output: 125

Question 2 (Error Finding - 2 Marks)

Identify and rewrite the corrected Python code after removing all syntax and logical errors. Underline each correction.

Def Check(N1, N2=10, N3):
    Sum = N1 + N2 + N3
    Return Sum

print(Check(5, 15))

Answer:

Errors identified:

  1. Keyword Def must be lowercase def.
  2. Non-default argument N3 follows default argument N2=10, which violates Python syntax. N3 must precede N2=10 or also have a default value.
  3. Keyword Return must be lowercase return.

Corrected Code:

def Check(N1, N3, N2=10):
    Sum = N1 + N2 + N3
    return Sum

print(Check(5, 15))

Question 3 (Find the Output - 2 Marks)

Find and write the output of the following Python code snippet:

a = 100

def update(b):
    global a
    a += b
    b = a * 2
    print("Inside:", a, b)

update(20)
print("Outside:", a)

Answer / Output:

Inside: 120 240
Outside: 120

Explanation: Inside update(), global a references the global variable a. a += 20 modifies a to 120. b becomes 120 * 2 = 240. When printing outside, the global a retains its modified value of 120.

Question 4 (Find the Output with Mutable List - 3 Marks)

Predict the output of the following Python program:

def Alter(List1):
    for i in range(len(List1)):
        if List1[i] % 5 == 0:
            List1[i] //= 5
        elif List1[i] % 3 == 0:
            List1[i] //= 3
        else:
            List1[i] *= 2

Nums = [25, 18, 7, 10]
Alter(Nums)
for item in Nums:
    print(item, end="#")

Answer / Output:

5#6#14#2#

Step-by-step Trace:

  • 25 % 5 == 025 // 5 = 5
  • 18 % 5 != 0, 18 % 3 == 018 // 3 = 6
  • 7 % 5 != 0, 7 % 3 != 07 * 2 = 14
  • 10 % 5 == 010 // 5 = 2

Question 5 (Programming Question - 3 Marks)

Write a user-defined function VowelCount(Text) in Python that accepts a string Text as a parameter and counts and displays the number of uppercase and lowercase vowels present in the string.

Answer:

def VowelCount(Text):
    vowels = "aeiouAEIOU"
    count = 0
    for ch in Text:
        if ch in vowels:
            count += 1
    print("Total number of vowels:", count)

# Sample function call:
# VowelCount("CBSE Computer Science Examination 2026")
# Output: Total number of vowels: 13

How to Prepare for This Topic

  1. Practice Trace Tables for Output Questions: Construct systematic trace columns for local variables, global variables, and loop indices to prevent calculation slips on 2-mark and 3-mark tracing questions.
  2. Master Default Parameter Placement: Memorize the syntax rule that non-default parameters must always precede default parameters. This is tested repeatedly in error-detection questions.
  3. Understand List and String Mutability: Remember that modifying a list inside a function alters the caller's list, whereas modifying an integer or string inside a function creates a local copy unless explicitly returned or declared global.
  4. Check Function Headers and Indentation: In Python coding questions, ensure proper indentation, parameter naming, and the presence of colons (:) to avoid losing step-marking points.

Where to Practice More

To master Python functions and score a perfect 70/70 in your theory paper, practice chapter-wise question banks, previous year board exam questions (PYQs), and full-length model test papers curated by CBSE examiners on QPTool (qptool.theorify.in).

Want to customize or export this paper?

Load this template into the Theorify editor to modify questions, add your school header logo, or download Word (.docx) & PDF.

Create New Paper
Paper Specifications
  • Target Class: Class 12
  • Subject: Computer Science
  • Curriculum: CBSE Standard
  • Export Formats: Microsoft Word & High-Res PDF
  • Formatting: Dual-Column CBSE Standard
Theorify Pro with AI

Generate matching step-by-step answer keys, embed custom school logos, and create unlimited tests in seconds.

Explore Pro Plans (₹499/mo) →