-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathC++ Function Syntax Pass By Reference.cpp
More file actions
51 lines (36 loc) · 1.26 KB
/
C++ Function Syntax Pass By Reference.cpp
File metadata and controls
51 lines (36 loc) · 1.26 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
/**
[PROGRAM] : C++ Functions - Pass By Reference
[AUTHOR] : Saddam Arbaa
[Email] : <saddamarbaas@gmail.com> */
#include <iostream>
using namespace std;
// function to swap values of two variables
void swapNums(int &a, int &b);
// the main Function
int main()
{
// local variable declaration:
int firstNum = 12;
int secondNum = 30;
cout << "Before swap: " << "\n";
cout << "Before swap, value of first Number : " << firstNum << endl;
cout << "Before swap, value of second Number : " << secondNum << endl;
/* calling a function to swap the values using variable reference.*/
swapNums(firstNum, secondNum);
cout << "After swap: " << "\n";
cout << "After swap, value of first Number : " << firstNum << endl;
cout << "After swap, value of second Number : " << secondNum << endl;
return 0;// signal to operating system everything works fine
}/** End of main function */
/**
function to swap values of two variables
(user should pass address of two as parameter)*/
void swapNums(int &a, int &b)
{
// declare temp variable
int temp;
temp = a; /* save the value at address a in temp */
a = b; /* put b into a */
b = temp; /* put a into b */
// swap is done
}/** End of swap () */