Python Functions - Day 1 & 2 Notes
Day 1: Introduction to Functions
What is a Function?
A function is a reusable block of code that performs a specific task.
Why Use Functions?
- Avoid repetition
- Organize and modularize code
- Easier debugging
- Better readability and reusability
Syntax:
def function_name():
# block of code
Example:
def greet():
print("Hello!")
greet()
Analogy:
Think of a function as a coffee machine. You press a button (call function), and it gives output
(coffee).
Page 1
Python Functions - Day 1 & 2 Notes
Practice Zone:
1. hello_world() - prints "Hello, World!"
2. add(a, b) - returns sum
3. greet(name) - prints greeting
4. square_numbers() - prints square of 1 to 5
Day 2: Return and Variable Scope
Return Statement
Used to send result back to where the function was called.
Example:
def add(a, b):
return a + b
result = add(2, 3)
Without return result is None
With return result = output of function
Variable Scope:
- Local: Inside function only
- Global: Outside and accessible everywhere
Use 'global' keyword to modify global variables inside function.
Page 2
Python Functions - Day 1 & 2 Notes
Example:
x = 10
def change():
global x
x = 20
Practice Tasks:
1. area_of_circle(radius)
2. convert_to_uppercase(text)
3. max_of_three(a, b, c)
4. is_even(n) + print_even_numbers(start, end)
Summary Table:
- return: sends output
- local vs global scope
- global keyword usage
Page 3