
Let’s master how functions work in Python – from defining custom logic to understanding variable scope and returning values. This is a must-know topic for logic building and coding rounds.
Concept Notes: Python Functions & Scope
🔹 1. What is a Function?
A function is a block of code that performs a specific task and can be reused.
def greet():
print("Hello!")
Use def
to define a function.
🔹 2. Function With Parameters
def add(a, b):
return a + b
a
andb
are parametersreturn
gives output
🔹 3. Calling a Function
result = add(5, 3)
print(result) # Output: 8
🔹 4. Types of Arguments
- Positional: Based on order
- Keyword: Named explicitly
- Default: Value given during definition
- Variable-length:
*args
and**kwargs
🔹 5. Scope of Variables
Type | Defined Inside | Accessible Where |
---|---|---|
Local | Function only | Inside that function |
Global | Outside function | Entire program |
x = 10 # Global
def func():
x = 5 # Local
print(x) # 5
🔹 6. Global Keyword
x = 10
def modify():
global x
x = 20
Allows modifying a global variable inside a function.
🔹 7. Return vs Print
return
gives value to callerprint
only displays output
def square(x):
return x*x
🔹 8. Recursive Function
A function that calls itself:
def fact(n):
if n == 0:
return 1
return n * fact(n - 1)
op 10 MCQs – Functions & Scope in Python
1️⃣ Which keyword is used to define a function in Python?
A) function
B) def
C) define
D) func
2️⃣ What does the return statement do?
A) Ends the program
B) Exits the loop
C) Returns value from a function
D) Prints the result
3️⃣ What is the output of:
def add(x, y):
return x + y
print(add(3, 4))
A) 7
B) 34
C) add
D) Error
4️⃣ What is the default return value of a function without a return statement?
A) 0
B) undefined
C) None
D) Error
5️⃣ Which of these are valid variable scopes?
A) Local
B) Global
C) Both
D) None
6️⃣ What will be the output?
x = 5
def show():
x = 10
print(x)
show()
A) 5
B) 10
C) Error
D) None
7️⃣ To modify a global variable inside a function, use:
A) static
B) extern
C) global
D) public
8️⃣ What is printed here?
def test(a=3, b=4):
print(a * b)
test(2)
A) 8
B) 6
C) 12
D) Error
9️⃣ Choose the correct recursive function:
A) def r(): r()
B) def r(): print(r)
C) def r(): return r()
D) Both A & C
🔟 What is the difference between print and return?
A) return gives value, print displays
B) return prints value
C) print stores value
D) Both are same
✅ Answer Key
Q.No | Answer |
---|---|
1 | B |
2 | C |
3 | A |
4 | C |
5 | C |
6 | B |
7 | C |
8 | A |
9 | D |
10 | A |
📖 Explanations
- Q1: Python uses
def
to define functions. - Q2:
return
sends back result from the function. - Q3: 3 + 4 = 7.
- Q4: If no return – function returns
None
. - Q5: Local and Global are two main scopes.
- Q6: Local
x = 10
, so output is 10. - Q7: Use
global
to access/modify a global variable. - Q8: b defaults to 4 → 2 × 4 = 8.
- Q9: A and C are recursive (they call themselves).
- Q10:
return
passes values,print
only shows.
📥 Download Notes + MCQs PDF
👉 Get PDF from: @learnnewthingsoffcial
💬 Comment Task
💬 Share one Python function you wrote recently in the group and we’ll give feedback 💻🔥