Functions
Functions
🔹 What is a Function?
A function is a block of code that performs a specific task. It can be reused multiple times.
🔹 Types of Functions:
🔹 Syntax of a Function:
python
CopyEdit
def function_name(parameters):
# Function body
return value
🧠 Examples
Example 1: Function with no parameters and no return
python
CopyEdit
def greet():
print("Hello, students!")
✏️Exercises
🔸 Section A: Objective Questions
1. You can define multiple functions with the same name in Python.
2. A function can return only one value.
3. The function body is defined by indentation.
python
CopyEdit
def square(x):
return x * x
print(square(3))
print(square(5))
python
CopyEdit
def greet(name)
print("Hi", name)
Q6. Write a function named multiply that takes two arguments and returns their product.
Q7. Write a function called is_even that returns True if a number is even, otherwise False.
Q9. Write a function fibonacci(n) that prints the first n terms of the Fibonacci sequence.
Q10. Write a function that counts and returns the number of vowels in a given string.
📎 Teacher’s Note / Tips for Students
Use meaningful function names and comments to improve code readability.
Practice tracing functions step-by-step to understand flow.
Try creating small functions to solve real-life problems like calculators or temperature
converters.
✅ An Answer Key?
📄 A PDF or DOCX version?
🧪 A worksheet with MCQs or coding-based assessments?
You said:
answer key
ChatGPT said:
Great! Here's the Answer Key for the Class 12 Python Functions Worksheet.
1. def
2. return
3. modularity
1. False – You cannot have multiple functions with the same name in the same scope.
2. False – A function can return multiple values using tuples.
3. True – Indentation is used to define blocks of code, including the function body.
🔸 Section B: Short Answer Questions
Q3. Difference between built-in and user-defined functions:
Q4. Output:
python
CopyEdit
def square(x):
return x * x
print(square(3)) # 9
print(square(5)) # 25
Answer:
CopyEdit
9
25
python
CopyEdit
def greet(name)
print("Hi", name)
Corrected Version:
python
CopyEdit
def greet(name):
print("Hi", name)
python
CopyEdit
def multiply(a, b):
return a * b
python
CopyEdit
def is_even(n):
return n % 2 == 0
python
CopyEdit
def print_upper(text):
print(text.upper())
python
CopyEdit
def fibonacci(n):
a, b = 0, 1
for _ in range(n):
print(a, end=' ')
a, b = b, a + b
python
CopyEdit
def count_vowels(text):
vowels = "aeiouAEIOU"
count = 0
for char in text:
if char in vowels:
count += 1
return count