YOUR C LEARNING COMPANION

Kahan se padhein?

Learning path
C ki buniyadChapter 13 of 73

Functions: inputs, results aur contracts

Declarations, calls, parameters, return values, scope aur pass-by-value samjhein; return ko print karna assume na karein.

11 min readFoundationsC17 baseline
Offline reading
Is chapter mein11 sections

Function kisi work ko naam deta hai. Caller inputs de sakta hai aur uska result use kar sakta hai. Function declare karna, uska work define karna aur use call karna alag samjhenge, phir dekhenge ki dono ke beech information kaise pass hoti hai.

Ideas ko saath rakhkar samajhna hai? Concept guide kholein.

1. Function ka interface hota hai

C17 · translation-unit fragment
int add(int left, int right);

Declaration add naam ka function describe karti hai. Woh do int arguments leta hai aur int return karta hai. Parameter-type information ki wajah se yeh prototype hai. Body yahan nahi hai. Names jobs explain kar sakte hain; matching types aur compatibility rules interface govern karte hain.

Definition function body deti hai. Call use run karne ko kehti hai. Declare, define aur call karna alag actions hain. Call site par declaration visible ho sakti hai aur matching definition program mein elsewhere ho sakti hai.

Complete C17 program
#include <stdio.h>

int add(int left, int right) {
    return left + right;
}

int main(void) {
    int answer = add(2, 4);
    printf("%d\n", answer);
    return 0;
}

Yeh complete C17 example small arguments use karta hai jinka sum representable hai. add(2, 4) function call hai; returned value answer initialize karti hai. Later printf output request karti hai. Successful output six aur newline likhta hai. add define karna usko call nahi karta, aur six return karna six print nahi karta.

2. Arguments parameters ko values dete hain

Call mein supplied values arguments hain. Function unhein parameters ke through leta hai. add(2, 4) ke arguments two aur four hain. Definition mein parameters ke names left aur right hain. Parameter types ke according argument values convert hoti hain.

C arguments by value pass karti hai. Har call mein parameter ko apni value milti hai. Integer parameter change karne se caller ka integer object nahi badalta. Copied pointer shared storage ko refer kar sakta hai; isliye function pointer ke target ko affect kar sakta hai, jabki passing by value hi hai.

Complete C17 program
#include <stdio.h>

int next_value(int value) {
    value += 1;
    return value;
}

int main(void) {
    int original = 4;
    int next = next_value(original);
    printf("%d %d\n", original, next);
    return 0;
}

Local parameter five hota hai, lekin caller ka original four rehta hai. Returned value se function deliberately computed next value communicate karti hai. Range ke andar original + 2 jaisi expression bhi valid argument ho sakti hai; named variable hona zaroori nahi.

3. Return value result hai, display command nahi

return expression; control caller ko wapas deta hai aur return type mein converted result supply karta hai. Caller decide karta hai ki result kaise use karna hai. void function value return nahi karta aur return; use kar sakta hai. Value return karna use print nahi karta.

Saare paths deliberately design karein. Ordinary non-void function ke end tak pahunchna result supply karne ka generally safe tareeka nahi; missing result use karna undefined behavior ho sakta hai. Hosted main ki special rule hai: C99 aur later mein closing brace tak pahunchna zero return karne ke equivalent hai. Is exception ko har function par apply na karein.

4. Function ki requirements aur promises specify karein

Contract function ke accepted inputs aur promises batata hai. Changes aur failures bhi describe ho sakte hain. add example ki sum int mein fit honi chahiye. Prototype type information deta hai; har range requirement automatically check nahi karta.

Naam aur task ko coherent rakhein. Value return karne wali calculation function presentation se independently samajh aur check kar sakte hain. Jo function print bhi kare, global state badle aur input padhe, uska contract larger hai. Yeh appropriate ho sakta hai, lekin effects deliberate hon, harmless-looking name ke peeche surprise nahi.

5. Har invocation ki apni ordinary local state hoti hai

Parameters aur ordinary automatic locals invocation se belong karte hain. Names ka function source mein scope aur objects ki runtime lifetimes hoti hain. Dobara call karne se ordinary automatic local automatically previous call ki stored value yaad nahi rakhta. Static-storage objects ki lifetime rules alag hain.

Recursion mein function directly ya doosre functions ke through khud ko call karta hai. Har call ki ordinary local state alag hai. Terminating design ko stopping case aur uski taraf progress chahiye. Recursion chapter calls ko ek-ek karke trace karke resource limits samjhata hai.

