-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPassByReferenceDEMO.cpp
More file actions
89 lines (67 loc) · 2.38 KB
/
Copy pathPassByReferenceDEMO.cpp
File metadata and controls
89 lines (67 loc) · 2.38 KB
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
85
86
87
88
89
//************************************************************************
// Author: Rolando Carreon
// Date: mar 6 2025
// Language: C++
// Assignment: PassByValueDEMO
// Description: swaps values using pass-by-value and pass-by-reference
// to show the way it affects varibales values
//************************************************************************
#include<iostream>
using namespace std;
// function protypes
void callByValue(int, int);
void callByReference(int&, int&);
int main()
{
// declare local variables
int firstNumber = 10;
int secondNumber = 5;
// display values before any function calls
cout << "Before the callByValue.\n";
cout << "\t First Number: " << firstNumber << endl;
cout << "\t Second Number: " << secondNumber << endl;
// call the callByValue functin
callByValue(firstNumber, secondNumber);
// display values after callByValue
cout << "After the callByValue.\n";
cout << "\t First Number: " << firstNumber << endl;
cout << "\t Second Number: " << secondNumber << endl;
// call the callByReference function
callByReference(firstNumber, secondNumber);
// display values after callByReference
cout << "After the callByReference.\n";
cout << "\t First Number: " << firstNumber << endl;
cout << "\t Second Number: " << secondNumber << endl;
return 0;
}
// function definitions
// function that accepts two integers passed by value,
// swaps the values and displays the values
void callByValue(int numOne, int numTwo)
{
// declare variables
int temp;
// use temp to swap the value
temp = numOne;
numOne = numTwo;
numTwo = temp;
// display values inside callByValue function
cout << "Inside the callByValue.\n";
cout << "\t First Number: " << numOne << endl;
cout << "\t Second Number: " << numTwo << endl;
}
// function that accepts two integers passed by two integers passed by refference,
// swaps the values and displays the values
void callByReference(int& numOne, int& numTwo)
{
// declare variables
int temp;
// use temp to swap the value
temp = numOne;
numOne = numTwo;
numTwo = temp;
// display values inside callByReference function
cout << "Inside the callByReference.\n";
cout << "\t First Number: " << numOne << endl;
cout << "\t Second Number: " << numTwo << endl;
}