Class 10 • Python Programming

Functions Made Simple

Learn what functions are, how to use them, and the difference between passing by value and passing by reference — with easy, runnable Python code.

What is a Function?

A function is a small, reusable block of code that does one specific job. Think of it like a recipe — you write it once, and then you can use it as many times as you want without writing the same code again and again.

Why use functions?
Instead of writing the same code 10 times, write a function once and call it 10 times. Your program becomes shorter, cleaner, and easier to fix.

A Simple Real-Life Example

Imagine you have a function called make_tea(). Every time you want tea, you don't write the whole recipe — you just say "make_tea" and it does the work. In programming, it works the same way!

Program 1 — Your First Function
# Program to print a message using a function

def greet():
    print("Hello! Welcome to Class 10.")

# Function call — it prints the message
greet()

Output:

Output
Hello! Welcome to Class 10.
Remember: def greet(): is the function definition (the recipe), and greet() below it is the function call (asking it to do the job).

Parts of a Function

Every function has these parts:

  1. def keyword — Tells Python "I am creating a function."
  2. Function Name — The name you give it (e.g., add, print_msg, greet).
  3. Parameters (optional) — Values you pass into the function so it can work with them. Inside the function, they act like local variables.
  4. Body — The indented code that does the actual work.
Anatomy of a Function
def add(a, b):
    return a + b

   ↑     ↑  ↑         ↑
   |     |  |         body (the work)
   |     |  parameters (inputs)
   |     function name
   def keyword (starts the function)
No types needed! Unlike C++, Python doesn't need you to declare types like int or float. Python figures it out automatically — that's one reason Python is easy to learn!

Program with Parameters and Return Value

Program 2 — Add Two Numbers
# Function that adds two numbers and returns the result

def add(a, b):
    return a + b

result = add(5, 3)
print("5 + 3 =", result)

Output: 5 + 3 = 8

What Can a Function Return?

Python functions can return any type of value. You don't need to declare the type — just use return.

What it ReturnsExample
Nothing (like void) def greet(): print("Hi")
A number def add(a, b): return a + b
A string def shout(w): return w.upper()
A list def nums(): return [1, 2, 3]
True or False def is_even(n): return n % 2 == 0
Easy rule: If your function needs to give back a value, use return. If it just does something (like printing), you don't need return — it returns None automatically.
Program 3 — Factorial Using a Function
# Find factorial of a number using a function

def factorial(n):
    fact = 1
    for i in range(1, n + 1):
        fact = fact * i
    return fact

print("Factorial of 5 =", factorial(5))

Output: Factorial of 5 = 120

Pass by Value (Immutable Types)

In Python, when you pass a number, string, or boolean (called immutable types) to a function, Python sends a copy of the value. The function works on that copy. Whatever happens inside the function does NOT change the original variable.

Think of it like this: You photocopy your homework and give the photocopy to your friend. Your friend writes on the photocopy — but your original homework stays unchanged!
Program 4 — Passing a Number (Original stays the same)
# Demonstrate that integers don't change outside the function

def triple(x):
    x = x * 3          # x is a copy — changes don't go back
    print("Inside function: x =", x)

num = 10
print("Before calling: num =", num)

triple(num)        # A COPY of num is sent

print("After calling:  num =", num)

Output:

Output
Before calling: num = 10
Inside function: x = 30
After calling:  num = 10        <-- Original unchanged!

See? The original num stayed 10 because the function only worked on a copy called x.

Strings Also Stay Unchanged

Program 5 — Passing a String
# Strings are also immutable — changes don't go back

def change_name(name):
    name = "Rahul"         # reassigns the local copy
    print("Inside function: name =", name)

my_name = "Amit"
change_name(my_name)
print("After function:   my_name =", my_name)

Output:

Output
Inside function: name = Rahul
After function:   my_name = Amit        <-- Still "Amit"!

Pass by Reference (Mutable Types)

When you pass a list, dictionary, or set (called mutable types) to a function, Python sends a reference to the same object. So if the function modifies it, the original variable DOES change!

Think of it like this: You share a Google Doc with your friend. Your friend edits it — and now YOUR doc has the changes too, because you're both looking at the same document!
Program 6 — Passing a List (Original changes!)
# Lists are mutable — changes DO go back

def add_item(items, new_item):
    items.append(new_item)    # modifies the SAME list
    print("Inside function:", items)

fruits = ["apple", "banana"]
print("Before calling:", fruits)

add_item(fruits, "mango")

print("After calling: ", fruits)

Output:

Output
Before calling: ['apple', 'banana']
Inside function: ['apple', 'banana', 'mango']
After calling:  ['apple', 'banana', 'mango']        <-- Original changed!

See the difference? Now the original fruits list got updated because lists are mutable.

Another Example: Sort a List

Program 7 — Sort a List In-Place
# Sorting modifies the same list

def sort_list(numbers):
    numbers.sort()           # sorts in-place
    print("Inside function:", numbers)

marks = [45, 12, 78, 34]
sort_list(marks)
print("After sorting: ", marks)

Output:

Output
Inside function: [12, 34, 45, 78]
After sorting:  [12, 34, 45, 78]        <-- Sorted permanently!

Swapping Two Values (Python Style!)

Program 8 — Swap Using a List
# Using a list to swap — this works because lists are mutable

def swap(pair):
    pair[0], pair[1] = pair[1], pair[0]   # swap inside the list

data = [7, 4]
print("Before swap:", data)
swap(data)
print("After swap: ", data)

Output:

Output
Before swap: [7, 4]
After swap:  [4, 7]        <-- Swapped!

Pass by Value vs Pass by Reference

FeaturePass by Value (Immutable)Pass by Reference (Mutable)
What types? int, float, str, bool, tuple list, dict, set
What is sent? A copy of the value Reference to the same object
Original changes? No Yes (if modified in-place)
How to modify? Cannot modify directly Use .append(), .sort(), [i] =
When to use? When you don't want original to change When you want to modify the original
Exam tip: Numbers and strings = immutable (copy, original stays). Lists and dicts = mutable (same object, original changes). That's the key rule!

Final Example: What Doesn't Work

Program 9 — Trying to Change an Integer (Fails!)
# What happens when you try to change a number?

def try_change(a, b):
    a, b = b, a              # swap inside the function
    print("Inside function: a =", a, ", b =", b)

x, y = 7, 4
try_change(x, y)
print("After function:   x =", x, ", y =", y)

Output:

Output
Inside function: a = 4 , b = 7
After function:   x = 7 , y = 4        <-- Swapped inside, but NOT outside!

This proves that integers cannot be changed outside the function. To swap actual variables, you need a workaround like using a list (see Program 8).

The Golden Rule of Python:
Immutable types (int, str, float, bool, tuple) → function gets a copy, original stays the same.
Mutable types (list, dict, set) → function gets the same object, original CAN change.