Notes in Hindi

Function that Return Structure in Hindi

/ BCA / Programming with C and CPP

Function that Return Structure in Hindi

Function that Return Structure in Hindi

What is Structure in C?

Structure C language में एक user-defined data type होता है, जो अलग-अलग type के variables को एक साथ एक unit में group करने की सुविधा देता है। इसे हम real-world object की तरह भी समझ सकते हैं, जैसे एक "Student" के पास name (string), roll (int), marks (float) होते हैं। Structure इन सभी को एक साथ group कर देता है।

Why Return Structure from Function?

जब हम किसी function से एक ही value return करते हैं तो simple return type जैसे int, float का use करते हैं, लेकिन जब हमें एक साथ multiple values return करनी होती हैं, जैसे किसी student का name, roll number, marks इत्यादि — तब हम structure return करते हैं। इससे function से complex data को एक साथ return करना आसान हो जाता है।

Use Case Example:

मान लीजिए हमें एक function बनाना है जो student की जानकारी input लेकर उसे return करे ताकि हम बाद में उसे main() function में print कर सकें — तब हम structure return का उपयोग करते हैं।

Returning Structure from Function in Hindi

Step-by-step Explanation:

  • पहले हम एक structure define करते हैं।
  • फिर एक ऐसा function बनाते हैं जो structure type return करता है।
  • उस function में हम structure variable बनाकर उसमें values डालते हैं।
  • अंत में वह structure variable return किया जाता है।

Structure Return करने वाला Function:

#include <stdio.h> #include <string.h> struct Student { int roll; char name[50]; float marks; }; struct Student getStudentDetails() { struct Student s; s.roll = 101; strcpy(s.name, "Ravi Kumar"); s.marks = 87.5; return s; } int main() { struct Student std; std = getStudentDetails(); printf("Roll Number: %d\\n", std.roll); printf("Name: %s\\n", std.name); printf("Marks: %.2f\\n", std.marks); return 0; }

ऊपर के program में getStudentDetails() एक ऐसा function है जो structure type return करता है। यह function struct Student का एक object बनाता है, उसमें data भरता है और फिर उसे return कर देता है। main() function में उस return किए गए structure को receive करके उसके values को print किया गया है।

Syntax for Returning Structure in Hindi

Structure Return Function की Syntax:

struct StructureName functionName() { struct StructureName var; // var के members में values डालो return var; }

जैसे ऊपर के example में हमने struct Student return किया, उसी तरह किसी भी structure को return करने के लिए syntax same रहेगा। बस आप अपनी जरूरत के अनुसार structure का नाम और members define करेंगे।

Important Points:

  • Structure को function से return करने के लिए structure का नाम return type के रूप में use होता है।
  • function के अंदर structure का object बनाकर उसमें data भरना होता है।
  • structure का object return किया जाता है।
  • main() या किसी भी caller function में return हुए structure को receive करके उस पर काम किया जा सकता है।

Handling Returned Structures in Hindi

Structure Receive करने की प्रक्रिया:

जब कोई function structure return करता है, तब हमें caller function में उसी structure type का एक variable बनाकर उसे return value से assign करना होता है।

Receiving the Returned Structure:

struct Student std; std = getStudentDetails();

यहां std एक structure variable है जो getStudentDetails() से returned structure को receive कर रहा है।

Structure के Members को Access करना:

printf("Roll: %d", std.roll); printf("Name: %s", std.name); printf("Marks: %.2f", std.marks);

Structure के अंदर रखे गए members को access करने के लिए dot (.) operator का उपयोग किया जाता है। जैसे std.roll, std.name आदि।

Functions से Returned Structure के साथ कार्य करने के लाभ:

  • हम complex data structures को एक जगह से दूसरी जगह भेज सकते हैं।
  • function modular और reusable बनते हैं।
  • code readable और manageable बनता है।
  • programming real-world की तरह महसूस होती है क्योंकि हम object-type data को manage करते हैं।

Example: Multiple Students के Structure Return करना

#include <stdio.h> struct Student { int roll; float marks; }; struct Student inputStudent(int r, float m) { struct Student s; s.roll = r; s.marks = m; return s; } int main() { struct Student s1, s2; s1 = inputStudent(1, 90.5); s2 = inputStudent(2, 85.0); printf("Student 1 - Roll: %d, Marks: %.2f\\n", s1.roll, s1.marks); printf("Student 2 - Roll: %d, Marks: %.2f\\n", s2.roll, s2.marks); return 0; }

इस example में inputStudent() function दो अलग-अलग students की जानकारी return कर रहा है और हम main() function में उन्हें अलग-अलग variables में store कर रहे हैं।

Structure Return करते समय ध्यान देने योग्य बातें:

  • Structure बहुत बड़ा है तो return करने से memory overhead बढ़ सकता है।
  • pointer का use करके memory efficient बनाया जा सकता है लेकिन वो advanced topic है।
  • beginner के लिए value return करना एक safe और अच्छा तरीका है।

Advanced Note (Beginner को समझने के लिए):

  • यदि आप dynamic memory (heap) में structure बनाते हैं और pointer return करते हैं तो structure lifetime को manage करना पड़ेगा।
  • लेकिन यदि आप सिर्फ local structure return कर रहे हैं जैसे ऊपर के examples में — तो C में यह safe है क्योंकि return copy होती है।

Interview या Exam Point of View से:

  • Structure return करने वाला function कैसे बनता है, syntax जरूर याद रखें।
  • Structure variable में return value को कैसे receive करना है — यह practice करें।
  • Dot operator से structure के members को access करने की आदत डालें।

Structure Return करने का Real-Life Example:

Function Return Use
getStudentDetails() Student Structure Student की details return करता है
createAccount() BankAccount Structure Account बनाकर उसकी जानकारी return करता है
getDate() Date Structure Current date return करता है

इस प्रकार structure को function से return करना real-world logic और modular programming दोनों के लिए बहुत उपयोगी होता है।

FAQs

Function जो structure type का data return करता है उसे structure returning function कहते हैं। यह function structure variable में भरा हुआ complete data एक साथ return करता है, जैसे student का name, roll, marks आदि।
C में structure return करने के लिए पहले structure define किया जाता है, फिर function लिखा जाता है जिसका return type वही structure होता है। function में structure variable को initialize करके उसे return किया जाता है। जैसे: struct Student getStudent() { struct Student s; // values assign करें return s; }
जब कोई function structure return करता है तो caller function में उसी structure type का variable बना कर उसे assignment के द्वारा value दी जाती है। जैसे: struct Student s1; s1 = getStudent();
हां, C में किसी भी structure को function से return किया जा सकता है। यह feature बहुत useful होता है जब हमें एक साथ कई values return करनी होती हैं। इससे modular और clean code लिखा जा सकता है।
Structure return करने से हम एक ही बार में multiple values को return कर सकते हैं, जिससे program modular और organized बनता है। साथ ही यह real-world objects को represent करने में मदद करता है और code को readable बनाता है।

Please Give Us Feedback