1.2.27 · D1 · HinglishIntroduction to Programming (Python)

FoundationsFunctions — def, parameters, return, docstrings

3,059 words14 min read↑ Read in English

1.2.27 · D1 · Coding › Introduction to Programming (Python) › Functions — def, parameters, return, docstrings

Jab tak aap parent note ki ek bhi line padh sako, pehle aapko symbols aur words ka ek dher milta hai: def, parentheses ( ), colon :, indentation, return, =, **, //, %, None, triple quotes """, aur default parameters jaise exp=2. Yeh page in sab ko bilkul zero se define karta hai — plain words, phir ek picture, phir kyun uss topic ko yeh chahiye. Upar se neeche padho; har item sirf wahi cheezein use karta hai jo usse upar define ki gayi hain.


0. Sabse basic idea: ek naam jo ek cheez hold karta hai

Functions se pehle, aapko variables ki zaroorat hai. Ek variable bas ek value par laga label hai.

Figure s01 (neeche) — agar image load na ho: isme ek blue box dikh raha hai jo right mein value 5 hold kar raha hai, left mein ek orange tag x label kiya hua hai, aur ek green arrow x se 5 ki taraf point kar raha hai, caption ke saath "the = arrow works right → left".

Picture dekho: box 5 memory mein baitha hai, aur naam x ek tag hai jo uski taraf point kar raha hai. Isliye parent note result = add(2, 3) likh sakta hai — value add se nikalti hai, phir tag result uske saath laga diya jaata hai.


1. Indentation — shape hi grammar hai

Kai languages mein aap lines ko { } braces se group karte ho. Python spaces use karta hai: ek hi amount se right ki taraf push ki gayi lines ek block banati hain.

Figure s02 (neeche) — agar image load na ho: isme def add(a, b): left margin par dikh raha hai (blue), uska body return a + b 4 spaces right shift hua (green) ek orange arrow ke saath labelled "4 spaces = inside the function", aur ek de-indented line result = add(2, 3) red mein marked "OUTSIDE".

Picture mein def add(a, b): header left margin par hai, aur uska body return a + b andar push hua hai. Indentation hi Python ko batata hai "yeh line function ke andar hai." De-indent karo aur line function se bahar nikal jaati hai.


2. Colon : — "yahan se ek block shuru hota hai"

Isse ek hinge ki tarah socho: colon se pehle ki line header hai (block ka owner kaun hai), aur baad mein indented lines body hain (woh kya karte hain). Parent ka def area(r): bilkul yahi hinge use karta hai.


3. Parentheses ( ) — do nahi, teen kaam

Yeh beginners ko confuse karta hai kyunki ek hi symbol kai kaam karta hai.

Figure s03 (neeche) — agar image load na ho: left half mein do dashed blue boxes hain labelled a aur b ("empty slots"); right half mein do solid orange boxes hain jo 2 aur 3 hold kar rahe hain ("real values drop in"), beech mein ek green "fill" arrow ke saath, aur caption "parameter = slot, argument = filling".

Picture definition aur call ke beech contrast dikhati hai: left mein, khali labelled slots wait kar rahe hain; right mein, real values un slots mein gir rahi hain. Yahi pura "slot vs filling" distinction hai jo parent parameter vs argument kehta hai. (Grouping ka kaam — (a + b) * c — woh chautha jagah hai jahan aap parentheses miloge; uska functions se koi lena dena nahi hai lekin aapko ise pehchanna chahiye.)


4. Word def — "ek box banao, abhi run mat karo"

Socho ek factory ek vending machine bana rahi hai aur us par nameplate laga rahi hai. Banana ≠ vending karna. Machine tabhi dispense karti hai jab baad mein koi button press karta hai (the call). Isliye parent defining ≠ running par stress karta hai.


5. Default parameters — ek slot jo fallback value ke saath aata hai

Socho ek vending machine mein ek pre-set dial: agar aap dial ko chhodo, toh woh factory setting (2) use karta hai; agar aap ghoomate ho, aapki setting jeetti hai.

  • power(5) → sirf base diya, toh exp 2 par fall back karta hai → 5 ** 2 compute karta hai.
  • power(2, 3) → dono diye, toh exp 3 ban jaata hai → 2 ** 3 compute karta hai.

6. return — output chute

Figure s04 (neeche) — agar image load na ho: isme ek blue box hai labelled "function body (the work)"; ek orange arrow upar se enter karta hai ("inputs in"); ek green arrow neeche se nikalta hai ("return: result drops out"); aur ek red arrow side mein keh raha hai "then the machine STOPS".