6. Aisa interface likhein jo meaning clear kare

C17 mein int read_count(void); no-parameter function declare karta hai. int read_count(); woh prototype information nahi deta; us declaration mein parameter list unspecified hai. C23 empty-list meaning badalkar no parameters specify karta hai. Explicit void dono versions mein clear hai.

Call sites par compatible declarations use karein, multi-file program mein commonly headers se. Library calls ke bhi contracts hain. printf ka format relevant promotions ke baad actually supplied argument types se match hona chahiye. Desired display pattern likhne se format string arbitrary argument safely convert nahi kar deti.

Aur gehra samjhein: static aur inline magic speed switches nahi

File-scope function ka static identifier ko internal linkage deta hai. Doosri translation unit us naam ko external function name ki tarah link nahi kar sakti, lekin supplied pointer se function call kar sakti hai. inline machine-code substitution force nahi karta; function-definition aur linkage rules bhi hain. Plain inline definitions se bhara header automatically correct multi-file design nahi.

Thoda sochkar dekhein

Function integer parameter ko four se five karke five return karti hai. Caller returned result ignore kare toh kya uska original integer object necessarily change hua?

Explanation padhein

Nahi. Parameter ko value ki copy mili thi. Caller ko deliberately result use karna hoga, ya function ke paas shared object affect karne ka another valid specified way hona chahiye. Return karna aur caller ka original object modify karna alag actions hain.

Primary references aur aage ki reading

  • WG14 N1570 — public C11 committee draft: Sections 5.1.2.2, 6.2.2, 6.5.2.2, 6.7.4, 6.7.6.3, 6.8.6.4, and 6.9.1 is C17-baseline explanation ke core rules deti hain. Yeh public draft hai, final ISO publication nahi.
  • WG14 N3096 — public C23 draft: C23-specific differences ke liye: The meaning of an empty parameter list in C23 differs from the C17 non-prototype declaration.

Recursion ke baad pointers aur library interfaces familiar hon toh later callback aur variadic-function chapters par aayein.

Optional visual model: Function argument ki copy dekhein

Function argument ki copy dekhein

VISUAL MODEL

Parameter ko value milte aur badalte dekhein. Caller ka object alag rehta hai.

replace alag function hai. Aakhri do lines us doosre function ke andar hain jo ise call karta hai.

  1. void replace(int x) {
  2. x = 9;
  3. }
  4. /* inside a different function: */
  5. int caller = 4;
  6. replace(caller);
intcaller4Caller ka object
intx—Abhi active parameter nahi

Calling function mein caller, 4 se initialized hai. Abhi replace call active nahi hai.

C argument ki value pass karta hai. Pointer argument bhi copy hota hai; copied pointer phir bhi caller ke saath shared object ko refer kar sakta hai. Is model mein int pass hota hai.

Poora trace padhein
  1. Calling function mein caller, 4 se initialized hai. Abhi replace call active nahi hai.
  2. Argument expression caller apni current value, 4 deta hai.
  3. Parameter x alag int object hai, jo argument value 4 se initialize hota hai.
  4. x ko 9 milta hai. caller mein 4 hi hai kyunki woh alag object hai.
  5. replace return karta hai. Parameter ki lifetime end hoti hai; caller mein 4 hi rehta hai.

Call alag execution ko values deta hai

Idea compare karein, follow karein aur samjhayein

Suitable prototype wale function mein argument values parameter types mein convert hoti hain. Har call ke parameters alag objects hain. Copied pointer value abhi bhi caller ke object ko designate kar sakti hai.

Yeh kab kaam aata hai Output parameters, independent recursive calls aur parameter badalne versus target badalne ka difference samajhne ke liye yeh model use karein.

Ideas ko saath rakhkar padhein

Argument
Call par diya expression, jaise score ya &score.
Parameter
Us call ke function body mein use hone wala named object, jaise value ya target.

Parameter caller ke variable se automatically bound doosra naam nahi hai.

Copied int ko badalna
value = 9 se parameter object value badalta hai.
Copied pointer se write karna
*target = 9 se copied pointer ke designated live int object mein change hota hai.

Dono calls mein C ek value hi pass karta hai.

Function declaration
Function introduce hota hai; prototype ho to parameter types describe hoti hain.
Function definition
Saath mein woh body bhi milti hai jo function call par execute hoti hai.

