5.1.7 · HinglishC Programming

Functions — declaration vs definition, prototypes, call by value

1,912 words9 min readRead in English

5.1.7 · Coding › C Programming


YEH TEEN CHEEZEIN KYA HAIN?


C KO PROTOTYPE KI ZARURAT KYUN HAI?

HOW yeh pieces ek file mein fit hote hain:

#include <stdio.h>
 
int add(int a, int b);          // 1. DECLARATION (prototype) — promise
 
int main(void) {
    int s = add(3, 4);          // 2. CALL — compiler promise ke against check karta hai
    printf("%d\n", s);          // 7 print karta hai
    return 0;
}
 
int add(int a, int b) {         // 3. DEFINITION — promise pura karo
    return a + b;
}

Figure — Functions — declaration vs definition, prototypes, call by value

Call by value — COPY, step by step derive kiya gaya


Common Mistakes (Steel-manned)


Recall Ek 12-saal ke bachche ko explain karo (Feynman)

Socho tumhare paas ek recipe machine hai. Pehle tum ek sign lagate ho jo kehta hai "ADD naam ki ek machine hai jo do numbers leti hai aur ek wapas deti hai" — woh declaration hai. Baad mein tum actually machine banate ho apne saare gears ke saath — woh definition hai. Jab tum ise use karte ho, tum apne real cookies nahi dete; tum use apne numbers ki photocopies dete ho. Machine photocopies ke saath khelti hai, tumhe ek result deti hai, aur ghar par tumhare real numbers bilkul waise hi hain jaise the. Woh photocopy rule call by value kehlaata hai. Agar tum sach mein chahte ho ki machine tumhara ghar ka saman change kare, tum use copy ki jagah apna ghar ka address (ek pointer) dete ho.


Flashcards

Ek function declaration (prototype) kya specify karta hai?
Function ka naam, return type, aur parameter types — lekin koi body nahi; ; se khatam hota hai.
Ek function kitni baar define vs declare ho sakta hai?
Exactly ek baar define (One Definition Rule); jitni baar chahein declare.
C ko call se pehle prototype kyun chahiye?
Single-pass compiler ko argument types aur return type pata hona chahiye call check karne aur arguments convert karne ke liye; warna woh guess karta hai (implicit int) aur bugs aate hain.
Call by value mein function ko kya pass hota hai?
Har argument ki value ki ek copy, jo parameter mein rakh di jaati hai (stack frame mein ek fresh local).
Normal swap(a,b) main mein a aur b kyun nahi change karta?
Woh local copies x aur y swap karta hai; originals alag memory hain aur untouched rehte hain.
C mein function caller ki variable modify kaise karta hai?
Address (&var) pass karo aur pointer parameter use karo; original edit karne ke liye dereference karo (*p).
Kya definition bhi declaration hai?
Haan — definition mein signature hai, isliye woh function declare bhi karta hai. Reverse true nahi hai.
Kya prototype mein parameter names zaroori hain?
Nahi — int add(int, int); valid hai; declarations mein names optional hain.
Call by value mein stack frame ka kya role hai?
Yeh ek call ke liye copied parameters aur locals hold karta hai; return par destroy ho jaata hai, isliye changes escape nahi hote.
Pointer pass karna — kya woh abhi bhi call by value hai?
Haan; address value copy hoti hai. Lekin kyunki woh original ki taraf point karta hai, dereference karne se real data edit hota hai.

Connections

  • Pointers in C — call-by-value se kaise bachein aur caller data edit karein.
  • Stack and Function Call Mechanism — copies kahan rehti hain.
  • Header Files and Separate Compilation — declarations .h mein kyun rehte hain.
  • Arrays as Function Arguments — arrays pointers mein "decay" hote hain (ek exception jo call-by-reference jaisa lagta hai).
  • Recursion — har recursive call apne copied parameters leta hai.
  • Scope and Lifetime of Variables — parameters function scope ke locals hain.

Concept Map

announced by

built by

used by

specifies

adds

has same

limited by

requires before call

lets compiler check

creates

holds

realizes

leaves

Function: named reusable machine

Declaration / Prototype

Definition

Function Call

Name, return type, param types

Body with braces

One Definition Rule

Single-pass compiler

Call by Value

Stack frame scratchpad

Argument value copied

Caller variables unchanged