If you write the same code again and again, you are not programming — you are copying.
Functions solve this problem. They allow you to write logic once and reuse it everywhere.
A function is a block of code that runs only when called.
def greet():
print("Hello, welcome to Python")
greet()
Parameters allow functions to accept data.
def greet_user(name):
print("Hello", name)
greet_user("Amit")
greet_user("Neha")
Functions can return results instead of printing.
def add(a, b):
return a + b
result = add(10, 20)
print(result)
Printing shows output. Returning gives control to the program.
def greet(name="User"):
print("Hello", name)
greet()
greet("Rahul")
def calculate(a, b):
return a + b, a - b, a * b
sum_, diff, mul = calculate(10, 5)
print(sum_, diff, mul)
def outer():
def inner():
print("Inner function")
inner()
outer()
def login(username, password):
if username == "admin" and password == "1234":
return True
return False
if login("admin", "1234"):
print("Access Granted")
else:
print("Access Denied")
print(10 + 20)
print(10 + 20)
print(10 + 20)
def add():
print(10 + 20)
add()
add()
add()
Functions are not syntax. They represent thinking in blocks. This is where beginners start becoming developers.
Next part: File Handling — reading & writing files.