By the end of this lesson, you will be able to:
🎯 Define and call functions using the def keyword.Â
🎯 Pass one or more parameters to a function.
🎯 Return a value from a function.
🎯 Understand local vs global variable scope.
🎯 Apply functions to real coding problems.
In this lesson, you will learn how to use functions in Python. Functions are like machines or mini-programs inside your code. You give them some input (parameters), they do something, and then they give you output (return values). Instead of repeating yourself, you can write a function once and use it many times.Â
Python Code
def greet(): # function name
   print("Hello! This is my first function.") # code block (indented)
greet() # To use the function (call it)
Open Google Colab, create a new notebook, and run the examples above in new code cells.
💡If you need to greet users in multiple places, instead of repeating print("Hello!") everywhere, just call greet().
Python Code
def greet(name):Â # 'name' is a parameter
   print("Hello,", name)
greet("Ali")Â # "Ali is an argument
Python Code
def add_numbers(a, b):
   return a + b Â
# a + b is returned by the function and stored in result
result = add_numbers(5, 7) # Calling the function
print("Sum:", result)
Run the examples above in new code cells.
print() shows something on the screen but does not return a value to use later.
return gives back a value that you can store, modify, or print later.
Python Code
# Defining the function (Parameter is 'name')
def greet(name):
   return "Hello, " + name
# Calling the function (Argument is 'Ali')
message = greet("Ali")
# 'Hello, Ali' is the return value
print(message)
Run the examples above in new code cells.
Python Code
def square(x):
   return x * x
result = square(4)
print("Square:", result)Â # Output: Square: 16
Run the examples above in new code cells.
def square(x):
   print(x * x) # Just displays result, doesn’t give it back
square(5)Â # Output: 25
Run the examples above in new code cells.
(1) Local variable: Created inside a function. Can only be used there.
(2) Global variable: Created outside any function. Can be accessed anywhere.
Python Code
def show_local():
   x = 10 # local
   print("Inside:", x)
x = 5Â # global
show_local()
print("Outside:", x)
Run the examples above in new code cells.
Python Code
x = 10
def modify():
   global x
   x = 20
modify()
print(x)Â # Output: 20
Run the examples above in new code cells.
def multiply(a, b):
   return a * b
product = multiply(4, 5)
print("Product:", product)
Run the examples above in new code cells.
def check_even(num):
   if num % 2 == 0:
       return "Even"
   else:
       return "Odd"
print(check_even(7))Â # Output: Odd
Run the examples above in new code cells.
🧠Answer these to test your understanding.
A function must always return a value. (True / False)
Which keyword is used to define a function?
a) func
b) def
c) function
What happens if you try to access a local variable outside the function?
What’s the difference between a parameter and an argument?
How is a local variable different from a global one?
🔧 Instructions:
Function to calculate area of a rectangle
✅ Define a function named calculate_area
✅ It should take two parameters: length and width
✅ It should return the area (length × width)
Function to find maximum of three numbers
✅ Define a function named find_max
✅ Accept three parameters
✅ Return the highest value using conditional logic or the built-in max() function
Function to convert celsius to fahrenheit
✅ Formula: F = C × 9/5 + 32
🔹 A function is a reusable block of code that performs a specific task.
🔹 Functions are defined using the def keyword.
🔹 Parameters are names listed in the function definition to accept inputs.
🔹 Arguments are actual values passed to a function when it is called.
🔹 The return statement sends a value back to the place where the function was called.
🔹 If no return is used, the function returns None by default.
🔹 Local variables exist only inside the function where they are created.
🔹 Global variables are accessible from anywhere in the program.
🔹 Functions help make code cleaner, easier to read, more organized, and more reusable.