Master Python Programming From Scratch

Clear, interactive, and structured coding lessons designed for absolute beginners.

Defining & Calling Functions in Python

Python function is a reusable block of code that performs a specific task. Instead of writing the same code again and again, we can create a function once and call it whenever required.

CIIT Tip: Function ko ek small reusable machine samjho β€” input do, processing karo aur required output return karo.

1. What is a Function?

A function is a block of statements that is created to perform a particular task. Python mein function define karne ke liye def keyword use hota hai.

def greet():
    print("Welcome to CIIT Institute πŸ“©πŸ”“..!")

Upar humne greet() naam ka function define kiya hai. Sirf function define karne se code execute nahi hota. Function ko execute karne ke liye usse call karna padta hai.

2. Function Syntax

def function_name():
    # function body
    statement
Part Meaning
def Function define karne ka keyword
function_name Function ka naam
() Parameters ke liye parentheses
: Function block start karta hai
Function Body Function ke andar execute hone wala code

3. Defining a Function

Function define karte waqt hum function ka naam aur uske andar execute hone wala code specify karte hain.

def welcome():
    print("Have a nice day ..😁😍")
    print("Welcome to CIIT Training Institute πŸ“©πŸ”“..!")

Abhi function sirf define hua hai. Code execute karne ke liye function call karna hoga.

4. Calling a Function

Function ko call karne ke liye function name ke baad parentheses () use karte hain.

def welcome():
    print("Welcome to CIIT Training Institute πŸ“©πŸ”“..!")

welcome()
Output:
Welcome to CIIT Training Institute πŸ“©πŸ”“..!

5. Calling a Function Multiple Times

Ek function ko multiple times call kiya ja sakta hai.

def message():
    print("Learn Python with CIIT πŸ‘¨β€πŸ«")

message()
message()
message()
Output:
Learn Python with CIITπŸ‘¨β€πŸ«
Learn Python with CIIT πŸ‘¨β€πŸ«
Learn Python with CIIT πŸ‘¨β€πŸ«

6. Function with Parameters

Function ko data pass karne ke liye parameters use kiye jaate hain.

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

greet("Rahul")
greet("Priya")
Output:
Hello Rahul
Hello Priya

7. Function with Multiple Parameters

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

add(10, 20)
add(50, 30)
Output:
30
80

8. Returning a Value

Function ke andar calculation karke result ko bahar bhejne ke liye return statement use karte hain.

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

result = add(10, 20)

print(result)
Output:
30

9. print() vs return

print() return
Screen par output display karta hai Value function ke bahar bhejta hai
Further calculation ke liye directly use nahi hota Returned value ko variable mein store kar sakte hain
def square(number):
    return number * number

result = square(5)

print(result)

10. Default Parameter

Agar caller value provide nahi karta, toh default value use ki ja sakti hai.

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

greet()
greet("Amit")
Output:
Hello Student
Hello Amit

11. Keyword Arguments

Keyword arguments mein parameter ka naam specify karke value pass kar sakte hain.

def student_info(name, course):
    print("Name:", name)
    print("Course:", course)

student_info(
    course="Python Full Stack",
    name="Rahul"
)
Output:
Name: Rahul
Course: Python Full Stack

12. CIIT Example πŸ§‘β€πŸŽ“πŸŒ

Suppose CIIT mein student ka course aur fee display karna hai. Iske liye hum reusable function bana sakte hain.

def student_details(name, course, fee):
    print("Student:", name)
    print("Course:", course)
    print("Fee:", fee)

student_details(
    "Amit",
    "Python Full Stack",
    35000
)
Output:
Student: Amit
Course: Python Full Stack
Fee: 35000

13. Function Execution Flow

Step 1: Function define karo
↓
Step 2: Function call karo
↓
Step 3: Arguments pass karo
↓
Step 4: Function body execute hoti hai
↓
Step 5: Result return hota hai

14. Function Best Practices

  • Function ka meaningful naam rakho.
  • Ek function ko preferably ek clear responsibility do.
  • Repeated code ko function mein convert karo.
  • Required data parameters ke through pass karo.
  • Result chahiye ho toh return use karo.
  • Function names generally lowercase aur underscore style mein rakho.

15. Common Mistakes

# Wrong
def greet()
    print("Hello")

# Correct
def greet():
    print("Hello")
# Function defined but not called

def greet():
    print("Hello")

# Call the function
greet()
Important: Python mein indentation bahut important hai. Function ke andar ka code proper indentation ke saath likhna zaroori hai.

16. Interview Points

  • Python mein function define karne ke liye kaunsa keyword use hota hai?
  • Function ko execute kaise karte hain?
  • Parameter aur argument mein kya difference hai?
  • print() aur return mein difference kya hai?
  • Default parameter kya hota hai?
  • Keyword argument kya hota hai?

CIIT Learning Point

Functions Python programming ka core concept hai. Agar aap reusable, clean aur maintainable code likhna chahte ho, toh functions ka proper use karna bahut important hai.

CIIT Practice Tasks

  1. Greeting Function:
    greet() function create karo jo "Welcome to CIIT Training Institute" print kare.
  2. Addition Function:
    add(a, b) function create karo jo do numbers ka addition return kare.
  3. Square Function:
    square(number) function create karo jo number ka square return kare.
  4. Student Details:
    Student name, course aur city accept karne wala function create karo aur details display karo.
  5. Even Number Function:
    is_even(number) function create karo jo number even hai toh True aur otherwise False return kare.

Summary :

Python functions reusable blocks of code hote hain. Function ko def keyword se define kiya jata hai aur function name ke saath parentheses use karke call kiya jata hai. Parameters ke through data pass kar sakte hain aur return statement ke through result function ke bahar bhej sakte hain.