FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
cpp-tutorials/015-ptr-vs-ref-for-output/main.cpp at main · SuboptimalEng/cpp-tutorials · GitHub
SuboptimalEng
/
cpp-tutorials
Public
Notifications
You must be signed in to change notification settings
Fork
1
Star
17
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
cpp-tutorials
/
015-ptr-vs-ref-for-output
/
main.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
80 lines (68 loc) · 1.6 KB
Breadcrumbs
cpp-tutorials
/
015-ptr-vs-ref-for-output
/
main.cpp
Copy path
File metadata and controls
80 lines (68 loc) · 1.6 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#
include
<
stdlib.h
>
#
include
<
iostream
>
int
sum
(
int
& a,
int
* b);
int
sum
(
int
& a,
int
* b) {
return
a + *b;
}
void
PrintText
(std::string s) {
std::cout <<
"
---
"
<< s <<
"
---
"
<< std::endl;
}
void
PrintVariables
(
int
a,
int
b,
int
c) {
std::cout << std::endl;
std::cout <<
"
a:
"
<< a << std::endl;
std::cout <<
"
b:
"
<< b << std::endl;
std::cout <<
"
c:
"
<< c << std::endl;
std::cout << std::endl;
}
void
PrintResult
(
int
result) {
std::cout <<
"
result ->
"
<< result << std::endl;
}
int
DoubleVarsAndAdd_v1
(
int
a,
int
b,
int
c) {
a = a *
2
;
b = b *
2
;
c = c *
2
;
return
a + b + c;
}
//
Should we rename this fn instead?
int
DoubleVarsAndAdd_v2
(
int
a,
int
b,
int
& c) {
a = a *
2
;
b = b *
2
;
c = c *
2
;
return
a + b + c;
}
int
DoubleVarsAndAdd_v3
(
int
a,
int
b,
int
* c) {
a = a *
2
;
b = b *
2
;
(*c) = (*c) *
2
;
return
a + b + (*c);
}
int
main
() {
//
--- v1: Simple Example ---
//
int a = 1;
//
int b = 1;
//
int c = 1;
//
PrintText("Version 1");
//
PrintVariables(a, b, c);
//
int result_v1 = DoubleVarsAndAdd_v1(a, b, c);
//
PrintResult(result_v1);
//
PrintVariables(a, b, c);
//
--- v2: Pass by Reference ---
//
int a = 1;
//
int b = 1;
//
int c = 1;
//
PrintText("Version 2");
//
PrintVariables(a, b, c);
//
int result_v2 = DoubleVarsAndAdd_v2(a, b, c);
//
PrintResult(result_v2);
//
PrintVariables(a, b, c);
//
--- v3: Pass with Pointer ---
int
a =
1
;
int
b =
1
;
int
c =
1
;
PrintText
(
"
Version 3
"
);
PrintVariables
(a, b, c);
int
result_v3 =
DoubleVarsAndAdd_v3
(a, b, &c);
PrintResult
(result_v3);
PrintVariables
(a, b, c);
return
0
;
}
Back
|
FazBrowse Home
|
New Git URL