Local vs Global variables c++ In Hindi

Local vs. Global variables c++ In Hindi

C++ प्रोग्रामिंग लैंग्वेज में किसी फंक्शन प्रोग्राम में पैरामीटर वेरिएबल का यूज़ यूजर डिफाइन डिक्लेअर डेटा और इनफार्मेशन को स्टोर और प्रोसेस करने में किया जाता है. जिसे आप किसी फंक्शन प्रोग्राम में जरूरत के अनुसार एक्सेस और मैनिपुलेट कर सकते है। यहाँ इन यूजर डिफाइन फंक्शन वेरिएबल का स्कोप और लाइफटाइम इस बात पर टोटली डिपेंड करता है कि वे फंक्शन में और किस बिहैवियर के साथ और कहाँ डिक्लेयर किए गए हैं। फंक्शन प्रोग्राम में वेरिएबल को लोकल वेरिएबल और ग्लोबल वेरिएबल केटेगरी में डिवाइड किया जा सकता है. जहा हर फंक्शन में डिक्लेअर एक वेरिएबल पैरामीटर की अपनी स्कोप, लाइफटाइम, और विज़िबिलिटी, से रिलेटेड मल्टीप्ल इंडिविजुअल यूनिक फीचर्स और फंक्शन्स होते हैं।

Local vs Global variables c++ In Hindi

So, let’s explore the concepts of local variables and global variables in C++ programming.

Local Variable Concept in C++.

फंक्शन प्रोग्राम में लोकल वेरिएबल एक तरह से यूजर डिफाइन प्रोग्राम डिक्लेअर वेरिएबल होते हैं, जिन्हें किसी कस्टम यूजर डिफाइन फ़ंक्शन (या प्रोग्राम सोर्स कोड के एक ब्लॉक) के अंदर डिक्लेयर किया जाता है, और इन डिक्लेअर लोकल वेरिएबल को सिर्फ़ उसी डिक्लेअर डिफाइन फ़ंक्शन या ब्लॉक के अंदर से एक्सेस और मैनेज किया जा सकता है। एक बार जब किसी लोकल फंक्शन वेरिएबल में एग्ज़िक्यूशन फ़ंक्शन या ब्लॉक के स्कोप से एग्जिट हो जाता है. तो उसके बाद लोकल वेरिएबल का अस्तित्व अपने आप समाप्त हो जाता है।

Important features and functions of local variables.

  • Scope – लोकल वेरिएबल उस फ़ंक्शन या ब्लॉक तक ही लिमिटेड यूज़ और एक्सेस होते है. जिस फंक्शन में उन्हें एक पैरामीटर वेरिएबल के रूप में डिक्लेयर या डिफाइन किया गया है।
  • Lifetime – लोकल वेरिएबल किसी यूजर डिफाइन फ़ंक्शन के कॉल होने पर क्रिएट किये जाते है, और प्रोग्राम फ़ंक्शन के एग्ज़िट होने पर अपने आप टर्मिनेट हो जाते है।
  • Visibility – यूजर डिफाइन फंक्शन में लोकल वेरिएबल सिर्फ़ उसी फ़ंक्शन या ब्लॉक के अंदर से ही एक्सेस और मैनेज किए जा सकते है. जिस फंक्शन में उन्हें डिक्लेयर या डिफाइन किया गया है।
  • Storage – लोकल वेरिएबल को किसी फंक्शन में स्टैक आर्डर पर स्टोर किया जाता है. मोस्ट ऑफ़ फंक्शन प्रोग्राम केसेस में।

Syntax of a Local Variable.

return_type function_name() {

int localPara; // integer Local variable define here

// you can write your custom Code here with use of localPara variable

}

Example of a local variable in C++.

#include <iostream>

using namespace std;

void preview() {

    int localPara = 4;  // here we define a Local variable inside the function

    cout << “here is The local variable parameter – ” << localPara << endl;

}

int main() {

    preview();  // here it Calling the function that uses for the local variable value

    // here user define localVar cannot be accessed

    return 0;

}

Local variable explanation.

  • यहाँ दिए गए एक्साम्प्ल में, localPara एक लोकल इन्टिजर वेरिएबल डिफाइन है, जिसे preview() फ़ंक्शन के अंदर डिक्लेयर किया गया है। localPara को सिर्फ़ preview()  फ़ंक्शन के अंदर से ही एक्सेस और मैनेज किया जा सकता है। यदि आप preview() को फ़ंक्शन के बाहर (जैसे, main() में) एक्सेस करने की कोशिश की करते हैं. तो C++ यूजर को एक कम्पाइलेशन एरर डिस्प्ले होता है. क्योंकि इसमें localPara एक लोकल वेरिएबल स्कोप से बाहर है।

