Python · Chapter 2.6 · Intermediate level

Functions (def — Doraemon's Gadget Pocket!)

फ़ंक्शन्स (def — डोरेमोन की जादुई मशीन!)

Think of a Function like a gadget in Doraemon's pocket (or a juicer machine): you build the machine once using def, give it a name like square(n), and tell it what to do inside. After that, whenever you want, just call square(5) or square(9) and your machine instantly gives the answer!

Function (def) को डोरेमोन का गैजेट या जूसर मशीन समझो: एक बार def square(n): लिखकर मशीन बना दो और बता दो कि अंदर क्या काम करना है। उसके बाद जब मन करे बस उसका नाम लेकर बुलाओ — square(5) या square(9) — मशीन तुरंत जवाब तैयार कर देगी!

In the classroom you can run and edit this code in your browser, get hints and mark the chapter as done. क्लास में यह code ब्राउज़र में चलाकर बदल सकते हो।

Words and symbols in this chapter

def
Define — tells Python we are building a new reusable function machine.
Define — Python को बताता है कि हम एक नई फ़ंक्शन मशीन बना रहे हैं।
square(n):
square is the function's name; n inside ( ) is the input slot (parameter).
square मशीन का नाम है और (n) वह इनपुट स्लॉट है जहाँ नंबर जाएगा।
print(n * n)
Indented body — multiplies input n by itself (5 * 5 = 25).
4 स्पेस अंदर लिखा काम — इनपुट n का खुद से गुणा करता है (5 * 5 = 25)।
square(5)
Function Call — switches ON the machine with input 5!
फ़ंक्शन कॉल — मशीन में 5 डालकर उसे चालू करता है!

Ready example

def square(n):
    ans = n * n
    print(n, "ka square hai:", ans)

square(5)
square(9)

What you will see: Doraemon's Function Machine diagram shows your input 5 entering square(n) and outputting 25!

प्रिव्यू में फ़ंक्शन मशीन कार्ड दिखेगा जिसमें इनपुट 5 अंदर जाते और 25 बाहर निकलते दिखेगा!

Line by line

def square(n):
Builds a machine named square that accepts one number n.
square नाम की मशीन बनाई जो एक नंबर n लेती है।
ans = n * n / print(...)
Calculates n * n and prints the result.
n * n निकालकर स्क्रीन पर छापती है।
square(5) / square(9)
Uses the same machine twice — first for 5 (25), then for 9 (81)!
एक ही मशीन को दो बार चलाया — 5 से 25 और 9 से 81 मिला!

Common mistake

✗ Wrong

def add(a, b):
    return a + b   # defined, never called!

✓ Right

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

print(add(4, 6))

Defining def only builds the machine — you must call add(4, 6) to actually run it!

सिर्फ़ def लिखने से मशीन बनती है, चलती नहीं — चलाने के लिए नीचे नाम लिखकर add(4, 6) कॉल करना पड़ता है!

Your task

Define def square(n): that prints n * n, and call square(5) to output 25!

def square(n): फ़ंक्शन बनाओ जो print(n * n) करे, और नीचे square(5) कॉल करके 25 प्रिंट करवाओ!

  1. Line 1: Write def square(n):.
    लाइन 1: def square(n): लिखो।
  2. Line 2: Indent 4 spaces and write print(n * n).
    लाइन 2: 4 स्पेस देकर print(n * n) लिखो।
  3. Line 4 (no spaces at front!): Write square(5) and click ▶ Run & Preview!
    आगे बिना स्पेस दिए square(5) लिखो और ▶ Run & Preview दबाओ!
Starter code for the task
# TODO 1: def square(n): likho (colon : ke saath)
# TODO 2: agli line mein 4 spaces dekar print(n * n)
# TODO 3: sabse neeche (bina spaces) square(5) call karo - 25 aana chahiye

Do this task in the classroom: your code is checked automatically and you get hints if something is missing.

Recap

Quick quiz

Which keyword defines a reusable function machine in Python?

Python में नया फ़ंक्शन बनाने के लिए कौन-सा कीवर्ड लिखा जाता है?

  1. func
  2. void
  3. def
  4. function
Show the answer

Answer: C (def)

def (short for define) creates a function in Python, e.g. def double(n):.

def कीवर्ड से नई फ़ंक्शन मशीन बनाई जाती है!

Practice problems for this chapter

Solve them in the Practice Zone, where each answer is checked automatically and two hints are available.