Introduction to Functions in Python
A function in Python is a block of reusable code that performs a specific task.
Instead of writing the same code repeatedly, you write it once inside a function and “call” it whenever needed.
Functions help to make programs:
✔ Modular
✔ Reusable
✔ Easy to read
✔ Easy to test
✔ Easy to debug
What is a Function?
A function is a group of statements that perform a particular operation and return a result.
Python provides two types of functions:
- Built-in Functions (predefined)
- User-defined Functions (created by the programmer)
1. Built-in Functions
Python comes with many built-in functions such as:
print()input()len()type()sum()max()min()range()
Example:
print(len("Python"))
2. User-defined Functions
These are functions created by the user using the def keyword.
Why Use Functions?
✔ Code reusability
Write once → use many times.
✔ Better organization
Break a large program into smaller parts.
✔ Testing and debugging
Functions isolate logic → easier to test.
✔ Avoid repetition
Removes duplicate code.
✔ Increases readability
Defining a Function
A user-defined function is created using the def keyword.
Syntax:
def function_name(parameters):
statements
return value
Example: Simple function
def greet():
print("Hello, Welcome to Python!")
Calling the function:
greet()
Function with Parameters (Arguments)
def add(a, b):
print(a + b)
add(10, 20) # Output: 30
Function with Return Statement
A function can return a value using the return keyword.
def square(x):
return x * x
result = square(5)
print(result)
Types of Function Arguments
Python supports different types of arguments:
- Required arguments
- Default arguments
- Keyword arguments
- Variable-length arguments (
*args,**kwargs)
We can go deeper if you want.
Flow of Function Execution
- Function is defined
- Program reaches function call
- Control shifts to function body
- Function executes
- Function returns (if return is used)
- Control goes back to calling statement
Docstring (Function Documentation)
A docstring explains what a function does.
def greet():
"""This function prints a greeting message."""
print("Hello!")
print(greet.__doc__)
Lambda Functions (Anonymous Functions)
Small, one-line functions created using lambda.
square = lambda x: x * x
print(square(6)) # 36
Example Programs Using Functions
Example 1: Check even or odd
def check(num):
if num % 2 == 0:
return "Even"
else:
return "Odd"
print(check(7))
Example 2: Calculate factorial
def factorial(n):
if n == 0 or n == 1:
return 1
return n * factorial(n-1)
print(factorial(5))
When Should We Use Functions?
Use a function when:
- A piece of code is used multiple times
- You want a modular program
- You want to group related logic
- You want to return results from computation
Conclusion
Functions are a fundamental part of Python programming.
They make programs:
✔ simpler
✔ modular
✔ efficient
✔ reusable
Understanding functions is essential before learning:
▶ Modules
▶ Object-Oriented Programming
▶ Data structures
▶ Recursion
