🔁 Most Repeated & Important Questions on Functions – CBSE Class 12 CS
📘 Theory & Definitions (Very Short/Short Answer Type)
1. What is a function in Python?
2. Differentiate between built-in and user-defined functions.
3. Define the term recursion. How is it different from iteration?
4. What is the difference between return and print() in a function?
5. What are default arguments in a function? Give an example.
6. What is the significance of None in Python functions?
7. What are local and global variables? Give suitable examples.
8. Explain the difference between def func(*args) and def func(**kwargs).
9. What is the purpose of the pass statement in a function?
10. What do you understand by function scope in Python?
🔧 Application-Based Questions (Code Output/Debugging)
11. Predict the output of the following function:
python
CopyEdit
def calc(x, y=10):
return x + y
print(calc(5))
print(calc(5, 15))
12. Trace the output:
python
CopyEdit
def update(lst):
lst.append(100)
nums = [1, 2, 3]
update(nums)
print(nums)
13. Write a function to count vowels in a string.
14. Write a function that returns the factorial of a number using recursion.
15. Define a function to check whether a number is a palindrome.
16. Write a function that accepts a list and returns a list of even numbers.
17. Write a program with a function to reverse a string without using slicing.
⚙ User-Defined Function Practice
18. Create a menu-driven program using functions for basic calculator operations.
19. Write a function to find the HCF of two numbers.
20. Write a function to find the sum of digits of a number.
21. Define a function to check whether a number is prime.
22. Write a program using a function to return the nth Fibonacci number (recursively).
23. Define a function that returns the largest of 3 numbers.
24. Write a function that returns the sum of all prime numbers between 1 and 100.
25. Define a function that takes a string and returns the number of words in it.
🧩 Function-based MCQ/Conceptuals (common in CUET too!)
26. Which of the following statements defines a function in Python?
a) function func():
b) def func():
c) define func():
d) None of the above
27. What will be the output of:
python
CopyEdit
def foo(x, y=[]):
y.append(x)
return y
print(foo(1))
print(foo(2))
28. Can a function return multiple values in Python? If yes, give an example.
29. What is the difference between positional and keyword arguments in Python?
30. Consider:
python
CopyEdit
def show(name, age=18):
print(name, age)
show("Tanya")
show("Tanya", 20)
What will be the output?
✅ Q1. What is a function in Python?
A function is a block of reusable code that performs a specific task. You define it using the
def keyword.
python
CopyEdit
def greet():
print("Hello!")
✅ Q2. Difference between built-in and user-defined functions
Built-in Functions User-defined Functions
Predefined by Python Created by the programmer
Eg: len(), type(), max() Eg: def sum(a, b): return a + b
✅ Q3. What is recursion? How is it different from iteration?
Recursion is when a function calls itself to solve a smaller part of the problem.
Iteration uses loops (for, while) to repeat tasks.
🔁 Recursion vs Iteration:
Recursion Iteration
Function calls itself Uses loops
Uses more memory (stack calls) Memory efficient
Harder to debug Easier to trace
✅ Q4. Difference between return and print()
print() displays output to the screen.
return sends a value back to the calling function.
python
CopyEdit
def add(a, b):
return a + b # returns the result
print(add(2, 3)) # prints 5
✅ Q5. Default arguments with example
When you give a default value to a parameter.
python
CopyEdit
def greet(name, msg="Hello"):
print(msg, name)
greet("Tanya") # Hello Tanya
greet("Tanya", "Hi") # Hi Tanya
✅ Q6. What is the significance of None in functions?
None is returned by default when a function doesn't explicitly return anything.
python
CopyEdit
def func():
pass
result = func()
print(result) # Output: None
✅ Q7. Local vs Global Variables
Local: Defined inside a function; accessible only there.
Global: Defined outside all functions; accessible everywhere.
python
CopyEdit
x = 5 # global
def show():
x = 10 # local
print(x)
show() # 10
print(x) # 5
✅ Q8. Difference between *args and **kwargs
*args → variable number of positional arguments.
**kwargs → variable number of keyword arguments.
python
CopyEdit
def func(*args, **kwargs):
print(args)
print(kwargs)
func(1, 2, a=3, b=4)
# args = (1, 2)
# kwargs = {'a': 3, 'b': 4}
✅ Q9. Purpose of the pass statement
It’s a placeholder for future code; prevents syntax errors.
python
CopyEdit
def func():
pass # Do nothing for now
✅ Q10. What is function scope?
It refers to where a variable can be accessed – inside or outside the function.
Local scope: Within function only.
Global scope: Throughout the program.
🧠 Q11. Predict the output:
python
CopyEdit
def calc(x, y=10):
return x + y
print(calc(5))
print(calc(5, 15))
✅ Output:
CopyEdit
15
20
Explanation:
calc(5) → x=5, y=10 (default) → 5+10 = 15
calc(5,15) → both values passed → 5+15 = 20
🧠 Q12. Output:
python
CopyEdit
def update(lst):
lst.append(100)
nums = [1, 2, 3]
update(nums)
print(nums)
✅ Output:
csharp
CopyEdit
[1, 2, 3, 100]
Because lists are mutable, changes inside the function affect the original list.
✍️Q13. Function to count vowels in a string
python
CopyEdit
def count_vowels(s):
count = 0
for char in s.lower():
if char in 'aeiou':
count += 1
return count
# Example
print(count_vowels("Tanya")) # Output: 2
✍️Q14. Recursive factorial function
python
CopyEdit
def factorial(n):
if n == 0 or n == 1:
return 1
return n * factorial(n-1)
# Example
print(factorial(5)) # Output: 120
✍️Q15. Check if number is palindrome
python
CopyEdit
def is_palindrome(n):
return str(n) == str(n)[::-1]
# Example
print(is_palindrome(121)) # Output: True
print(is_palindrome(123)) # Output: False
✍️Q16. Return even numbers from a list
python
CopyEdit
def get_evens(lst):
return [num for num in lst if num % 2 == 0]
# Example
print(get_evens([1, 2, 3, 4, 5])) # Output: [2, 4]
✍️Q17. Reverse a string without slicing
python
CopyEdit
def reverse_string(s):
result = ''
for char in s:
result = char + result
return result
# Example
print(reverse_string("Tanya")) # Output: aynaT
✍️Q18. Calculator using functions
python
CopyEdit
def add(a, b): return a + b
def subtract(a, b): return a - b
def multiply(a, b): return a * b
def divide(a, b): return a / b if b != 0 else "Error"
# Menu-driven (for concept)
print(add(5, 3)) # 8
print(subtract(5, 3)) # 2
✍️Q19. Function to find HCF
python
CopyEdit
def hcf(a, b):
while b != 0:
a, b = b, a % b
return a
# Example
print(hcf(12, 18)) # Output: 6
✍️Q20. Sum of digits of a number
python
CopyEdit
def sum_digits(n):
total = 0
while n > 0:
total += n % 10
n //= 10
return total
# Example
print(sum_digits(1234)) # Output: 10
✅ Q21. Function to check if a number is prime
python
CopyEdit
def is_prime(n):
if n <= 1:
return False
for i in range(2, int(n**0.5) + 1):
if n % i == 0:
return False
return True
# Example
print(is_prime(7)) # Output: True
print(is_prime(10)) # Output: False
✅ Q22. Recursive function for nth Fibonacci number
python
CopyEdit
def fibonacci(n):
if n == 0: return 0
if n == 1: return 1
return fibonacci(n-1) + fibonacci(n-2)
# Example
print(fibonacci(5)) # Output: 5 (0,1,1,2,3,5)
✅ Q23. Function to return largest of 3 numbers
python
CopyEdit
def largest(a, b, c):
return max(a, b, c)
# Example
print(largest(5, 9, 3)) # Output: 9
✅ Q24. Sum of all prime numbers between 1 and 100
python
CopyEdit
def is_prime(n):
if n <= 1: return False
for i in range(2, int(n**0.5)+1):
if n % i == 0:
return False
return True
def sum_of_primes():
return sum(i for i in range(2, 101) if is_prime(i))
print(sum_of_primes()) # Output: 1060
✅ Q25. Function to count number of words in a string
python
CopyEdit
def count_words(s):
return len(s.split())
# Example
print(count_words("Class 12 Computer Science")) # Output: 4
🧠 Q26. MCQ – What defines a function in Python?
python
CopyEdit
def func():
pass
✅ Correct answer: (b) def func():
🧠 Q27. Output of the following:
python
CopyEdit
def foo(x, y=[]):
y.append(x)
return y
print(foo(1))
print(foo(2))
✅ Output:
csharp
CopyEdit
[1]
[1, 2]
Why? Because default mutable arguments ([]) retain changes across function calls.
✅ Q28. Can a function return multiple values in Python?
Yes! It returns them as a tuple.
python
CopyEdit
def get_stats():
return 90, 85, 95
marks = get_stats()
print(marks) # Output: (90, 85, 95)
✅ Q29. Positional vs Keyword arguments
python
CopyEdit
def greet(name, age):
print(name, age)
greet("Tanya", 17) # Positional
greet(age=17, name="Tanya") # Keyword
🔸 Positional → order matters
🔸 Keyword → you specify the parameter names
✅ Q30. Output:
python
CopyEdit
def show(name, age=18):
print(name, age)
show("Tanya")
show("Tanya", 20)
✅ Output:
nginx
CopyEdit
Tanya 18
Tanya 20