🟢 Beginner  ·  Lesson 10

Functions in Python

Python में Functions

What is a Function?

A function is a reusable block of code that does one job. Write it once, call it many times. This keeps programs short, organized and easy to fix.

💡 Why functions matter

Instead of copying the same 5 lines in 10 places, you put them in a function and call it. One fix updates everywhere — this is the heart of clean B.Tech-level code.

Defining a Function

Use the def keyword, a name, brackets, and a colon. The indented block is the body.

Python – def.py
def say_hello():
    print("Hello!")

say_hello()    # call it
say_hello()    # call again
Hello! Hello!

Parameters & Arguments

A function can accept inputs (parameters). The values you pass in are arguments.

Python – param.py
def greet(name):
    print("Hello,", name)

greet("Aman")
greet("Riya")
Hello, Aman Hello, Riya

Return Values

return sends a result back to the caller so it can be stored or reused.

Python – ret.py
def square(n):
    return n * n

result = square(5)
print(result)
25

Program 1: Greeting Function

Python – greet_fn.py
def greet(name, time):
    print(f"Good {time}, {name}!")

greet("Aman", "morning")
greet("Riya", "evening")
Good morning, Aman! Good evening, Riya!

Program 2: Area Calculator

Python – area.py
def rectangle_area(length, width):
    return length * width

def circle_area(radius):
    return 3.14 * radius * radius

print("Rectangle:", rectangle_area(10, 5))
print("Circle   :", circle_area(7))
Rectangle: 50 Circle : 153.86

Two small functions, each doing one calculation and returning the answer.

Program 3: Default & Keyword Arguments

Python – defaults.py
def power(base, exp=2):     # exp defaults to 2
    return base ** exp

print(power(5))           # uses default exp=2
print(power(2, 3))        # exp=3
print(power(exp=4, base=2))  # keyword args
25 8 16
  • exp=2 is a default — used if you do not pass it.
  • You can pass arguments by name (keyword) in any order.

Program 4: Factorial Function

Python – fact_fn.py
def factorial(n):
    result = 1
    for i in range(1, n + 1):
        result *= i
    return result

print("5! =", factorial(5))
print("0! =", factorial(0))
5! = 120 0! = 1

Program 5: *args — Any Number of Inputs

Python – args.py
def total(*numbers):       # *args collects all into a tuple
    return sum(numbers)

print(total(10, 20))
print(total(1, 2, 3, 4, 5))
30 15

*numbers lets the function accept any count of values, gathered into a tuple.

Local vs Global Variables

  • A variable made inside a function is local — it exists only there.
  • A variable made outside is global — visible everywhere.
  • To change a global from inside, use the global keyword.

Common Mistakes

  • Forgetting return — the function then gives None.
  • Defining a function but never calling it.
  • Wrong number of arguments passed.
  • Confusing print (shows) with return (gives back).

Practice Tasks

  1. Write a function that returns the larger of two numbers.
  2. Write a function to check if a number is prime.
  3. Write a function that takes a name and an optional greeting (default "Hello").
  4. Write a function using *args to find the average of any numbers.

Summary

  • Functions are reusable code blocks defined with def.
  • Parameters receive inputs; return sends back a result.
  • Defaults, keyword args and *args make functions flexible.
  • Variables inside a function are local by default.

Function क्या है?

Function code का दोबारा use होने वाला block है जो एक काम करता है। एक बार लिखो, कई बार call करो। इससे programs छोटे, organized और ठीक करने में आसान रहते हैं।

💡 Functions क्यों ज़रूरी

एक ही 5 lines को 10 जगह copy करने के बजाय, उन्हें function में रखें और call करें। एक fix हर जगह update — यही clean B.Tech-level code का दिल है।

Function Define करना

def keyword, एक नाम, brackets और colon use करें। indented block body है।

Python – def.py
def say_hello():
    print("Hello!")

say_hello()    # call करें
say_hello()    # फिर call करें
Hello! Hello!

Parameters और Arguments

Function inputs ले सकता है (parameters)। आप जो values देते हैं वे arguments हैं।

Python – param.py
def greet(name):
    print("Hello,", name)

greet("Aman")
greet("Riya")
Hello, Aman Hello, Riya

Return Values

return result को caller के पास वापस भेजता है ताकि उसे store या reuse किया जा सके।

Python – ret.py
def square(n):
    return n * n

result = square(5)
print(result)
25

Program 1: Greeting Function

Python – greet_fn.py
def greet(name, time):
    print(f"Good {time}, {name}!")

greet("Aman", "morning")
greet("Riya", "evening")
Good morning, Aman! Good evening, Riya!

Program 2: Area Calculator

Python – area.py
def rectangle_area(length, width):
    return length * width

def circle_area(radius):
    return 3.14 * radius * radius

print("Rectangle:", rectangle_area(10, 5))
print("Circle   :", circle_area(7))
Rectangle: 50 Circle : 153.86

दो छोटे functions, हर एक एक calculation करके answer लौटाता है।

Program 3: Default और Keyword Arguments

Python – defaults.py
def power(base, exp=2):     # exp default 2
    return base ** exp

print(power(5))           # default exp=2
print(power(2, 3))        # exp=3
print(power(exp=4, base=2))  # keyword args
25 8 16
  • exp=2 default है — न देने पर इस्तेमाल होता है।
  • Arguments को नाम (keyword) से किसी भी order में दे सकते हैं।

Program 4: Factorial Function

Python – fact_fn.py
def factorial(n):
    result = 1
    for i in range(1, n + 1):
        result *= i
    return result

print("5! =", factorial(5))
print("0! =", factorial(0))
5! = 120 0! = 1

Program 5: *args — कितने भी Inputs

Python – args.py
def total(*numbers):       # *args सबको tuple में जमा करता है
    return sum(numbers)

print(total(10, 20))
print(total(1, 2, 3, 4, 5))
30 15

*numbers function को कितनी भी values लेने देता है, जो tuple में इकट्ठा होती हैं।

Local बनाम Global Variables

  • Function के अंदर बना variable local है — सिर्फ वहीं रहता है।
  • बाहर बना variable global है — हर जगह दिखता है।
  • अंदर से global बदलने के लिए global keyword use करें।

सामान्य गलतियाँ

  • return भूलना — तब function None देता है।
  • Function define करना पर कभी call न करना।
  • गलत संख्या में arguments देना।
  • print (दिखाता है) और return (वापस देता है) में confusion।

Practice Tasks

  1. एक function लिखें जो दो numbers में बड़ा लौटाए।
  2. एक function लिखें जो check करे number prime है या नहीं।
  3. एक function जो नाम और optional greeting ले (default "Hello")।
  4. *args से किसी भी numbers का average निकालने वाला function।

सारांश

  • Functions def से बनाए reusable code blocks हैं।
  • Parameters inputs लेते हैं; return result वापस भेजता है।
  • Defaults, keyword args और *args functions को flexible बनाते हैं।
  • Function के अंदर के variables default रूप से local होते हैं।
← Back to Python Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

WhatsApp