-
Notifications
You must be signed in to change notification settings - Fork 0
/
PassingArguments.cpp
41 lines (33 loc) · 976 Bytes
/
PassingArguments.cpp
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
#include <iostream>
#include <string>
// Passing by Value/Copy
void myfunction(int byvalue)
{
std::cout << "Argument passed by value: " << byvalue;
}
// Passing by Reference
void myfunctionRef(int& byreference) // O Endereço
{
byreference++; // we can modify the value of the argument
std::cout << "Argument passed by reference: " << byreference;
}
// We use passing by const reference for efficiency reasons,
// and the const modifier ensures the value of an argument will
// not be changed.
void myfunctionConst(const std::string& byconstreference)
{
std::cout << "Arguments passed by const reference: " <<
byconstreference;
}
// Function Overloading
void myprint(char param);
void myprint(int param);
void myprint(double param);
int main() {
myfunction(123);
int x = 123;
// When passing by reference, we need to pass the variable
// itself; we can’t pass in a literal representing a value.
// Passing by reference is best avoided.
myfunctionRef(x);
}