PGDCA, BCA, B-Tech, MCA, M-Tech, B.Sc computer science राजस्थान सूचना सहायक व कंप्यूटर शिक्षक विशेष नोट्स: इस अध्याय में हम Functions in C: Definition, Declaration (Prototype) and Call (फंक्शन्स: फंक्शन प्रोटोटाइप, परिभाषा और कॉलिंग सिंटैक्स) की संपूर्ण थ्योरी, मुख्य तकनीकी बिंदु, तुलनात्मक तालिका और विगत परीक्षाओं के पैटर्न पर आधारित 20 अति-महत्वपूर्ण बहुविकल्पीय प्रश्न (MCQs) विस्तृत हिंदी व अंग्रेजी व्याख्या सहित पढ़ेंगे।
Overview: A C function organizes code into modular reusable blocks governed by three parts: declaration (prototype) announcing signature, definition providing executable body, and call executing it on the call stack. Modern ISO C (C99+) requires prior declaration or definition before invocation so argument counts and types are strictly verified.
अध्याय सार: C फंक्शन कोड को व्यवस्थित व पुनः प्रयोज्य इकाइयों में बाँटता है जिसके तीन मुख्य चरण हैं: डिक्लेरेशन (प्रोटोटाइप) जो सिग्नेचर बताता है, डेफिनिशन जिसमें बॉडी कोड होता है, और कॉल जो कॉल-स्टैक पर निष्पादन शुरू करता है। आधुनिक ISO C (C99+) में कॉल करने से पहले प्रोटोटाइप या परिभाषा देना अनिवार्य है ताकि टाइप व आर्गुमेंट संख्या की सही जांच हो सके।
Functions in C: Definition, Declaration (Prototype) and Call: Detailed Notes & Architecture (फंक्शन्स: फंक्शन प्रोटोटाइप, परिभाषा और कॉलिंग सिंटैक्स)
1. Function Declaration (Prototype) & Parameter Rules (फंक्शन डिक्लेरेशन (प्रोटोटाइप) व नियम)
[EN] A function prototype has the form 'return_type name(type1, type2);' terminated by a semicolon; parameter names are optional and need not match the definition. Placed above main() or inside .h header files, prototypes enable compile-time validation of argument count and implicit conversions. Importantly, under C89 through C17, 'int f();' means unspecified arguments (not zero arguments!), whereas 'int f(void);' explicitly declares zero parameters (changed in C23 so () means (void)).
[HI] फंक्शन प्रोटोटाइप 'return_type name(type1, type2);' के रूप में सेमीकोलन पर समाप्त होता है, जहाँ पैरामीटर के नाम लिखना वैकल्पिक है। हेडर फाइल या main() से पहले प्रोटोटाइप लिखने से कंपाइलर कॉल के समय आर्गुमेंट संख्या व टाइप की सटीक जांच करता है। C89 से C17 तक 'int f();' का अर्थ अज्ञात पैरामीटर्स होता था, इसलिए शून्य पैरामीटर बताने के लिए 'int f(void);' लिखना जरूरी था।
2. Function Definition, Body & Return Semantics (फंक्शन डेफिनिशन, बॉडी व रिटर्न नियम)
[EN] A function definition provides the actual implementation block '{ ... }' with mandatory parameter variable names and no trailing semicolon after the parameter list. Every non-void function whose returned value is used must execute 'return expr;' along all control paths (except main(), which implicitly returns 0 starting in C99). Standard ISO C strictly forbids nested function definitions—every function must be defined independently at file scope.
[HI] फंक्शन डेफिनिशन में पैरामीटर्स के नाम अनिवार्य होते हैं और हेडर के बाद सेमीकोलन के बजाय '{ ... }' ब्लॉक में वास्तविक कोड लिखा जाता है। प्रत्येक non-void फंक्शन को अपना मान लौटाने के लिए return का उपयोग करना चाहिए (केवल C99+ में main() बिना return लिखे स्वतः 0 लौटाता है)। मानक ISO C में एक फंक्शन के भीतर दूसरे फंक्शन को परिभाषित (Nested Function) करने की अनुमति नहीं है।
3. Function Call Execution & Call Stack Frames (फंक्शन कॉल व कॉल-स्टैक फ्रेम कार्यप्रणाली)
[EN] Invoking 'ans = calc(3, 4);' evaluates the arguments, pushes an activation record (stack frame) containing argument values, saved return address, and local variables onto the call stack, and jumps to the function's entry point. Note that C leaves the evaluation order of multiple arguments inside one call like 'f(g(), h())' unspecified! Writing a function's name without parentheses ('calc;') does not invoke it—it merely yields its function pointer address.
[HI] फंक्शन कॉल करते समय आर्गुमेंट्स का मान निकालकर कॉल-स्टैक पर एक नया स्टैक फ्रेम (रिटर्न एड्रेस, आर्गुमेंट्स व लोकल वेरिएबल्स सहित) बनाया जाता है। C मानक यह तय नहीं करता कि एक ही कॉल 'f(g(), h())' में g() पहले चलेगा या h()—यह क्रम अनिर्दिष्ट (Unspecified) रहता है। फंक्शन के नाम के पीछे () कोष्ठक न लगाने पर फंक्शन कॉल नहीं होता बल्कि उसका फंक्शन पॉइंटर पता मिलता है।
4. Abolition of Implicit int (C99) & Library Headers (C99 में इम्प्लिसिट int की समाप्ति व हेडर फाइलें)
[EN] In legacy ANSI C89, calling an undeclared function silently assumed return type int and skipped parameter type checking, causing subtle bugs with float/pointer functions; C99 removed both implicit int and implicit function declarations. Functions in C fall into library functions (declared via headers like <stdio.h>, <math.h>) and user-defined functions. Functions returning nothing must explicitly specify 'void' as their return type.
[HI] पुराने C89 मानक में बिना प्रोटोटाइप के फंक्शन कॉल करने पर कंपाइलर रिटर्न टाइप स्वतः int मान लेता था जिससे गंभीर बग आते थे; C99 ने इम्प्लिसिट int नियम को समाप्त कर दिया। C में दो प्रकार के फंक्शन होते हैं: <stdio.h>, <math.h> जैसी हेडर फाइलों में घोषित लाइब्रेरी फंक्शन और यूजर-डिफाइंड फंक्शन। कोई मान न लौटाने वाले फंक्शन का रिटर्न टाइप स्पष्ट रूप से void लिखना अनिवार्य है।
Syntax, Formula & Technical Example (Functions in C: Definition, Declaration (Prototype) and Call)
int maxOfTwo(int, int); // 1. Prototype (parameter names optional)
int main(void) {
int ans = maxOfTwo(14, 29); // 2. Function Call
printf("Max = %d\n", ans); // Output: Max = 29
return 0;
}
int maxOfTwo(int a, int b) { // 3. Function Definition
return (a > b) ? a : b;
}
Functions in C: Definition, Declaration (Prototype) and Call Quick Reference & Comparison Table
| Function Component | Syntax & Semicolon Rule | ISO C Standard Rule |
|---|---|---|
| Prototype (Declaration) | int add(int, int); ends with ';' | Parameter names optional; validates calls early |
| Function Definition | int add(int a, int b) { ... } no ';' | Contains body; names required; file scope only |
| Function Call | res = add(x, y); invokes via () | Pushes stack frame; arg eval order unspecified |
| Explicit void Params | void init(void); zero parameters | Unlike init() in C99/C11 which allows any args |
Functions in C: Definition, Declaration (Prototype) and Call Top 20 Bilingual MCQs with Answers
Q1. Which statement is accurate regarding a function prototype (declaration) in C?
(C में फंक्शन प्रोटोटाइप (डिक्लेरेशन) के संबंध में इनमें से कौन सा कथन सही है?)
(A) It cannot appear outside main()
(B) Parameter variable names are optional; only parameter types and semicolon termination are required
(C) Parameter names must match the definition identically
(D) It must include the full curly-brace function body
Correct Answer: (B)
[EN] A prototype like 'int mul(int, int);' communicates return type and argument types to the compiler before definition.
[HI] प्रोटोटाइप में केवल रिटर्न टाइप, पैरामीटर डेटा टाइप्स और अंत में सेमीकोलन होना जरूरी है; वेरिएबल्स के नाम लिखना वैकल्पिक होता है।
Q2. In C99 and C11, what is the difference between prototypes 'int f();' and 'int f(void);'?
(C99 व C11 मानकों में 'int f();' और 'int f(void);' प्रोटोटाइप के बीच क्या अंतर है?)
(A) int f() accepts an unspecified parameter list, whereas int f(void) strictly takes zero arguments
(B) int f(void) returns a void pointer
(C) int f() fails to compile in C99
(D) Both mean zero parameters identically
Correct Answer: (A)
[EN] Until C23, empty parentheses in a C prototype meant 'unknown arguments' (disabled checking), while (void) enforced zero parameters.
[HI] C23 से पहले 'int f()' का अर्थ अनिर्दिष्ट आर्गुमेंट्स था, जबकि 'int f(void)' सख्ती से शून्य पैरामीटर घोषित करता है।
Q3. What return type did ANSI C89 assume if a function's return type was omitted, and how did C99 change it?
(ANSI C89 में रिटर्न टाइप न लिखने पर क्या मान लिया जाता था, और C99 में इसमें क्या बदलाव हुआ?)
(A) C89 assumed void; C99 defaults to int
(B) C99 assumes float automatically
(C) C89 assumed int; C99 abolished implicit int and requires explicit return types
(D) Both C89 and C99 forbid missing return types
Correct Answer: (C)
[EN] C99 removed the legacy implicit-int rule so functions without an explicit return type fail modern strict compilation.
[HI] C89 में रिटर्न टाइप छोड़ने पर स्वतः int मान लिया जाता था, लेकिन C99 ने इस 'इम्प्लिसिट int' नियम को समाप्त कर दिया।
Q4. Does standard ISO C permit defining one function inside the body of another function?
(क्या मानक ISO C एक फंक्शन की बॉडी के भीतर दूसरे फंक्शन को परिभाषित (Nested Definition) करने की अनुमति देता है?)
(A) Yes, inside main() only
(B) Yes, up to 3 nesting levels
(C) No, ISO C requires all function definitions to reside at file scope (no nesting)
(D) Yes, starting from ANSI C89
Correct Answer: (C)
[EN] While GCC provides nested functions as a non-standard extension, ISO C forbids defining functions inside functions.
[HI] मानक ISO C में नेस्टेड फंक्शन परिभाषा वर्जित है; सभी फंक्शन फाइल स्कोप (एक-दूसरे से बाहर) पर ही परिभाषित होते हैं।
Q5. What error occurs in: int sq(int x); { return x * x; }?
(कोड 'int sq(int x); { return x * x; }' में किस कारण से त्रुटि आएगी?)
(A) Compile-time error because the trailing semicolon turns the header into a prototype leaving an orphan block
(B) sq is a reserved keyword
(C) x cannot be multiplied by itself
(D) It compiles and works normally
Correct Answer: (A)
[EN] Putting ';' after the header ends a declaration, so the following { ... } body triggers a syntax error.
[HI] हेडर के तुरंत बाद ';' लगाने से वह प्रोटोटाइप बन जाता है और बाद का { ... } ब्लॉक अवैध सिंटैक्स एरर उत्पन्न करता है।
Q6. In C99 and newer, what happens if main() reaches its closing brace '}' without a return statement?
(C99 और नए मानकों में यदि main() बिना return स्टेटमेंट के समाप्त हो जाए, तो क्या होता है?)
(A) It implicitly returns 0 to the operating system
(B) It returns -1
(C) It fails to compile
(D) It triggers Undefined Behavior
Correct Answer: (A)
[EN] ISO C99 added a special rule for main() only: reaching the closing brace implies 'return 0;'. Other non-void functions have no such guarantee.
[HI] केवल main() के लिए C99 से यह विशेष नियम है कि अंत तक पहुंचने पर वह स्वतः 0 लौटाता है (अन्य non-void फंक्शनों में ऐसा नहीं होता)।
Q7. Which memory region holds active function frames (parameters, return addresses, local variables) during execution?
(फंक्शन कॉल के दौरान पैरामीटर्स, रिटर्न एड्रेस और लोकल वेरिएबल्स किस मेमोरी क्षेत्र में संग्रहीत होते हैं?)
(A) Heap
(B) Call Stack
(C) BSS Segment
(D) Read-only Code Segment
Correct Answer: (B)
[EN] Each function call pushes an activation record onto the LIFO Call Stack and pops it when returning.
[HI] प्रत्येक फंक्शन कॉल पर कॉल-स्टैक में एक एक्टिवेशन रिकॉर्ड (फ्रेम) बनता है और return होते ही हट जाता है।
Q8. In 'printf("%d %d", f1(), f2());', in what order does ISO C guarantee f1() and f2() will be called?
('printf("%d %d", f1(), f2());' में मानक C के अनुसार f1() और f2() किस क्रम में कॉल होंगे?)
(A) Strictly right-to-left (f2 then f1)
(B) Simultaneously on multiple threads
(C) Strictly left-to-right (f1 then f2)
(D) Unspecified order (either f1() or f2() may execute first)
Correct Answer: (D)
[EN] ISO C leaves the evaluation order of function arguments unspecified; relying on side-effect order across arguments is non-portable.
[HI] C में फंक्शन आर्गुमेंट्स के मूल्यांकन का क्रम Unspecified होता है, यानी कंपाइलर f1() या f2() में से किसी को भी पहले चला सकता है।
Q9. What happens when writing 'greet;' instead of 'greet();' as a statement inside main()?
(main() में 'greet();' के बजाय केवल 'greet;' लिखने पर क्या होता है?)
(A) It calls greet with zero arguments
(B) Compile error for missing arguments
(C) It evaluates to a function pointer without calling greet
(D) It deletes greet from memory
Correct Answer: (C)
[EN] Parentheses () are the function-call operator in C; omitting them simply produces the function's address.
[HI] कोष्ठक () ही फंक्शन कॉल ऑपरेटर है; इसके बिना केवल नाम लिखने से फंक्शन निष्पादित नहीं होता बल्कि उसका पता मिलता है।
Q10. How many values can a C 'return' statement return directly from a function?
(C में एक 'return' स्टेटमेंट सीधे फंक्शन से अधिकतम कितने मान लौटा सकता है?)
(A) Up to two comma-separated values simultaneously
(B) At most one value (scalar, pointer, or single struct)
(C) Only integers
(D) Unlimited values via tuple return
Correct Answer: (B)
[EN] C lacks tuple returns; returning multiple outputs requires pointer parameters or returning a struct containing multiple fields.
[HI] C में return केवल एक ही मान (या एक struct) लौटा सकता है; कई मान पाने के लिए पॉइंटर्स या struct उपयोग करते हैं।
Q11. What value does 'int f(void) { return (10, 20, 30); }' return to its caller?
(फंक्शन 'int f(void) { return (10, 20, 30); }' कॉलर को कौन सा मान लौटाएगा?)
(A) Compile error
(B) 60
(C) 30
(D) 10
Correct Answer: (C)
[EN] Parentheses turn 10, 20, 30 into a comma expression, which evaluates left-to-right and returns only the rightmost value 30.
[HI] कोष्ठक के अंदर कॉमा ऑपरेटर बाएँ से दाएँ चलते हुए केवल सबसे दायाँ मान 30 ही लौटाता है।
Q12. What happens if a function declared with return type 'void' executes 'return 1;'?
(यदि 'void' रिटर्न टाइप वाले फंक्शन में 'return 1;' लिख दिया जाए, तो क्या होगा?)
(A) Program exits with code 1
(B) 1 is discarded silently
(C) Function implicitly becomes int
(D) Compile-time error because a void function cannot return a value
Correct Answer: (D)
[EN] Inside a void function, only a bare 'return;' (with no expression) is permitted for early exit.
[HI] void फंक्शन कोई मान नहीं लौटा सकता; केवल समय से पहले बाहर आने के लिए खाली 'return;' लिखा जा सकता है।
Q13. Is a separate prototype required when the complete function definition is already placed above main()?
(जब फंक्शन की पूरी परिभाषा main() से ऊपर ही लिख दी गई हो, तो क्या अलग से प्रोटोटाइप लिखना आवश्यक है?)
(A) No, the definition placed before the call acts as its own declaration
(B) Yes, unless the function returns void
(C) Only if the function takes 2 or more parameters
(D) Yes, prototypes are mandatory even if defined earlier
Correct Answer: (A)
[EN] A function definition prior to its first call supplies full signature and body to the compiler at once.
[HI] कॉल से पहले लिखी गई पूर्ण डेफिनिशन स्वयं ही डिक्लेरेशन का काम कर देती है, अतः अलग प्रोटोटाइप जरूरी नहीं होता।
Q14. Why must you include <math.h> before calling sqrt(16.0) in C?
(C में sqrt(16.0) कॉल करने से पहले #include <math.h> क्यों जोड़ना चाहिए?)
(A) Without it, main() cannot return 0
(B) It provides the prototype 'double sqrt(double);' so types match properly
(C) It disables floating-point exceptions
(D) It contains the compiled binary code of sqrt
Correct Answer: (B)
[EN] Header files provide prototypes/declarations so the compiler generates correct double-argument passing and return handling.
[HI] हेडर फाइल कंपाइलर को sqrt का प्रोटोटाइप बताती है जिससे आर्गुमेंट व double रिटर्न मान की सही हैंडलिंग सुनिश्चित होती है।
Q15. Which statement is NOT true about C functions?
(C फंक्शन्स के बारे में इनमें से कौन सा कथन असत्य है?)
(A) Recursive functions allocate a new stack frame per call
(B) A function prototype ends with a semicolon
(C) Parameter names in a function prototype must match the exact names in the function definition
(D) A void function may use bare 'return;' to exit early
Correct Answer: (C)
[EN] Prototype parameter names are purely optional documentation and do not have to match the definition's names.
[HI] प्रोटोटाइप में पैरामीटर के नाम लिखना वैकल्पिक है और उनका डेफिनिशन के नामों से मेल खाना बिल्कुल जरूरी नहीं है।
Q16. What severe bug occurs in: int* bad(void) { int x = 42; return &x; }?
('int* bad(void) { int x = 42; return &x; }' फंक्शन में कौन सा गंभीर बग है?)
(A) int* cannot be returned by a function
(B) Returns a dangling pointer to local stack variable x destroyed upon return
(C) Memory leak in heap
(D) x cannot have its address taken
Correct Answer: (B)
[EN] Automatic local variable x ceases to exist when bad()'s stack frame pops, making the returned address invalid (Undefined Behavior).
[HI] लोकल वेरिएबल x की स्टैक मेमोरी फंक्शन समाप्त होते ही नष्ट हो जाती है, अतः &x लौटाना डैंगलिंग पॉइंटर (Undefined Behavior) पैदा करता है।
Q17. Within Functions in C: Definition, Declaration (Prototype) and Call, which function component directly maps to: "int add(int, int); ends with ';'"?
(Functions in C: Definition, Declaration (Prototype) and Call में कौन सा Function Component सीधे "int add(int, int); ends with ';'" से संबंधित है?)
(A) Prototype (Declaration)
(B) Explicit void Params
(C) Function Call
(D) Function Definition
Correct Answer: (A)
[EN] In Functions in C: Definition, Declaration (Prototype) and Call, 'Prototype (Declaration)' handles 'int add(int, int); ends with ';'' (Parameter names optional; validates calls early).
[HI] Functions in C: Definition, Declaration (Prototype) and Call में 'Prototype (Declaration)' का कार्य/विवरण 'int add(int, int); ends with ';'' (Parameter names optional; validates calls early) है।
Q18. Which option accurately describes 'Function Definition' in Functions in C: Definition, Declaration (Prototype) and Call?
(Functions in C: Definition, Declaration (Prototype) and Call के संदर्भ में 'Function Definition' की सही विशेषता कौन सी है?)
(A) int add(int, int); ends with ';'
(B) res = add(x, y); invokes via ()
(C) int add(int a, int b) { ... } no ';'
(D) void init(void); zero parameters
Correct Answer: (C)
[EN] 'Function Definition' provides 'int add(int a, int b) { ... } no ';'' — key point: Contains body; names required; file scope only.
[HI] 'Function Definition' का मुख्य विवरण 'int add(int a, int b) { ... } no ';'' है (Contains body; names required; file scope only)।
Q19. In Functions in C: Definition, Declaration (Prototype) and Call, identify the correct parameter for the specification: "res = add(x, y); invokes via ()".
(Functions in C: Definition, Declaration (Prototype) and Call में "res = add(x, y); invokes via ()" विवरण किस विकल्प के लिए सही है?)
(A) Prototype (Declaration)
(B) Explicit void Params
(C) Function Definition
(D) Function Call
Correct Answer: (D)
[EN] 'Function Call' corresponds to 'res = add(x, y); invokes via ()' (Pushes stack frame; arg eval order unspecified).
[HI] 'Function Call' का संबंध 'res = add(x, y); invokes via ()' (Pushes stack frame; arg eval order unspecified) से है।
Q20. Considering Functions in C: Definition, Declaration (Prototype) and Call, what key behavior defines 'Explicit void Params'?
(Functions in C: Definition, Declaration (Prototype) and Call में 'Explicit void Params' की मुख्य भूमिका या मान क्या है?)
(A) int add(int a, int b) { ... } no ';'
(B) void init(void); zero parameters
(C) int add(int, int); ends with ';'
(D) res = add(x, y); invokes via ()
Correct Answer: (B)
[EN] 'Explicit void Params' represents 'void init(void); zero parameters' (Unlike init() in C99/C11 which allows any args).
[HI] 'Explicit void Params' का अर्थ/मान 'void init(void); zero parameters' है (Unlike init() in C99/C11 which allows any args)।
Functions in C: Definition, Declaration (Prototype) and Call — Exam FAQs
Q1: Why is 'int main(void)' preferred over 'int main()' in modern C?
आधुनिक C में 'int main()' की तुलना में 'int main(void)' क्यों बेहतर माना जाता है?
[EN] Prior to C23, empty parentheses '()' meant an unspecified parameter list rather than zero parameters; writing '(void)' explicitly tells the compiler to reject accidental arguments passed to main().
[HI] C23 से पहले खाली कोष्ठक '()' का अर्थ अज्ञात आर्गुमेंट्स होता था; '(void)' लिखने से कंपाइलर स्पष्ट रूप से सुनिश्चित करता है कि फंक्शन शून्य आर्गुमेंट ही ले।
Q2: What happens in C99+ if you call a user-defined function defined below main() without writing a prototype above main()?
यदि किसी फंक्शन की परिभाषा main() के नीचे हो और ऊपर प्रोटोटाइप न लिखा जाए, तो C99+ में क्या होगा?
[EN] Modern C99/C11 compilers forbid implicit function declarations and reject the call with an error or strict diagnostic because the signature is unknown at the call site.
[HI] C99 और नए मानकों में बिना डिक्लेरेशन के फंक्शन कॉल करना वर्जित है, इसलिए कंपाइलर कॉल के समय सिग्नेचर न मिलने पर त्रुटि देगा।
Q3: Is a function prototype mandatory if the function definition is already placed above main()?
यदि फंक्शन की परिभाषा पहले ही main() से ऊपर लिखी हो, तो क्या प्रोटोटाइप लिखना अनिवार्य है?
[EN] No. Writing the complete definition before its first call acts as its own declaration; prototypes are mainly needed when functions call each other mutually, appear below main(), or reside in separate .c files via .h headers.
[HI] नहीं। कॉल से पहले परिभाषा देने पर वह स्वयं प्रोटोटाइप का कार्य करती है; प्रोटोटाइप मुख्यतः तब जरूरी है जब फंक्शन main() के नीचे हो या अलग फाइलों में हो।
Post a Comment
Post a Comment