Passing Global Variables in C++ | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Passing Global Variables in C++

Can we pass the address of global variables in C++(pass by reference) .Will it affect the actual value of the variable?

29th Jun 2017, 2:34 AM
Nirmal Kumar Bhakat
Nirmal Kumar Bhakat - avatar
4 Answers
+ 9
There is no need to pass a global variable. You can directly alter the value of a global variable in any existing function.
29th Jun 2017, 2:53 AM
Hatsy Rei
Hatsy Rei - avatar
+ 9
#include <iostream> int y = 10; void test(int& num) { num = 11; } int main() { test(y); std::cout << y; return 0; } // outputs 11
29th Jun 2017, 3:08 AM
Hatsy Rei
Hatsy Rei - avatar
+ 1
but if we do will the value be altered
29th Jun 2017, 3:03 AM
Nirmal Kumar Bhakat
Nirmal Kumar Bhakat - avatar
0
thanks bro
29th Jun 2017, 3:03 AM
Nirmal Kumar Bhakat
Nirmal Kumar Bhakat - avatar