Jahan body required hai, use dena hota hai; declaration akele implementation nahi deta.

Reasoning ko follow karein

Neeche pointer call ko &score milta hai, isliye target valid live int hai. Function arbitrary pointers ko validate karne wala general checker nahi hai.

C17 · diye gaye context ke illustrative fragments

  1. void change_copy(int value) { value = 9; (void)value; }
  2. void change_target(int *target) { *target = 9; }
  3. /* inside a caller: */
  4. int score = 4;
  5. change_copy(score);
  6. change_target(&score);
  1. Caller ka object initialize karein

    score int object hai, jiski value 4 hai.

  2. change_copy call karein

    Value 4 uske int parameter ko pass hoti hai. Alag parameter ko 9 assign karne se score assign nahi hota.

  3. First call se return karein

    Parameter ki lifetime khatam hoti hai. score abhi bhi 4 rakhta hai.

  4. change_target call karein

    &score ek pointer value deta hai. Parameter target ko yeh value milti hai aur woh score ko designate karta hai.

  5. target ke through write karein

    *target = 9 se score badalta hai. Return par pointer parameter ki lifetime khatam hoti hai, caller ke score object ki nahi.

Common misconceptions

Yeh statements misleading hain. Kyun, yeh samajhne ke liye har statement kholein.

MisconceptionPointer pass karne par C pass-by-reference mode mein aa jaata hai.

Pointer value by value pass hoti hai. Suitable target tak indirect access milta hai, par parameter alag pointer object hi rehta hai.

MisconceptionRecursive call previous call ke parameters overwrite karta hai.

Har active call ke apne automatic parameter objects hote hain. Agar shared static object use kiya hai to woh alag issue hai.

MisconceptionFunction arguments hamesha left to right evaluate hote hain.

C generally unka evaluation order specify nahi karta. Kisi order par depend na karein, khaaskar jab arguments same object ko read aur modify karte hain.

Thoda aur depth meinOptional technical detail

Array parameter spelling array copy nahi karti

Array ke roop mein declared parameter pointer parameter mein adjust hota hai. Zaroori count separately pass karein ya clear size contract wala interface use karein.

Macros alag kaam karte hain

Function-like macro preprocessing tokens substitute karta hai. Parameter objects ya function-call boundary nahi banti. Parentheses grouping fix kar sakte hain, par repeated evaluation abhi bhi possible hai.

C17 C17 mein int f(); jaisi declaration parameter-type prototype nahi deti. int f(void); explicitly no parameters declare karta hai. Is guide ke calls prototypes use karte hain.

C23 C23 mein int f(); ki empty parameter list no parameters specify karti hai aur prototype deti hai. Is change se function arguments left to right evaluate nahi hone lagte.

Source rules check karein

Yeh public committee drafts hain; clause references se explanation verify kar sakte hain. N3096 ek C23 working draft hai.

Apne words mein samjhayein

target copy hai, to *target se score kaise badalta hai?

Ek explanation se compare karein

Copied pointer value abhi bhi score ko designate karti hai. target ko khud badalna aur *target se write karna alag operations hain.

Aage badhne se pehle

  • Function ki declaration, definition aur call alag parts hain.
  • C argument values parameters ko pass karti hai; copied pointers shared targets access kar sakte hain.
  • Useful contract mein signature ke alawa ranges, effects aur failure behavior bhi hai.
In ideas ko alag rakhein
Returning / Printing
Return caller ko result deta hai; output ke liye suitable library call jaisa separate effect chahiye.
Apne words mein samjhaayein

Function pointer se caller ka object change kare toh bhi argument passing by value kaise rehti hai?

Optional: bolkar samjhein ya note rakhein. Tayyar hon toh aage badh sakte hain.

Apni understanding check karein

Optional hai. Answers save ya send nahi hote; chapter kabhi lock nahi hota.

Kya pointer argument pass karne se C pass-by-reference ho jaata hai?
Har choice ki explanation padhein
  1. Nahi. Pointer value copy hoti hai aur copy shared object refer kar sakti hai.

    ✓ Sahi reasoning. Function permitted pointed-to object modify kar sakta hai, par parameter alag pointer object hi hai.

  2. Haan, pointer parameters caller ke pointer variable ke aliases hain.

    Pointer parameter value leta hai; us parameter ko assign karna caller ke pointer object ko direct assign nahi karta.

  3. Pointer argument poori pointed-to allocation copy karta hai.

    Pointer value copy karne se referred objects copy nahi hote.

