UNIT–4: FUNCTIONS & MODULAR PROGRAMMING
(SOLUTIONS)
1. Introduction to Functions
Q1. Define a function and list its advantages.
A function is a reusable block of code that performs a specific task.
Advantages: Code reusability, readability, easy debugging, modular structure.
Q2. Program to display your name using a function.
def display_name():
print('Japjeet Kaur')
display_name()
2. Syntax of Functions
Q1. General syntax of Python function.
def function_name(parameters):
"""docstring"""
statements
return value
Q2. Identify error in function definition.
Common errors: Missing def keyword, missing colon (:), incorrect indentation.
3. Defining and Calling Function
Q1. Function to print numbers 1 to 10.
def print_numbers():
for i in range(1, 11):
print(i)
print_numbers()
Q2. Function to print welcome message.
def welcome():
print('Welcome to Python')
welcome()
4. Docstring
Q1. What is a docstring?
A docstring is a string written in triple quotes to describe a function.
Q2. Function with docstring.
def add(a, b):
"""Returns sum of two numbers"""
return a + b
5. Return Statement
Q1. Function to return square.
def square(x):
return x*x
print(square(5))
Q2. Can a function return more than one value?
Yes, using commas.
def values():
return 1,2,3
print(values())
6. Parameters and Arguments
Q1. Difference.
Parameters are variables in function definition; arguments are values passed.
Q2. Function to return sum.
def sum_numbers(a, b):
return a + b
print(sum_numbers(10,5))
7. Keyword & Default Arguments
Q1. Keyword arguments example.
def student(name, age):
print(name, age)
student(age=20, name='Aman')
Q2. Default arguments example.
def student(name, age=18):
print(name, age)
student('Rahul')
8. Variable Length Arguments
Q1. Definition.
Used when number of arguments is unknown using *args.
Q2. Sum of n numbers.
def total(*numbers):
return sum(numbers)
print(total(1,2,3,4))
9. Lambda Functions
Q1. Define lambda.
Anonymous one-line function.
Q2. Cube using lambda.
cube = lambda x: x**3
print(cube(3))
10. map() and filter()
Q1. map() example.
nums=[1,2,3,4]
print(list(map(lambda x:x*2, nums)))
Q2. filter() example.
nums=[1,2,3,4,5]
print(list(filter(lambda x:x%2==0, nums)))
11. Fruitful Functions
Q1. Definition.
Function that returns a value.
Q2. Example.
def maximum(a,b):
return a if a>b else b
print(maximum(10,20))
12. Global and Local Variables
Q1. Difference.
Global declared outside, local inside function.
Q2. Program.
x=10
def show():
x=5
print('Local:',x)
show()
print('Global:',x)
13. Function Composition
Q1. Explanation.
Using one function inside another.
Q2. Program.
def add(x): return x+2
def mul(y): return y*3
print(mul(add(4)))
14. Recursion
Q1. Definition.
Function calling itself.
Q2. Factorial.
def factorial(n):
if n==1:
return 1
return n*factorial(n-1)
print(factorial(5))
15. Modular Programming
Q1. Definition.
Dividing program into modules.
Q2. Advantages.
Easy maintenance, reusability, organized code.
16. Import Statements
Q1. Difference.
import module imports full module; from module import function imports specific function.
Q2. Syntax.
import math
from math import sqrt
17. Python Modules & Date-Time
Q1. Four modules.
math, random, datetime, os
Q2. Program.
import datetime
print([Link]())