๐Ÿ“˜ Lesson 7: Functions in Python examples

๐Ÿ”น 1. What is a Function?

A function is a block of reusable code that performs a specific task.

๐Ÿ‘‰ Instead of writing the same code again and again, we define it once and reuse it.


๐Ÿ”น 2. Why Use Functions?

โœ” Avoid repetition
โœ” Make code reusable
โœ” Improve readability
โœ” Easy debugging


๐Ÿ”น 3. Defining a Function

Syntax

def function_name():
    # code block

Example 1: Simple Function

def greet():
    print("Hello, welcome to Python")

๐Ÿ”น Calling the function:

greet()

Output

Hello, welcome to Python

๐Ÿ“Œ Explanation

  • def keyword defines a function
  • greet is the function name
  • Function runs only when called

๐Ÿ”น 4. Function with Parameters

Parameters allow us to pass data into a function.


Example 2: Greeting with Name

def greet(name):
    print("Hello", name)

๐Ÿ”น Calling the function:

greet("Sameer")
greet("Ayesha")

Output

Hello Sameer
Hello Ayesha

๐Ÿ“Œ Explanation

  • name is a parameter
  • Values passed are called arguments

๐Ÿ”น 5. Function with Multiple Parameters


Example 3: Student Details

def student_info(name, age):
    print("Name:", name)
    print("Age:", age)

๐Ÿ”น Function call:

student_info("Saddam", 24)

Output

Name: Saddam
Age: 24

๐Ÿ”น 6. Function with Return Value

Functions can return a value using return.


Example 4: Add Two Numbers

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

๐Ÿ”น Function call:

result = add(10, 5)
print(result)

Output

15

๐Ÿ“Œ Explanation

  • return sends value back to caller
  • Code after return does not execute

๐Ÿ”น 7. Return vs Print


Example 5

def show_sum(a, b):
    print(a + b)

def get_sum(a, b):
    return a + b
show_sum(5, 5)
print(get_sum(5, 5))

Output

10
10

๐Ÿ“Œ Explanation

  • print() โ†’ displays value
  • return โ†’ gives value for further use

๐Ÿ”น 8. Function with User Input


Example 6

def square_number():
    num = int(input("Enter a number: "))
    print("Square:", num * num)

๐Ÿ”น Function call:

square_number()

Sample Output

Enter a number: 4
Square: 16

๐Ÿ”น 9. Default Parameter Value


Example 7

def greet(name="Guest"):
    print("Hello", name)
greet()
greet("Chandini")

Output

Hello Guest
Hello Chandini

๐Ÿ“Œ Explanation

  • Default value is used if argument not provided

๐Ÿ”น 10. Keyword Arguments


Example 8

def employee(name, salary):
    print("Name:", name)
    print("Salary:", salary)
employee(salary=30000, name="Gousya")

Output

Name: Gousya
Salary: 30000

๐Ÿ”น 11. Function Calling Another Function


Example 9

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

def calculate():
    result = add(3, 4)
    print("Result:", result)
calculate()

Output

Result: 7

๐Ÿ”น 12. Real-Life Example: Salary Calculator


Example 10

def total_salary(basic, bonus):
    return basic + bonus
salary = total_salary(25000, 5000)
print("Total Salary:", salary)

Output

Total Salary: 30000

๐Ÿ”น 13. Common Mistakes (IMPORTANT)

โŒ Forgetting to call function
โŒ Wrong indentation
โŒ Missing return value
โŒ Confusing print with return


๐Ÿง  Final Summary

โœ” def is used to define a function
โœ” Functions must be called to execute
โœ” Parameters receive values
โœ” return sends data back
โœ” Functions make code reusable


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *