Python Lesson 11: Functions
PYTHON
AllComputerss
4/18/20261 min read


Functions are reusable blocks of code that perform a specific task. They help make your programs more organized, reduce repetition, and improve readability. By grouping related code into functions, you can call them whenever needed instead of rewriting the same logic multiple times.
Defining a Function
In Python, you define a function using the def keyword followed by the function name and parentheses.
def greet():
print("Hello, welcome to Python!")
# Calling the function
greet()
Output:
Hello, welcome to Python!
Parameters and Arguments
Functions can accept inputs called parameters. When you call the function, you provide arguments that are passed into those parameters.
def greet_user(name):
print(f"Hello, {name}!")
# Calling the function with an argument
greet_user("Alice")
Output:
Hello, Alice!
Return Values
Functions can return results using the return keyword.
def add_numbers(a, b):
return a + b
result = add_numbers(5, 3)
print(result)
Output:
8
Scope of Variables
Variables defined inside a function are local to that function and cannot be accessed outside of it.
def my_function():
x = 10
print(x)
my_function()
# print(x) # This would cause an error because x is local to the function
Why Use Functions?
Reusability: Write once, use many times.
Organization: Break down complex problems into smaller, manageable pieces.
Readability: Makes code easier to understand.
Maintainability: Easier to update and debug.
Conclusion
Functions are a cornerstone of Python programming. They allow you to structure your code logically, reuse functionality, and make your programs more efficient. As you progress, you’ll learn about advanced concepts like default parameters, recursion, and lambda functions, which expand the power of functions even further.
© 2026 AllComputerss. All rights reserved.