C17

Pehle edition ka quick reference

Yeh compact reference existing links aur saved passages ke liye retained hai. Full context ke liye upar ki expanded lesson padhein.

Declaration, Definition, aur Prototypes

Declaration function announce karta hai; definition uski body bhi deti hai. C17 mein prototype parameter types deta hai, jaise int add(int, int);, taaki calls check hon. int f(); declaration prototype nahi hai aur parameter list unspecified chhodta hai; int f(void); no parameters specify karta hai. C23 mein f() bhi no parameters specify karta hai. Same compatible prototype source files mein share karne ke liye header use karein.

Parameter Passing

C arguments by value pass karti hai. Pointer argument bhi copied pointer value hai; dereference karke caller ka pointed-to object change ho sakta hai, lekin local parameter ko doosra address assign karne se caller ka pointer change nahi hota. Argument mein array expression normally first element ke pointer mein convert hota hai aur array parameters pointer parameters mein adjust hote hain. Yeh pass-by-value ka exception nahi: pointer value copy hoti hai.

Recursion

Recursive function khud ko directly ya indirectly call karta hai. Terminate hone ke liye stopping condition aur uski taraf progress chahiye. Typical implementations call state stack par rakhti hain, isliye deep recursion resources exhaust kar sakti hai; C standard physical stack ya har call ki fixed stack space mandate nahi karta aur optimization kuch calls hata sakti hai. Large input depths ke liye iteration often better hai.

Variadic Functions

printf jaise functions <stdarg.h> facilities use karke arguments ki ek variable number accept karte hain: va_list argument-traversal state hold karta hai, va_start isse initialize karta hai, va_arg har argument ko uske expected type ke through retrieve karta hai, aur va_end cleanup karta hai. Kyunki kitne arguments pass hue ya har ek ka type kya hai iska koi runtime record nahi hota, function ko yeh context se infer karna padta hai — typically ek format string se — isi wajah se mismatched printf format specifiers undefined behavior ka ek classic source hain.

Function Pointers

Ek function pointer kisi function ka address store karta hai, jisse functions ko arguments ke roop mein pass kiya ja sakta hai, dispatch tables ke roop mein arrays mein store kiya ja sakta hai, ya doosre functions se return kiya ja sakta hai:

C17 · illustrative fragment
int add(int a, int b) { return a + b; }

int function_pointer_example(void) {
    int (*op)(int, int) = add;
    return op(3, 4);  /* 7 return hota hai */
}

Static aur Inline Functions

File-scope static function ke identifier ki internal linkage hoti hai: doosri translation unit us name ko external name linkage ke through use nahi kar sakti. Lekin supplied pointer ke through elsewhere ka code function call kar sakta hai. inline call expansion force nahi karta; multi-file programs ke liye definition aur linkage rules bhi hain. Yeh sirf speed switch nahi.

main Function

Hosted C environment mein startup main ko call karta hai. Portable forms int main(void) aur int main(int argc, char *argv[]) (ya equivalent declarations) hain; implementations additional forms de sakti hain. Freestanding environment mein startup implementation-defined hai aur main required nahi. 0 ya EXIT_SUCCESS successful termination aur EXIT_FAILURE unsuccessful termination report karta hai. Doosre exit values ka meaning implementation-defined hai. C99 se main ke closing brace tak pahunchna return 0 ke equivalent hai.

Agla idea padhne ke liye tayyar?

Mark this chapter as read whenever you feel ready.

CHAPTER 13 / 73

Is chapter mein

Functions: inputs, results aur contracts

Search all 73 chapters in both languages.EN + HG

Try “pointer”, “why int”, or “undefined behavior”.

Concepts connect. Follow your curiosity.↑ ↓ to explore · Enter to open
MAKE YOURSELF COMFORTABLE

Your reading space

Appearance

A clear canvas with balanced contrast.

Text size

100%
AA

Reading tools

Your data stays in this browser

Export a backup before changing browsers or replacing an older version of the reader.

YOUR PERSONAL MARGINS

My notebook

Saved on this device
A LITTLE CLARITY

The glossary

A starting meaning, then a chapter to go deeper.

A QUICK DEFINITION

A little clarity

Read the full explanation →

A thought worth keeping