Picture ek machine dikhati hai: inputs upar se enter karte hain, body kaam karti hai, aur return neeche ka chute hai jahan se finished result nikalti hai. Jis moment kuch nikalta hai, machine band ho jaati hai.


7. None — "kuch nahi nikla" token

Socho vending button press karo lekin chute khaali ho — aapko ek response mila ("kuch nahi"), aur us "kuch nahi" ka ek naam hai: None. Isliye parent warn karta hai ki print (jo None return karta hai) return nahi hai.

Recall

None error ke jaisa kyun nahi hai? None bilkul valid value hai ::: iska bas matlab hai "koi meaningful result nahi," toh program chalta rehta hai; crash nahi hota.


8. Triple quotes """...""" — docstring wrapper

Socho vending machine par laga sticker jisme likha ho "yeh chips deta hai." Woh machine se attached hai lekin mechanism ka hissa nahi. Parent ka area docstring bilkul yahi sticker hai.


9. Chhote maths operators jo parent use karta hai

Parent ke examples quietly woh symbols use karte hain jo aapko pata hone chahiye:

Figure s05 (neeche) — agar image load na ho: isme 17 small squares ek row mein hain, teen groups of five mein coloured (blue, green, orange) plus do red squares baach gaye, caption ke saath "3 whole groups (//) + 2 left over (%)".

Picture layout karta hai: teen poore paaanch (// deta hai 3) aur 2 bachta hai (% deta hai 2). Saath milke yeh parent ka divmod2 example explain karte hain, jo (quotient, remainder) = (3, 2) return karta hai.


10. Tuples ( , ) aur unpacking — ek saath do cheezein pakadna

Socho ek do-compartment lunchbox: ek call aapko poora box deta hai, aur q, r = ... ek hi motion mein dono compartments kholti hai. Dekhein Lists and Tuples puri story ke liye.


Prerequisite map

Neeche ka diagram top-to-bottom padha jaata hai: har foundation neeche wale box mein feed karta hai, aur sab milke topic Functions mein flow karte hain. Agar diagram render nahi hota, wohi structure neeche ki checklist mein spell out hai.

Assignment: label on a value

Indentation: marks the body

Colon: a block starts here

Parentheses: group, slots, and GO button

def: builds the box

Default parameters: a slot with a fallback

return: the output chute

None: nothing came out

Triple-quote docstring

Operators: plus star power floordiv mod

Tuples and unpacking

FUNCTIONS the topic


Equipment checklist

Right side cover karo aur khud test karo. Agar koi answer surprising lage, us section ko dobara padho.

Single = kya karta hai?
Ek value par label (naam) lagata hai — assignment, right se left. Yeh koi sawaal nahi poochhta.
== kya karta hai instead?
Haan/na ka sawaal poochhta hai — kya yeh dono cheezein equal hain?
Is page par editorial double equals kisi word ke around kya matlab rakhte hain?
Bas study-note highlighting; yeh Python syntax NAHI hai.
Python ko kaise pata ki kaunsi lines function ke andar hain?
Indentation se — def header ke neeche right shift hui lines body banati hain.
def line ke end mein colon : kya signal karta hai?
Ki us line se ek indented block belong karta hai.
Parentheses ke teen kaam batao.
Expressions mein grouping (a+b)*c; def mein parameter slots hold karna; call mein arguments hold karna aur "abhi run karo" matlab dena.
add(2, 3) mein parentheses kya karte hain?
Argument values hold karte hain AUR matlab dete hain "function abhi run karo."
greet aur greet() mein kya fark hai?
greet function object hai (the box); greet() use run karta hai.
def line mein exp=2 ka matlab kya hai?
Ek default: agar caller koi exp nahi deta, toh 2 ko fallback ke roop mein use karo.
return ek saath kya do kaam karta hai?
Caller ko ek value wapas karta hai, aur turant function band kar deta hai.
Koi return na ho toh function kya wapas karta hai?
None — "yahan kuch nahi" value.
Docstring kahan jaata hai aur ise kya wrap karta hai?
Function body ke andar pehli line, triple quotes """...""" mein wrapped.
2 ** 3 kitna equal hai aur ** ka matlab kya hai?
8; ** hai "raise to the power".
17 aur 5 ke liye // aur % kya dete hain, aur % kaise type karte hain?
17 // 5 hai 3 (whole divisions); 17 % 5 hai 2 (remainder); plain percent sign type karo, koi backslash nahi.
return a, b kya build karta hai, aur ise kaise pakadते hain?
Ek tuple (a, b); ise unpacking se pakado x, y = f().

Parent par wapas jao: parent topic.