đ§Š Python Functions â Reusable Blocks of Code
Introduction đ
A function in Python is a reusable block of code that performs a specific task. Functions help make programs more organized, readable, and efficient.
Note
đĄ Define once â Use many times. Functions help avoid repeating code.
1. Defining a Function đ§ą
define_function.py
def greet():
print("Hello, welcome to Python!")2. Calling a Function đ
call_function.py
greet()âī¸ The parentheses () are required to execute a function.
3. Function With Parameters đ¯
function_params.py
def greet(name):
print("Hello", name)
greet("Sathish")Note
âī¸ Parameters allow input values to be passed into the function.
4. Function With Multiple Parameters â
multiple_params.py
def add(a, b):
print(a + b)
add(10, 20)5. Return Statement âŠī¸
return_statement.py
def add(a, b):
return a + b
result = add(10, 5)
print(result)Note
âī¸ return sends a value back to the caller.
âī¸ Without return, a function gives None.
âī¸ Without return, a function gives None.
6. Default Parameters đ§
default_params.py
def greet(name="Guest"):
print("Hello", name)
greet()
greet("Kumar")âī¸ If no argument is given, default value is used.
7. Keyword Arguments đī¸
keyword_args.py
def student(name, age):
print(name, age)
student(age=21, name="Sathish")Note
âī¸ Order doesnât matter when using keyword arguments.
8. Arbitrary Arguments (*args) đĸ
*args allows any number of positional arguments.
args_example.py
def add(*nums):
print(sum(nums))
add(1, 2, 3, 4)9. Arbitrary Keyword Arguments (**kwargs) đ§°
**kwargs allows passing multiple named arguments.
kwargs_example.py
def display(**info):
print(info)
display(name="Sathish", age=25)10. Return Multiple Values đ
multiple_return.py
def calc(a, b):
return a + b, a - b, a * b
x, y, z = calc(10, 5)
print(x, y, z)11. Nested Functions đĒ
nested_function.py
def outer():
print("Outer function")
def inner():
print("Inner function")
inner()
outer()12. Lambda Functions âĄ
Lambda functions are small, anonymous functions written in one line.
lambda_example.py
square = lambda x: x * x
print(square(5))13. Docstrings đ
docstring.py
def add(a, b):
"""This function returns the sum of two numbers."""
return a + b14. Scope of Variables đ
Local Scope
local_scope.py
def func():
x = 10 # local variable
print(x)
func()Global Scope
global_scope.py
x = 20 # global variable
def func():
print(x)
func()15. Using global Keyword đ
global_keyword.py
count = 0
def increment():
global count
count += 1
increment()
print(count)Note
â ī¸ Use global carefully â can make code harder to maintain.
16. Pass Statement in Functions đ§
pass_function.py
def todo():
pass # function not implemented yet17. Real-World Examples đ
Login Function
login_example.py
def authenticate(username, password):
if username == "admin" and password == "1234":
return "Access Granted"
return "Access Denied"
print(authenticate("admin", "1234"))Calculator Function
calculator.py
def calc(a, b, op):
if op == "+":
return a + b
elif op == "-":
return a - b
elif op == "*":
return a * b
elif op == "/":
return a / b
print(calc(10, 5, "*"))Reusable Greeting Function
greeting.py
def greet_user(name):
return f"Welcome, {name}!"
print(greet_user("Kumar"))Conclusion đ
>>âFunctions make your code modular, reusable, and clean â the foundation of good programming.â â¨
You now have a complete understanding of Python Functions! Want the next tutorial? Try Arguments & Parameters, Lambda Functions, Modules, or OOP Concepts. Just tell me! đ