What is the scope of the variable declared in the user-defined function?
2023
What is the scope of the variable declared in the user-defined function?
- A.
Whole program
- B.
Only inside the { } block
- C.
Inside the main function
- D.
More than one of the above
- E.
None of the above
Attempted by 245 students.
Show answer & explanation
Correct answer: B
When a variable is declared inside a user-defined function, its scope is limited to the block in which it is declared. This means the variable can only be accessed within the { } block of that function. It is not accessible outside the function, nor is it available in other functions, including the main function. This is known as local scope. Variables declared in a function are created when the function is called and destroyed when the function exits. Therefore, the scope of a variable declared in a user-defined function is confined to the function's block.
हिन्दी उत्तर: जब किसी यूज़र-डिफ़ाइंड फ़ंक्शन में एक वेरिएबल घोषित किया जाता है, तो उसका स्कोप उस ब्लॉक तक सीमित होता है जिसमें वह घोषित किया गया है। इसका अर्थ है कि वेरिएबल केवल उस फ़ंक्शन के { } ब्लॉक के भीतर ही उपयोग किया जा सकता है। यह फ़ंक्शन के बाहर या अन्य फ़ंक्शन में उपलब्ध नहीं होता है, जिसमें मुख्य फ़ंक्शन भी शामिल है। इसे स्थानीय स्कोप कहा जाता है। फ़ंक्शन में घोषित वेरिएबल तब बनाए जाते हैं जब फ़ंक्शन को कॉल किया जाता है और जब फ़ंक्शन बाहर निकलता है तो उन्हें नष्ट कर दिया जाता है। इसलिए, यूज़र-डिफ़ाइंड फ़ंक्शन में घोषित वेरिएबल का स्कोप फ़ंक्शन के ब्लॉक तक सीमित होता है।