Jan 26, 2026
# Chapter 5 — Functions (Write Less, Reuse More)
This chapter is a “workshop”: we build a function, then improve it.
## 5.1 A Basic Function
```python
def add(a, b):
return a + b
print(add(2, 3))
```
## 5.2 Parameters vs Arguments (Simple Meaning)
- Parameter: the name in the function definition (`a`, `b`)
- Argument: the value you pass when calling (`2`, `3`)
## 5.3 Default Values
```python
def greet(name, site="meetcode"):
return f"Hi {name}, welcome to {site}"
print(greet("Aman"))
print(greet("Aman", "Meetcode"))
```
## 5.4 Return vs Print
Use `return` when you want the value for later.
Use `print` when you want to show something to the user right now.
## 5.5 Scope (Local, Global) and Why Bugs Happen Here
Think like this:
- Names created inside a function usually stay inside that function.
- Names created outside are global, but reading and writing are not the same thing.
```python
x = 10
def show():
print(x)
show()
```
This works because you only read `x`.
But this fails:
```python
x = 10
def change():
x = x + 1
return x
print(change())
```
Because Python thinks `x` is local inside `change()` (you assign to it), so `x = x + 1` becomes “use local x before it exists”.
Fix by passing values:
```python
def change(x):
return x + 1
print(change(10))
```
### Diagram: Name Lookup (simple view)
```text
inside a function:
1) local names
2) enclosing function names (if nested)
3) global names (module)
4) built-in names
```
## 5.6 `*args` and `**kwargs` (Flexible Calling)
When you want a function to accept “many values”:
```python
def total(*nums):
s = 0
for n in nums:
s = s + n
return s
print(total(1, 2, 3))
```
When you want a function to accept “named options”:
```python
def build_user(**info):
return info
u = build_user(name="Riya", site="meetcode", level=1)
print(u)
```
## 5.7 Keyword-Only Arguments (Stop Confusing Calls)
Sometimes you want to force the caller to use names:
```python
def create_account(name, *, plan, active=True):
return {"name": name, "plan": plan, "active": active}
print(create_account("Aman", plan="pro"))
```
This prevents mistakes like `create_account("Aman", "pro", False)` where nobody remembers what the second value means.
## 5.8 Functions Are Values (The “Give Me Behavior” Idea)
You can pass functions to other functions.
```python
def by_length(text):
return len(text)
words = ["meetcode", "py", "student", "code"]
print(sorted(words, key=by_length))
```
This is the core idea behind callbacks, event handlers, and many libraries.
## 5.9 Closures (Function Factory)
Sometimes you want a function that “remembers” something.
```python
def make_multiplier(k):
def mul(x):
return x * k
return mul
times_2 = make_multiplier(2)
times_5 = make_multiplier(5)
print(times_2(10), times_5(10))
```
### Diagram: Closure Memory
```text
make_multiplier(5) returns mul()
mul() keeps k=5 inside it
later mul(10) uses that remembered k
```
## 5.10 Designing Better Functions (Real Rule)
If your function:
- takes too many inputs
- does too many things
- is hard to test
Then split it into smaller “pure” functions (no printing, no input, no file writes) and one small “controller” function that connects them.
---
## Conclusion
In this article, we explored the core concepts of All about Python - — Functions (Write Less, Reuse More). Understanding these fundamentals is crucial for any developer looking to master this topic.
## Frequently Asked Questions (FAQs)
**Q: What is All about Python - — Functions (Write Less, Reuse More)?**
A: All about Python - — Functions (Write Less, Reuse More) is a fundamental concept in this programming language/topic that allows developers to perform specific tasks efficiently.
**Q: Why is All about Python - — Functions (Write Less, Reuse More) important?**
A: It helps in organizing code, improving performance, and implementing complex logic in a structured way.
**Q: How to get started with All about Python - — Functions (Write Less, Reuse More)?**
A: You can start by practicing the basic syntax and examples provided in this tutorial.
**Q: Are there any prerequisites for All about Python - — Functions (Write Less, Reuse More)?**
A: Basic knowledge of programming logic and syntax is recommended.
**Q: Can All about Python - — Functions (Write Less, Reuse More) be used in real-world projects?**
A: Yes, it is widely used in enterprise-level applications and software development.
**Q: Where can I find more examples of All about Python - — Functions (Write Less, Reuse More)?**
A: You can check our blog section for more advanced tutorials and use cases.
**Q: Is All about Python - — Functions (Write Less, Reuse More) suitable for beginners?**
A: Yes, our guide is designed to be beginner-friendly with clear explanations.
**Q: How does All about Python - — Functions (Write Less, Reuse More) improve code quality?**
A: By providing a standardized way to handle logic, it makes code more readable and maintainable.
**Q: What are common mistakes when using All about Python - — Functions (Write Less, Reuse More)?**
A: Common mistakes include incorrect syntax usage and not following best practices, which we've covered here.
**Q: Does this tutorial cover advanced All about Python - — Functions (Write Less, Reuse More)?**
A: This article covers the essentials; stay tuned for our advanced series on this topic.