Global Variable Concept in C++.

फंक्शन प्रोग्राम में ग्लोबल वेरिएबल सभी फ़ंक्शन के बाहर की साइड में डिक्लेयर और डिफाइन किए जाते हैं. सामान्य रूप से किसी फंक्शन प्रोग्राम के टॉप पर ग्लोबल वेरिएबल डिफाइन किये जाते है। याद रहे, फंक्शन प्रोग्राम में ग्लोबल वेरिएबल प्रोग्राम के किसी भी हिस्से से एक्सेस और ग्लोबली मैनेज किए जा सकते हैं. जिसमें फ़ंक्शन के अंदर वाला एरिया भी शामिल होता है।

Important features and functions of global variables.

  • Scope – किसी फंक्शन प्रोग्राम में ग्लोबल वेरिएबल स्कोप का अर्थ डिक्लेयर होने के बाद ग्लोबल वेरिएबल प्रोग्राम में कहीं से भी एक्सेस और मैनेज किए जा सकते हैं।
  • Lifetime – ग्लोबल वेरिएबल को प्रोग्राम स्टार्ट होने पर क्रिएट किया जाता है, और प्रोग्राम टर्मिनेट होने पर ग्लोबल वेरिएबल अपने आप समाप्त हो जाते है।
  • Visibility – ग्लोबल वेरिएबल किसी फंक्शन प्रोग्राम के सभी फ़ंक्शन और ब्लॉक में एक्सेसिबल और मैनेजेबल होते है।
  • Storage – ग्लोबल वेरिएबल को फंक्शन में डेटा सेगमेंट नाम के एक स्पेशल मेमोरी एरिया में स्टोर या होल्ड किया जाता है।

Syntax of global variables.

int globalPara = 7; // This is a global variable method declared outside of any function.

return_type function_name() {

// Here you can create your own custom global variable code to access globalPara element.

}

Example of global variables.

#include <iostream>

using namespace std;

int globalPara = 7;  // outside the function integer Global variable define

void preview() {

    cout << “The universal global variable value is – ” << globalPara << endl;

}

int main() {

    preview();  // here it Calling the function that uses the global variable method

    globalPara = 9;  // here we Modify the global variable value

    preview();  // here it Calling the function again

    return 0;

}

global variables explanation.

  • यहाँ दिए गए एक्साम्प्ल में, globalPara फंक्शन में एक ग्लोबल वेरिएबल के रूप में डिफाइन है। इसे main() और preview() फ़ंक्शन के बाहर ग्लोबल वेरिएबल के रूप में डिक्लेयर किया गया है. इस वजह से इसे दोनों फ़ंक्शन द्वारा एक्सेस और मॉडिफ़ाई किया जा सकता है। यहाँ आप देख सकते हैं कि जब globalPara की वैल्यू को main() में मॉडिफ़ाई किया गया है, और यह मॉडिफिकेशन preview() फ़ंक्शन में भी डिस्प्ले होता है।

Combined Local and Global Variables Function Example.

#include <iostream>

using namespace std;

// Global variable with data name define here

int data = 70;

void preview()

{

    // Local variable declare inside the function

    int data = 40;

    cout << “Local variable inside preview() local function – ” << data << endl;

    cout << “Global variable inside preview() global funtion – ” << ::data << endl;

}

int main()

{

    // Local variable declare inside the main function

    int data = 15;

    cout << “Local variable inside the main() function – ” << data << endl;

    cout << “Global variable inside the main() funtion – ” << ::data << endl;

    preview();

    return 0;

}

Detail explanation of Local vs Global Variables in c++

Each AspectLocal Variables featuresGlobal Variables features
Each ScopeFunction declares local variable use Limited to the function/block in which they are declared or define by programmer.Global variable is Accessible throughout the entire program or anywhere in the declare function.
Lifetime featuresLocal variable Exists or remain only during the function call in program.Global variable Exists or use throughout the complete function program execution.
Visibility in programLocal variable Visible only within the function/block they are declared.Global variable Visible throughout the complete program inside or outside program.
Memory storageLocal variable Stored in the stack (temporary memory) order to process.Global variable Stored in the data segment (persistent memory) order.
Initialization methodLocal variable Must be explicitly initialized in function.Global variable Automatically initialized to 0 (if not set) or define.
Modification allowedLocal variable Can be modified within their function body when need.Global variable Can be modified by any function in the complete program.

Leave a Reply