Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 91613a7

Browse files
Local and global variable, block level scope example
1 parent fd3dcc6 commit 91613a7

File tree

3 files changed

+53
-0
lines changed
  • Learn_CPP_Programming_Deep_Dive/Section 10 Functions

3 files changed

+53
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include <iostream>
2+
3+
using namespace std;
4+
5+
int global_var = 12;
6+
7+
void fun()
8+
{
9+
int local_var = 45;
10+
cout<<"Local variable "<<local_var<<endl;
11+
12+
global_var++;
13+
cout<<"The global variable is "<<global_var<<endl; //13
14+
}
15+
16+
int main(void)
17+
{
18+
cout<<"The global variable is "<<global_var<<endl; //12
19+
fun();
20+
cout<<"The global variable is "<<global_var<<endl; //13
21+
22+
return 0;
23+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include <iostream>
2+
3+
using namespace std;
4+
5+
int my_var = 45;
6+
7+
int main(void)
8+
{
9+
int my_var = 70;
10+
11+
{
12+
int my_var = 69;
13+
cout<<my_var<<endl; //69
14+
}
15+
16+
cout<<my_var<<endl; // 70
17+
18+
cout<<::my_var<<endl; // 45
19+
return 0;
20+
}

‎Learn_CPP_Programming_Deep_Dive/Section 10 Functions/Return_by_reference/main.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,21 @@ int & fun(int &a)
88
return a;
99
}
1010

11+
int fun1(int b)
12+
{
13+
14+
return 2*b;
15+
}
16+
1117
int main(void)
1218
{
1319
int x = 1953;
1420

1521
fun(x) = 1954;
1622

23+
int y = fun1(3);
24+
cout<<"The value of y is "<<y<<endl;
25+
26+
1727
cout<<"The value of x is "<<x<<endl;
1828
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /