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) — मशीन तुरंत जवाब तैयार कर देगी!
Words and symbols in this chapter
def- Define — tells Python we are building a new reusable function machine.
- Define — Python को बताता है कि हम एक नई फ़ंक्शन मशीन बना रहे हैं।
square(n):squareis the function's name;ninside( )is the input slot (parameter).squareमशीन का नाम है और(n)वह इनपुट स्लॉट है जहाँ नंबर जाएगा।print(n * n)- Indented body — multiplies input
nby 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
squarethat accepts one numbern. squareनाम की मशीन बनाई जो एक नंबरnलेती है।ans = n * n / print(...)- Calculates
n * nand prints the result. n * nनिकालकर स्क्रीन पर छापती है।square(5) / square(9)- Uses the same machine twice — first for
5(25), then for9(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 प्रिंट करवाओ!
- Line 1: Write
def square(n):.
लाइन 1:def square(n):लिखो। - Line 2: Indent 4 spaces and write
print(n * n).
लाइन 2: 4 स्पेस देकरprint(n * n)लिखो। - 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 chahiyeDo this task in the classroom: your code is checked automatically and you get hints if something is missing.
Recap
def name(params):builds a reusable function once.- Call it as often as you like:
square(5),square(9). - A function does nothing until it is called;
returnsends a value back.
def name(params):एक बार दोबारा इस्तेमाल होने वाला function बनाता है।- जितनी बार चाहो बुलाओ:
square(5),square(9)। - Function बुलाए बिना कुछ नहीं करता;
returnवैल्यू वापस भेजता है।
Quick quiz
Which keyword defines a reusable function machine in Python?
Python में नया फ़ंक्शन बनाने के लिए कौन-सा कीवर्ड लिखा जाता है?
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
- Tiffin Service Bill Function (fill the blanks)
टिफ़िन सर्विस बिल Function (खाली जगह भरो)
Solve them in the Practice Zone, where each answer is checked automatically and two hints are available.