๐ Phase 1: Programming Fundamentals
๐ Topic 6: Python Functions
So far, you've learned variables, operators, input/output, conditional statements, and loops. As your programs grow larger, writing the same code repeatedly becomes inefficient. That's where functions come in.
A function is a reusable block of code that performs a specific task. Functions make your code cleaner, easier to maintain, and reusable.
Functions are heavily used in Data Science, Machine Learning, and AI because they allow you to organize complex workflows into smaller, manageable pieces.
๐น 1. What is a Function?
A function is a named block of code that executes only when it is called.
Instead of writing the same logic multiple times, you write it once inside a function and reuse it whenever needed.
Example
def greet():
print("Welcome to Data Science!")
greet()
Output
Welcome to Data Science!
๐น 2. Why Do We Use Functions?
Functions help you:
โ Avoid writing duplicate code
โ Improve code readability
โ Make debugging easier
โ Reuse code in multiple places
โ Build modular applications
๐น 3. Defining a Function
Syntax
def function_name():
# Function body
Example:
def welcome():
print("Hello, World!")
welcome()
๐น 4. Function Parameters
Parameters allow you to pass information into a function.
def greet(name):
print("Hello", name)
greet("Deepak")
Output
Hello Deepak
Here, "name" is called a parameter.
๐น 5. Function Arguments
When calling a function, the values you pass are called arguments.
def square(number):
print(number * number)
square(5)
Output
25
Here:
โข "number" โ Parameter
โข "5" โ Argument
๐น 6. Returning Values
A function can return a value using the return keyword.
def add(a, b):
return a + b
result = add(10, 20)
print(result)
Output
30
Using return allows the function's result to be stored or used later.
๐น 7. Default Parameters
You can assign default values to parameters.
def greet(name="Guest"):
print("Hello", name)
greet()
greet("Rahul")
Output
Hello Guest
Hello Rahul
๐น 8. Multiple Return Values
A function can return more than one value.
def calculate(a, b):
return a + b, a * b
sum_value, product = calculate(4, 5)
print(sum_value)
print(product)
Output
9
20
๐น 9. Scope of Variables
Variables created inside a function are called local variables.
def demo():
message = "Inside Function"
print(message)
demo()
Trying to access
message
outside the function will produce an error because it exists only inside the function.
๐น 10. Built-in Functions
Python provides many ready-to-use functions.
Examples:
numbers = [5, 2, 8, 1]
print(len(numbers))
print(max(numbers))
print(min(numbers))
print(sum(numbers))
Output
4
8
1
16
๐น 11. Real-World Data Science Example
Calculate the average marks of students.
def average(marks):
return sum(marks) / len(marks)
scores = [80, 75, 92, 88]
print(average(scores))
Output
83.75
Functions like this are commonly used while cleaning data, calculating statistics, and building machine learning pipelines.
๐น 12. Common Mistakes
โ Forgetting to Call the Function
def greet():
print("Hello")
# Nothing happens because the function isn't called.
Correct:
greet()
โ Forgetting to Return a Value
def add(a, b):
a + b
# Correct:
def add(a, b):
return a + b
๐ฏ Practice Questions
1. Write a function to add two numbers.
2. Create a function to calculate the square of a number.
3. Write a function that checks whether a number is even or odd.
4. Create a function to calculate the average of a list.
5. Write a function that returns the largest of three numbers.
Double Tap โค๏ธ For Part-7