-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExplore User Defined Functions
84 lines (67 loc) · 1.48 KB
/
Explore User Defined Functions
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
81
82
83
84
// S112 Kowata Explore User Defined Functions.cpp
//Preprocessor Declarations
#include <iostream>
#include <string>
using namespace std;
//Prototypes
void experiment0();
void experiment01(int num);
void experiment02(int& num);
void experiment03();
string getName();
int main()
{
experiment03();
}
//User-defined functions ---------------------------------
//Understanding passing by value and passing by reference
void experiment03()
{
string fullName = getName();
cout << "You entered: " << fullName;
}
//-----------------------------------------
string getName()
{
string fName;
do {
cout << "Enter a full name: ";
getline(cin, fName);
if (fName.length() == 0)
{
cout << "invalid name -- cannot be empty\n";
}
else
{
break;
}
} while (true);
return fName;
}
//-----------------------------------------
void experiment0()
{
int n = 100;
cout << "Before experiment01\n";
cout << "n before calling: " << n << endl;
experiment02(n);
cout << "n after calling: " << n << endl;
cout << "After experiment01\n";
cout << "\nAll Done\n";
}
//num accepts data by VALUE (copy)
void experiment01(int num)
{
cout << "Inside experiment01\n";
cout << num << endl;
num++;
cout << num++ << endl;
}
//num accepts data by REFERENCE (address of)
void experiment02(int& num)
{
cout << "Inside experiment02\n";
cout << num << endl;
num++;
cout << num++ << endl;
}