Table of Contents
- Day 4: Functions – Modularize Your Code with Python
- What is a Function?
- Defining a Function
- Function Arguments
- Return Statement
- Scope of Variables
- Lambda Functions
- Learn more about Lambda Function
- Recursion
- Quick Practice
- Wrap-Up
- Day 3: Control Flow – Making Decisions in Python
- Day 5: Exploring Data Structures in Python
- Back to Home
Day 4: Functions – Modularize Your Code with Python
Welcome to Day 4!
Today we are going to explore Functions in Python. Functions help you organize your code by breaking it into smaller, reusable chunks. Let’s learn how to define, use, and optimize functions.
What is a Function?
A function is a block of reusable code that performs a specific task. Functions help reduce redundancy and make your code more organized and readable.
Example:
def greet(name):
print("Hello, " + name + "!")
You can call the function as:
great("Alice")
Defining a Function
To define a function, we use the def keyword, followed by the function name, parentheses, and a colon. The function body contains the code that will execute when the function is called.
Syntax:
def function_name(parameters):
# Code to execute
Example:
def add_numbers(a, b):
return a + b
Function Arguments
Functions can accept parameters (or arguments) that allow you to pass data to them.
Types of Arguments:
1. Positional Arguments:
These are arguments passed based on their position.
def multiply(x, y):
return x * y
result = multiply(2, 3) # x = 2, y = 3
2. Keyword Arguments:
These are arguments passed by specifying the name of the parameter.
def introduce(name, age):
print(f"My name is {name} and I am {age} years old.")
introduce(name="John", age=25)
3. Default Arguments:
You can assign default values to parameters.
def greet(name="Guest"):
print(f"Hello, {name}!")
greet() # Uses default value
greet("Alice") # Overrides default value
Return Statement
A function can return a value using the return statement. This allows you to pass data back to the caller.
Example:
def square(number):
return number ** 2
result = square(4) # result will be 16
Scope of Variables
1. Local Variables: Variables defined inside a function are local to that function.
2. Global Variables: Variables defined outside of any function are global and can be accessed throughout the program.
Example:
x = 10 # Global variable
def example_function():
x = 5 # Local variable
print("Local x:", x) # Output: 5
print("Global x:", globals()['x']) # Output: 10
example_function()
Lambda Functions
A lambda function is a small anonymous function that is defined with the lambda keyword. It can take any number of arguments but can only have one expression.
*Syntax:
lambda arguments: expression
Example:
square = lambda x: x ** 2
result = square(5) # Output: 25
Learn more about Lambda Function here
Recursion
Recursion occurs when a function calls itself. It is useful for problems that can be broken down into smaller sub-problems.
Example (Factorial):
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n - 1)
print(factorial(5)) # Output: 120
Quick Practice
- Write a function to check whether a number is prime.
- Create a function to calculate the Fibonacci sequence up to a given number.
- Write a function that accepts a string and returns the reversed string.
Wrap-Up
Today, we learned:
How to define and use functions in Python.
The importance of function arguments and return values.
The difference between local and global variables.
The power of lambda functions and recursion.
In the next lesson, we’ll explore Data Structures in Python, which will help you organize and manipulate data more effectively.
Keep coding, and stay curious! 🚀