Last updated on May 27th, 2026 at 09:31 am
Topics covered: What is a function, types of functions, defining & calling, arguments & parameters, types of arguments (positional, keyword, default, variable-length), return statement, scope of variables (local, global), global keyword, lambda functions — with complete programs and MCQs.
1. What is a Function?
A function is a named block of code that performs a specific task. It runs only when called. Functions help break a large program into smaller, reusable, and organized pieces.
Why Use Functions?
- Code Reusability: Write once, use many times — no need to repeat the same code.
- Modularity: Break large programs into small manageable parts.
- Easy Debugging: Errors are easier to find and fix in small functions.
- Readability: Program becomes more organized and easy to understand.
- Avoid Duplication: Reduces redundant code in the program.
2. Types of Functions in Python
| Type | Description | Examples |
|---|---|---|
| Built-in Functions | Pre-defined in Python — always available, no import needed | print(), input(), len(), type(), int(), range(), min(), max() |
| Functions in Modules | Defined in modules/libraries — must import first | math.sqrt(), random.randint(), datetime.date.today() |
| User Defined Functions | Created by the programmer using def keyword | def add(), def greet(), def calculate_area() |
| Lambda Functions | Small anonymous (no name) single-line functions | lambda x: x*2 |
3. User Defined Functions — Syntax
def function_name(parameters): """docstring — optional description of function""" statement(s) return value # optional
Defining and Calling a Function
# DEFINING the function def greet(): print("Hello! Welcome to cbsepython.in") # CALLING the function greet() greet() # can be called multiple times
4. Arguments and Parameters
| Term | Where | Example |
|---|---|---|
| Parameter | In function DEFINITION — receives value | def add(a, b): — here a, b are parameters |
| Argument | In function CALL — value passed | add(5, 3) — here 5, 3 are arguments |
def welcome(name): # 'name' is PARAMETER print("Hello,", name) welcome("Rahul") # "Rahul" is ARGUMENT welcome("Priya")
5. Four Types of Functions (Based on Arguments & Return)
i) No Argument, No Return
def add(): a = int(input("Num1: ")) b = int(input("Num2: ")) print("Sum:", a+b) add()
Input inside function. Output printed inside function.
ii) With Argument, No Return
def add(a, b): print("Sum:", a+b) x = int(input("Num1: ")) y = int(input("Num2: ")) add(x, y)
Values passed from outside. Result printed inside.
iii) No Argument, With Return
def add(): a = int(input("Num1: ")) b = int(input("Num2: ")) return a + b result = add() print("Sum:", result)
Input inside function. Result returned and stored outside.
iv) With Argument, With Return
def add(a, b): return a + b x = int(input("Num1: ")) y = int(input("Num2: ")) result = add(x, y) print("Sum:", result)
Values passed in, result returned. Most flexible type.
6. Types of Arguments in Python
Type 1 — Positional Arguments (Required Arguments)
Arguments passed in the same order as parameters defined. Number must match exactly.
def student(name, roll, marks): print(f"Name: {name}, Roll: {roll}, Marks: {marks}") student("Rahul", 101, 85) # positional — order matters!
Type 2 — Keyword Arguments (Named Arguments)
Arguments passed by name — order does not matter.
def student(name, roll, marks): print(f"Name: {name}, Roll: {roll}, Marks: {marks}") student(roll=101, marks=85, name="Rahul") # order doesn't matter!
Type 3 — Default Arguments
Parameters with a default value — if no argument passed, default is used.
def greet(name, msg="Good Morning"): # msg has default value print(f"Hello {name}! {msg}") greet("Rahul") # uses default msg greet("Priya", "Good Evening") # overrides default
Type 4 — Variable Length Arguments (*args)
When you don’t know how many arguments will be passed — use *args. Receives all arguments as a tuple.
def add_all(*nums): # *args — any number of values total = 0 for n in nums: total += n print("Sum:", total) print("Type:", type(nums)) add_all(1, 2, 3) add_all(10, 20, 30, 40, 50)
Types of Arguments — Comparison
| Type | Syntax | Key Point | Example Call |
|---|---|---|---|
| Positional | def f(a, b) | Order must match | f(10, 20) |
| Keyword | def f(a, b) | Use name=value, order free | f(b=20, a=10) |
| Default | def f(a, b=5) | Default used if not passed | f(10) or f(10, 3) |
| Variable (*args) | def f(*nums) | Any number, stored as tuple | f(1,2,3,4,5) |
7. return Statement
The return statement sends a value back from the function to the calling code. It does not print — you must store or print the returned value.
def square(n): return n * n # returns value, does NOT print result = square(5) print(result) # 25 print(square(7)) # 49 — directly in print
Returning Multiple Values
Python functions can return multiple values as a tuple.
def min_max(lst): return min(lst), max(lst) # returns tuple lo, hi = min_max([4, 1, 9, 2, 7]) print("Min:", lo, "Max:", hi)
8. Scope of Variables — Local and Global
Scope refers to the region of a program where a variable is accessible.
| Type | Defined Where | Accessible Where | Lifetime |
|---|---|---|---|
| Local Variable | Inside a function | Only inside that function | Until function ends |
| Global Variable | Outside all functions (main program) | Everywhere — inside and outside functions | Entire program |
Local Variable Example
def myFunc(): x = 10 # x is LOCAL — only inside myFunc print("Inside:", x) myFunc() print(x) # ERROR! x not accessible outside
Global Variable Example
school = "CBSE Python School" # GLOBAL variable def display(): print(school) # accessible inside function display() print(school) # accessible outside too
Same Name — Local and Global
x = 100 # global x def test(): x = 50 # local x — DIFFERENT from global x print("Inside:", x) # 50 (local) test() print("Outside:", x) # 100 (global unchanged)
9. global Keyword
By default, a variable inside a function is local. To modify a global variable inside a function, use the global keyword.
count = 0 # global variable def increment(): global count # declare we want to use global count count += 1 # modifying global variable print("Count:", count) increment() increment() increment() print("Final:", count)
# Without global keyword — ERROR! count = 0 def increment(): count += 1 # UnboundLocalError!
10. Lambda Functions (Anonymous Functions)
A lambda function is a small, anonymous (no name) function defined in a single line using the lambda keyword.
# Syntax: lambda parameters : expression # Example — square a number: square = lambda x : x * x print(square(5)) # 25 # Lambda with two parameters: add = lambda a, b : a + b print(add(3, 4)) # 7 # Lambda for checking even/odd: is_even = lambda n : "Even" if n % 2 == 0 else "Odd" print(is_even(6)) # Even print(is_even(7)) # Odd
Lambda vs Normal Function
| Feature | def Function | Lambda Function |
|---|---|---|
| Name | Has a name | Anonymous (no name needed) |
| Lines | Multiple lines possible | Single expression only |
| return | Uses return keyword | Automatically returns expression |
| docstring | Can have docstring | Cannot have docstring |
| Use case | Complex functions | Short, simple one-line operations |
11. Complete Programs
Program 1 — Factorial using Function
def factorial(n): fact = 1 for i in range(1, n+1): fact *= i return fact num = int(input("Enter number: ")) print(f"Factorial of {num} is:", factorial(num))
Program 2 — Check Prime using Function
def is_prime(n): if n < 2: return False for i in range(2, n): if n % i == 0: return False return True n = int(input("Enter number: ")) if is_prime(n): print(n, "is Prime") else: print(n, "is Not Prime")
Program 3 — Default Argument — Area of Rectangle
def area(length, breadth=10): # breadth has default value return length * breadth print("Area 1:", area(5)) # breadth = 10 (default) print("Area 2:", area(5, 8)) # breadth = 8 (given)
Program 4 — *args — Sum of Any Numbers
def total(*nums): s = sum(nums) print(f"Numbers: {nums}") print(f"Sum: {s}, Average: {s/len(nums):.2f}") total(10, 20, 30) total(5, 15, 25, 35, 45)
Program 5 — global Keyword Usage
total_students = 0 def add_student(name): global total_students total_students += 1 print(f"Added: {name} | Total: {total_students}") add_student("Rahul") add_student("Priya") add_student("Ajay") print("Final count:", total_students)
Program 6 — Lambda — Sort by Second Element
students = [("Rahul", 85), ("Priya", 92), ("Ajay", 74)] # Sort by marks (2nd element) using lambda students.sort(key=lambda x: x[1], reverse=True) for s in students: print(f"{s[0]:<10} {s[1]}")
12. Practice MCQs — Board Exam Style
Q1. Which keyword is used to define a function in Python?
a) function b) define c) def d) funcQ2. What is the difference between a parameter and an argument?
a) Both are same b) Parameter is in function call, argument in definition c) Parameter is in function definition, argument is in function call d) None of aboveQ3. What type does *args store multiple values as?
a) List b) Dictionary c) Tuple d) SetQ4. Which of the following is a correct definition with default argument?
a) def f(a=5, b) b) def f(b, a=5) c) def f(=5, b) d) def f(a, 5=b)Q5. A variable defined inside a function is called:
a) Global variable b) Local variable c) Static variable d) Public variableQ6. To modify a global variable inside a function, which keyword is used?
a) local b) var c) global d) externQ7. What is the output?f = lambda x, y: x if x > y else y
print(f(10, 20))
Q8. What happens after a return statement executes in a function?
a) Function continues b) Function immediately ends c) return is ignored d) Error occursQ9. Which type of argument allows passing arguments by name regardless of position?
a) Positional b) Default c) Keyword d) Variable lengthQ10. What will be the output?def show(a, b=10, c=20):
print(a, b, c)
show(5, c=30)
📚 More Notes: Text File Handling | Binary File Handling | All Class 12 MCQs | All Class 12 Notes