Thursday, 21 September 2017

C++ program to demonstrate use of local and global variable

/*
 C++ program to demonstrate use of local and global variable
*/
#include "iostream"
using namespace std;

int num;
void myfunc();

int main(){

 num = 10;
 cout<<"Num in main: "<<num;
 myfunc();
 myfunc();
 cout<<"\n Num at the end: "<<num; 
 return 0;
}

void myfunc(){
 int num=20;
 cout<<"\nNum: "<<num;
 num += 10;
}

/*
--In case of same variable name (both local and global), reference will be always local.  
 
[pavan@localhost Practise]$ g++ LocalVariable.cpp
[pavan@localhost Practise]$ ./a.out 
Num in main: 10
Num: 20
Num: 20
 Num at the end: 10[pavan@localhost Practise]$ 


*/

No comments: